Files
tasks/common/persistence/tests/task_queue_task.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

292 lines
7.9 KiB
Go

package tests
import (
"context"
"math/rand"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
enumspb "go.temporal.io/api/enums/v1"
clockspb "go.temporal.io/server/api/clock/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
"go.temporal.io/server/common/debug"
"go.temporal.io/server/common/log"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/serialization"
"go.temporal.io/server/common/testing/protorequire"
"google.golang.org/protobuf/types/known/timestamppb"
)
type (
TaskQueueTaskSuite struct {
suite.Suite
*require.Assertions
stickyTTL time.Duration
taskTTL time.Duration
namespaceID string
taskQueueName string
taskQueueType enumspb.TaskQueueType
taskManager p.TaskManager
logger log.Logger
ctx context.Context
cancel context.CancelFunc
}
)
func NewTaskQueueTaskSuite(
t *testing.T,
taskManager p.TaskStore,
logger log.Logger,
) *TaskQueueTaskSuite {
return &TaskQueueTaskSuite{
Assertions: require.New(t),
taskManager: p.NewTaskManager(
taskManager,
serialization.NewSerializer(),
),
logger: logger,
}
}
func (s *TaskQueueTaskSuite) SetupSuite() {
}
func (s *TaskQueueTaskSuite) TearDownSuite() {
}
func (s *TaskQueueTaskSuite) SetupTest() {
s.Assertions = require.New(s.T())
s.ctx, s.cancel = context.WithTimeout(context.Background(), 30*time.Second*debug.TimeoutMultiplier)
s.stickyTTL = time.Second * 10
s.taskTTL = time.Second * 16
s.namespaceID = uuid.New().String()
s.taskQueueName = uuid.New().String()
s.taskQueueType = enumspb.TaskQueueType(rand.Int31n(
int32(len(enumspb.TaskQueueType_name)) + 1),
)
}
func (s *TaskQueueTaskSuite) TearDownTest() {
s.cancel()
}
func (s *TaskQueueTaskSuite) TestCreateGet_Conflict() {
rangeID := rand.Int63()
taskQueue := s.createTaskQueue(rangeID)
taskID := rand.Int63()
task := s.randomTask(taskID)
_, err := s.taskManager.CreateTasks(s.ctx, &p.CreateTasksRequest{
TaskQueueInfo: &p.PersistedTaskQueueInfo{
RangeID: rand.Int63(),
Data: taskQueue,
},
Tasks: []*persistencespb.AllocatedTaskInfo{task},
})
s.IsType(&p.ConditionFailedError{}, err)
resp, err := s.taskManager.GetTasks(s.ctx, &p.GetTasksRequest{
NamespaceID: s.namespaceID,
TaskQueue: s.taskQueueName,
TaskType: s.taskQueueType,
InclusiveMinTaskID: taskID,
ExclusiveMaxTaskID: taskID + 1,
PageSize: 100,
NextPageToken: nil,
})
s.NoError(err)
protorequire.ProtoSliceEqual(s.T(), []*persistencespb.AllocatedTaskInfo{}, resp.Tasks)
s.Nil(resp.NextPageToken)
}
func (s *TaskQueueTaskSuite) TestCreateGet_One() {
rangeID := rand.Int63()
taskQueue := s.createTaskQueue(rangeID)
taskID := rand.Int63()
task := s.randomTask(taskID)
_, err := s.taskManager.CreateTasks(s.ctx, &p.CreateTasksRequest{
TaskQueueInfo: &p.PersistedTaskQueueInfo{
RangeID: rangeID,
Data: taskQueue,
},
Tasks: []*persistencespb.AllocatedTaskInfo{task},
})
s.NoError(err)
resp, err := s.taskManager.GetTasks(s.ctx, &p.GetTasksRequest{
NamespaceID: s.namespaceID,
TaskQueue: s.taskQueueName,
TaskType: s.taskQueueType,
InclusiveMinTaskID: taskID,
ExclusiveMaxTaskID: taskID + 1,
PageSize: 100,
NextPageToken: nil,
})
s.NoError(err)
protorequire.ProtoSliceEqual(s.T(), []*persistencespb.AllocatedTaskInfo{task}, resp.Tasks)
s.Nil(resp.NextPageToken)
}
func (s *TaskQueueTaskSuite) TestCreateGet_Multiple() {
numCreateBatch := 32
createBatchSize := 32
numTasks := int64(createBatchSize * numCreateBatch)
minTaskID := rand.Int63()
maxTaskID := minTaskID + numTasks
rangeID := rand.Int63()
taskQueue := s.createTaskQueue(rangeID)
var expectedTasks []*persistencespb.AllocatedTaskInfo
for i := 0; i < numCreateBatch; i++ {
var tasks []*persistencespb.AllocatedTaskInfo
for j := 0; j < createBatchSize; j++ {
taskID := minTaskID + int64(i*numCreateBatch+j)
task := s.randomTask(taskID)
tasks = append(tasks, task)
expectedTasks = append(expectedTasks, task)
}
_, err := s.taskManager.CreateTasks(s.ctx, &p.CreateTasksRequest{
TaskQueueInfo: &p.PersistedTaskQueueInfo{
RangeID: rangeID,
Data: taskQueue,
},
Tasks: tasks,
})
s.NoError(err)
}
var token []byte
var actualTasks []*persistencespb.AllocatedTaskInfo
for doContinue := true; doContinue; doContinue = len(token) > 0 {
resp, err := s.taskManager.GetTasks(s.ctx, &p.GetTasksRequest{
NamespaceID: s.namespaceID,
TaskQueue: s.taskQueueName,
TaskType: s.taskQueueType,
InclusiveMinTaskID: minTaskID,
ExclusiveMaxTaskID: maxTaskID + 1,
PageSize: 1,
NextPageToken: token,
})
s.NoError(err)
token = resp.NextPageToken
actualTasks = append(actualTasks, resp.Tasks...)
}
protorequire.ProtoSliceEqual(s.T(), expectedTasks, actualTasks)
}
func (s *TaskQueueTaskSuite) TestCreateDelete_Multiple() {
numCreateBatch := 32
createBatchSize := 32
numTasks := int64(createBatchSize * numCreateBatch)
minTaskID := rand.Int63()
maxTaskID := minTaskID + numTasks
rangeID := rand.Int63()
taskQueue := s.createTaskQueue(rangeID)
for i := 0; i < numCreateBatch; i++ {
var tasks []*persistencespb.AllocatedTaskInfo
for j := 0; j < createBatchSize; j++ {
taskID := minTaskID + int64(i*numCreateBatch+j)
task := s.randomTask(taskID)
tasks = append(tasks, task)
}
_, err := s.taskManager.CreateTasks(s.ctx, &p.CreateTasksRequest{
TaskQueueInfo: &p.PersistedTaskQueueInfo{
RangeID: rangeID,
Data: taskQueue,
},
Tasks: tasks,
})
s.NoError(err)
}
_, err := s.taskManager.CompleteTasksLessThan(s.ctx, &p.CompleteTasksLessThanRequest{
NamespaceID: s.namespaceID,
TaskQueueName: s.taskQueueName,
TaskType: s.taskQueueType,
ExclusiveMaxTaskID: maxTaskID + 1,
Limit: int(numTasks),
})
s.NoError(err)
resp, err := s.taskManager.GetTasks(s.ctx, &p.GetTasksRequest{
NamespaceID: s.namespaceID,
TaskQueue: s.taskQueueName,
TaskType: s.taskQueueType,
InclusiveMinTaskID: minTaskID,
ExclusiveMaxTaskID: maxTaskID + 1,
PageSize: 100,
NextPageToken: nil,
})
s.NoError(err)
protorequire.ProtoSliceEqual(s.T(), []*persistencespb.AllocatedTaskInfo{}, resp.Tasks)
s.Nil(resp.NextPageToken)
}
func (s *TaskQueueTaskSuite) createTaskQueue(
rangeID int64,
) *persistencespb.TaskQueueInfo {
taskQueueKind := enumspb.TaskQueueKind(rand.Int31n(
int32(len(enumspb.TaskQueueKind_name)) + 1),
)
taskQueue := s.randomTaskQueueInfo(taskQueueKind)
_, err := s.taskManager.CreateTaskQueue(s.ctx, &p.CreateTaskQueueRequest{
RangeID: rangeID,
TaskQueueInfo: taskQueue,
})
s.NoError(err)
return taskQueue
}
func (s *TaskQueueTaskSuite) randomTaskQueueInfo(
taskQueueKind enumspb.TaskQueueKind,
) *persistencespb.TaskQueueInfo {
now := time.Now().UTC()
var expiryTime *timestamppb.Timestamp
if taskQueueKind == enumspb.TASK_QUEUE_KIND_STICKY {
expiryTime = timestamppb.New(now.Add(s.stickyTTL))
}
return &persistencespb.TaskQueueInfo{
NamespaceId: s.namespaceID,
Name: s.taskQueueName,
TaskType: s.taskQueueType,
Kind: taskQueueKind,
AckLevel: rand.Int63(),
ExpiryTime: expiryTime,
LastUpdateTime: timestamppb.New(now),
}
}
func (s *TaskQueueTaskSuite) randomTask(
taskID int64,
) *persistencespb.AllocatedTaskInfo {
now := time.Now().UTC()
return &persistencespb.AllocatedTaskInfo{
TaskId: taskID,
Data: &persistencespb.TaskInfo{
NamespaceId: s.namespaceID,
WorkflowId: uuid.New().String(),
RunId: uuid.New().String(),
ScheduledEventId: rand.Int63(),
CreateTime: timestamppb.New(now),
ExpiryTime: timestamppb.New(now.Add(s.taskTTL)),
Clock: &clockspb.VectorClock{
ClusterId: rand.Int63(),
ShardId: rand.Int31(),
Clock: rand.Int63(),
},
},
}
}