go.temporal.io/server@v1.23.0/common/persistence/serialization/task_serializer_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package serialization
    26  
    27  import (
    28  	"math/rand"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/google/uuid"
    33  	"github.com/stretchr/testify/require"
    34  	"github.com/stretchr/testify/suite"
    35  	enumspb "go.temporal.io/api/enums/v1"
    36  
    37  	enumsspb "go.temporal.io/server/api/enums/v1"
    38  	"go.temporal.io/server/common/definition"
    39  	"go.temporal.io/server/common/shuffle"
    40  	"go.temporal.io/server/service/history/tasks"
    41  )
    42  
    43  type (
    44  	taskSerializerSuite struct {
    45  		suite.Suite
    46  		*require.Assertions
    47  
    48  		workflowKey    definition.WorkflowKey
    49  		taskSerializer *TaskSerializer
    50  	}
    51  )
    52  
    53  func TestTaskSerializerSuite(t *testing.T) {
    54  	suite.Run(t, new(taskSerializerSuite))
    55  }
    56  
    57  func (s *taskSerializerSuite) SetupSuite() {
    58  
    59  }
    60  
    61  func (s *taskSerializerSuite) TearDownSuite() {
    62  
    63  }
    64  
    65  func (s *taskSerializerSuite) SetupTest() {
    66  	s.Assertions = require.New(s.T())
    67  
    68  	s.workflowKey = definition.NewWorkflowKey(
    69  		"random namespace ID",
    70  		"random workflow ID",
    71  		"random run ID",
    72  	)
    73  	s.taskSerializer = NewTaskSerializer()
    74  }
    75  
    76  func (s *taskSerializerSuite) TearDownTest() {
    77  
    78  }
    79  
    80  func (s *taskSerializerSuite) TestTransferWorkflowTask() {
    81  	workflowTask := &tasks.WorkflowTask{
    82  		WorkflowKey:         s.workflowKey,
    83  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
    84  		TaskID:              rand.Int63(),
    85  		TaskQueue:           shuffle.String("random task queue name"),
    86  		ScheduledEventID:    rand.Int63(),
    87  		Version:             rand.Int63(),
    88  	}
    89  
    90  	s.assertEqualTasks(workflowTask)
    91  }
    92  
    93  func (s *taskSerializerSuite) TestTransferActivityTask() {
    94  	activityTask := &tasks.ActivityTask{
    95  		WorkflowKey:         s.workflowKey,
    96  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
    97  		TaskID:              rand.Int63(),
    98  		TaskQueue:           shuffle.String("random task queue name"),
    99  		ScheduledEventID:    rand.Int63(),
   100  		Version:             rand.Int63(),
   101  	}
   102  
   103  	s.assertEqualTasks(activityTask)
   104  }
   105  
   106  func (s *taskSerializerSuite) TestTransferRequestCancelTask() {
   107  	requestCancelTask := &tasks.CancelExecutionTask{
   108  		WorkflowKey:             s.workflowKey,
   109  		VisibilityTimestamp:     time.Unix(0, rand.Int63()).UTC(),
   110  		TaskID:                  rand.Int63(),
   111  		TargetNamespaceID:       uuid.New().String(),
   112  		TargetWorkflowID:        uuid.New().String(),
   113  		TargetRunID:             uuid.New().String(),
   114  		TargetChildWorkflowOnly: rand.Int63()%2 == 0,
   115  		InitiatedEventID:        rand.Int63(),
   116  		Version:                 rand.Int63(),
   117  	}
   118  
   119  	s.assertEqualTasks(requestCancelTask)
   120  }
   121  
   122  func (s *taskSerializerSuite) TestTransferSignalTask() {
   123  	signalTask := &tasks.SignalExecutionTask{
   124  		WorkflowKey:             s.workflowKey,
   125  		VisibilityTimestamp:     time.Unix(0, rand.Int63()).UTC(),
   126  		TaskID:                  rand.Int63(),
   127  		TargetNamespaceID:       uuid.New().String(),
   128  		TargetWorkflowID:        uuid.New().String(),
   129  		TargetRunID:             uuid.New().String(),
   130  		TargetChildWorkflowOnly: rand.Int63()%2 == 0,
   131  		InitiatedEventID:        rand.Int63(),
   132  		Version:                 rand.Int63(),
   133  	}
   134  
   135  	s.assertEqualTasks(signalTask)
   136  }
   137  
   138  func (s *taskSerializerSuite) TestTransferChildWorkflowTask() {
   139  	childWorkflowTask := &tasks.StartChildExecutionTask{
   140  		WorkflowKey:         s.workflowKey,
   141  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   142  		TaskID:              rand.Int63(),
   143  		TargetNamespaceID:   uuid.New().String(),
   144  		TargetWorkflowID:    uuid.New().String(),
   145  		InitiatedEventID:    rand.Int63(),
   146  		Version:             rand.Int63(),
   147  	}
   148  
   149  	s.assertEqualTasks(childWorkflowTask)
   150  }
   151  
   152  func (s *taskSerializerSuite) TestTransferCloseTask() {
   153  	closeTask := &tasks.CloseExecutionTask{
   154  		WorkflowKey:         s.workflowKey,
   155  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   156  		TaskID:              rand.Int63(),
   157  		Version:             rand.Int63(),
   158  	}
   159  	s.assertEqualTasks(closeTask)
   160  }
   161  
   162  func (s *taskSerializerSuite) TestTransferResetTask() {
   163  	resetTask := &tasks.ResetWorkflowTask{
   164  		WorkflowKey:         s.workflowKey,
   165  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   166  		TaskID:              rand.Int63(),
   167  		Version:             rand.Int63(),
   168  	}
   169  
   170  	s.assertEqualTasks(resetTask)
   171  }
   172  
   173  func (s *taskSerializerSuite) TestTimerWorkflowTask() {
   174  	workflowTaskTimer := &tasks.WorkflowTaskTimeoutTask{
   175  		WorkflowKey:         s.workflowKey,
   176  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   177  		TaskID:              rand.Int63(),
   178  		EventID:             rand.Int63(),
   179  		ScheduleAttempt:     rand.Int31(),
   180  		TimeoutType:         enumspb.TimeoutType(rand.Int31n(int32(len(enumspb.TimeoutType_name)))),
   181  		Version:             rand.Int63(),
   182  	}
   183  
   184  	s.assertEqualTasks(workflowTaskTimer)
   185  }
   186  
   187  func (s *taskSerializerSuite) TestTimerWorkflowDelayTask() {
   188  	workflowDelayTimer := &tasks.WorkflowBackoffTimerTask{
   189  		WorkflowKey:         s.workflowKey,
   190  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   191  		TaskID:              rand.Int63(),
   192  		WorkflowBackoffType: enumsspb.WorkflowBackoffType(rand.Int31n(int32(len(enumsspb.WorkflowBackoffType_name)))),
   193  		Version:             rand.Int63(),
   194  	}
   195  
   196  	s.assertEqualTasks(workflowDelayTimer)
   197  }
   198  
   199  func (s *taskSerializerSuite) TestTimerActivityTask() {
   200  	activityTaskTimer := &tasks.ActivityTimeoutTask{
   201  		WorkflowKey:         s.workflowKey,
   202  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   203  		TaskID:              rand.Int63(),
   204  		EventID:             rand.Int63(),
   205  		Attempt:             rand.Int31(),
   206  		TimeoutType:         enumspb.TimeoutType(rand.Int31n(int32(len(enumspb.TimeoutType_name)))),
   207  		Version:             rand.Int63(),
   208  	}
   209  
   210  	s.assertEqualTasks(activityTaskTimer)
   211  }
   212  
   213  func (s *taskSerializerSuite) TestTimerActivityRetryTask() {
   214  	activityRetryTimer := &tasks.ActivityRetryTimerTask{
   215  		WorkflowKey:         s.workflowKey,
   216  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   217  		TaskID:              rand.Int63(),
   218  		EventID:             rand.Int63(),
   219  		Attempt:             rand.Int31(),
   220  		Version:             rand.Int63(),
   221  	}
   222  
   223  	s.assertEqualTasks(activityRetryTimer)
   224  }
   225  
   226  func (s *taskSerializerSuite) TestTimerUserTask() {
   227  	userTimer := &tasks.UserTimerTask{
   228  		WorkflowKey:         s.workflowKey,
   229  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   230  		TaskID:              rand.Int63(),
   231  		EventID:             rand.Int63(),
   232  		Version:             rand.Int63(),
   233  	}
   234  
   235  	s.assertEqualTasks(userTimer)
   236  }
   237  
   238  func (s *taskSerializerSuite) TestTimerWorkflowRun() {
   239  	workflowTimer := &tasks.WorkflowTimeoutTask{
   240  		WorkflowKey:         s.workflowKey,
   241  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   242  		TaskID:              rand.Int63(),
   243  		Version:             rand.Int63(),
   244  	}
   245  
   246  	s.assertEqualTasks(workflowTimer)
   247  }
   248  
   249  func (s *taskSerializerSuite) TestTimerWorkflowCleanupTask() {
   250  	workflowCleanupTimer := &tasks.DeleteHistoryEventTask{
   251  		WorkflowKey:         s.workflowKey,
   252  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   253  		TaskID:              rand.Int63(),
   254  		Version:             rand.Int63(),
   255  		BranchToken:         []byte{123},
   256  	}
   257  	s.assertEqualTasks(workflowCleanupTimer)
   258  }
   259  
   260  func (s *taskSerializerSuite) TestVisibilityStartTask() {
   261  	visibilityStart := &tasks.StartExecutionVisibilityTask{
   262  		WorkflowKey:         s.workflowKey,
   263  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   264  		TaskID:              rand.Int63(),
   265  		Version:             rand.Int63(),
   266  	}
   267  
   268  	s.assertEqualTasks(visibilityStart)
   269  }
   270  
   271  func (s *taskSerializerSuite) TestVisibilityUpsertTask() {
   272  	visibilityUpsert := &tasks.UpsertExecutionVisibilityTask{
   273  		WorkflowKey:         s.workflowKey,
   274  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   275  		TaskID:              rand.Int63(),
   276  		Version:             rand.Int63(),
   277  	}
   278  
   279  	s.assertEqualTasks(visibilityUpsert)
   280  }
   281  
   282  func (s *taskSerializerSuite) TestVisibilityCloseTask() {
   283  	visibilityClose := &tasks.CloseExecutionVisibilityTask{
   284  		WorkflowKey:         s.workflowKey,
   285  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   286  		TaskID:              rand.Int63(),
   287  		Version:             rand.Int63(),
   288  	}
   289  
   290  	s.assertEqualTasks(visibilityClose)
   291  }
   292  
   293  func (s *taskSerializerSuite) TestVisibilityDeleteTask() {
   294  	visibilityDelete := &tasks.CloseExecutionVisibilityTask{
   295  		WorkflowKey:         s.workflowKey,
   296  		VisibilityTimestamp: time.Unix(0, rand.Int63()).UTC(),
   297  		TaskID:              rand.Int63(),
   298  		Version:             rand.Int63(),
   299  	}
   300  
   301  	s.assertEqualTasks(visibilityDelete)
   302  }
   303  
   304  func (s *taskSerializerSuite) TestReplicateActivityTask() {
   305  	replicateActivityTask := &tasks.SyncActivityTask{
   306  		WorkflowKey:         s.workflowKey,
   307  		VisibilityTimestamp: time.Unix(0, 0).UTC(), // go == compare for location as well which is striped during marshaling/unmarshaling
   308  		TaskID:              rand.Int63(),
   309  		Version:             rand.Int63(),
   310  		ScheduledEventID:    rand.Int63(),
   311  	}
   312  
   313  	s.assertEqualTasks(replicateActivityTask)
   314  }
   315  
   316  func (s *taskSerializerSuite) TestReplicateHistoryTask() {
   317  	replicateHistoryTask := &tasks.HistoryReplicationTask{
   318  		WorkflowKey:         s.workflowKey,
   319  		VisibilityTimestamp: time.Unix(0, 0).UTC(), // go == compare for location as well which is striped during marshaling/unmarshaling
   320  		TaskID:              rand.Int63(),
   321  		Version:             rand.Int63(),
   322  		FirstEventID:        rand.Int63(),
   323  		NextEventID:         rand.Int63(),
   324  		BranchToken:         shuffle.Bytes([]byte("random branch token")),
   325  		NewRunBranchToken:   shuffle.Bytes([]byte("random new branch token")),
   326  	}
   327  
   328  	s.assertEqualTasks(replicateHistoryTask)
   329  }
   330  
   331  func (s *taskSerializerSuite) TestDeleteExecutionVisibilityTask() {
   332  	replicateHistoryTask := &tasks.DeleteExecutionVisibilityTask{
   333  		WorkflowKey:                    s.workflowKey,
   334  		VisibilityTimestamp:            time.Unix(0, 0).UTC(), // go == compare for location as well which is striped during marshaling/unmarshaling
   335  		TaskID:                         rand.Int63(),
   336  		Version:                        rand.Int63(),
   337  		CloseExecutionVisibilityTaskID: rand.Int63(),
   338  	}
   339  
   340  	s.assertEqualTasks(replicateHistoryTask)
   341  }
   342  
   343  func (s *taskSerializerSuite) TestDeleteExecutionTask() {
   344  	replicateHistoryTask := &tasks.DeleteExecutionTask{
   345  		WorkflowKey:         s.workflowKey,
   346  		VisibilityTimestamp: time.Unix(0, 0).UTC(), // go == compare for location as well which is striped during marshaling/unmarshaling
   347  		TaskID:              rand.Int63(),
   348  		Version:             rand.Int63(),
   349  	}
   350  
   351  	s.assertEqualTasks(replicateHistoryTask)
   352  }
   353  
   354  func (s *taskSerializerSuite) TestArchiveExecutionTask() {
   355  	task := &tasks.ArchiveExecutionTask{
   356  		WorkflowKey:         s.workflowKey,
   357  		VisibilityTimestamp: time.Unix(0, 0).UTC(), // go == compare for location as well which is striped during marshaling/unmarshaling
   358  		TaskID:              rand.Int63(),
   359  		Version:             rand.Int63(),
   360  	}
   361  	s.Assert().Equal(tasks.CategoryArchival, task.GetCategory())
   362  	s.Assert().Equal(enumsspb.TASK_TYPE_ARCHIVAL_ARCHIVE_EXECUTION, task.GetType())
   363  
   364  	s.assertEqualTasks(task)
   365  }
   366  
   367  func (s *taskSerializerSuite) assertEqualTasks(
   368  	task tasks.Task,
   369  ) {
   370  	blob, err := s.taskSerializer.SerializeTask(task)
   371  	s.NoError(err)
   372  	deserializedTask, err := s.taskSerializer.DeserializeTask(task.GetCategory(), blob)
   373  	s.NoError(err)
   374  	s.Equal(task, deserializedTask)
   375  }