mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
355 lines
10 KiB
Go
355 lines
10 KiB
Go
package cassandra
|
|
|
|
import (
|
|
"errors"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gocql/gocql"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/stretchr/testify/suite"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
enumsspb "github.com/hanzoai/tasks/api/enums/v1"
|
|
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
|
|
"github.com/hanzoai/tasks/common"
|
|
p "github.com/hanzoai/tasks/common/persistence"
|
|
"github.com/hanzoai/tasks/common/persistence/serialization"
|
|
"github.com/hanzoai/tasks/common/primitives/timestamp"
|
|
"github.com/hanzoai/tasks/common/testing/protorequire"
|
|
)
|
|
|
|
type (
|
|
cassandraErrorsSuite struct {
|
|
suite.Suite
|
|
*require.Assertions
|
|
protorequire.ProtoAssertions
|
|
}
|
|
)
|
|
|
|
func TestCassandraErrorsSuite(t *testing.T) {
|
|
s := new(cassandraErrorsSuite)
|
|
suite.Run(t, s)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) SetupSuite() {
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TearDownSuite() {
|
|
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) SetupTest() {
|
|
s.Assertions = require.New(s.T())
|
|
s.ProtoAssertions = protorequire.New(s.T())
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TearDownTest() {
|
|
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestSortErrors_Sorted() {
|
|
shardOwnershipLostErr := &p.ShardOwnershipLostError{}
|
|
currentWorkflowErr := &p.CurrentWorkflowConditionFailedError{}
|
|
workflowErr := &p.WorkflowConditionFailedError{}
|
|
genericErr := &p.ConditionFailedError{}
|
|
randomErr := errors.New("random error")
|
|
|
|
expectedErrors := []error{
|
|
shardOwnershipLostErr,
|
|
currentWorkflowErr,
|
|
workflowErr,
|
|
genericErr,
|
|
randomErr,
|
|
}
|
|
|
|
errorsCaseSorted := []error{
|
|
shardOwnershipLostErr,
|
|
currentWorkflowErr,
|
|
workflowErr,
|
|
genericErr,
|
|
randomErr,
|
|
}
|
|
s.Equal(expectedErrors, sortErrors(errorsCaseSorted))
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestSortErrors_ReverseSorted() {
|
|
shardOwnershipLostErr := &p.ShardOwnershipLostError{}
|
|
currentWorkflowErr := &p.CurrentWorkflowConditionFailedError{}
|
|
workflowErr := &p.WorkflowConditionFailedError{}
|
|
genericErr := &p.ConditionFailedError{}
|
|
randomErr := errors.New("random error")
|
|
|
|
expectedErrors := []error{
|
|
shardOwnershipLostErr,
|
|
currentWorkflowErr,
|
|
workflowErr,
|
|
genericErr,
|
|
randomErr,
|
|
}
|
|
|
|
errorsCaseReverseSorted := []error{
|
|
randomErr,
|
|
genericErr,
|
|
workflowErr,
|
|
currentWorkflowErr,
|
|
shardOwnershipLostErr,
|
|
}
|
|
s.Equal(expectedErrors, sortErrors(errorsCaseReverseSorted))
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestSortErrors_Random() {
|
|
shardOwnershipLostErr := &p.ShardOwnershipLostError{}
|
|
currentWorkflowErr := &p.CurrentWorkflowConditionFailedError{}
|
|
workflowErr := &p.WorkflowConditionFailedError{}
|
|
genericErr := &p.ConditionFailedError{}
|
|
randomErr := errors.New("random error")
|
|
|
|
expectedErrors := []error{
|
|
shardOwnershipLostErr,
|
|
currentWorkflowErr,
|
|
workflowErr,
|
|
genericErr,
|
|
randomErr,
|
|
}
|
|
|
|
errorsCaseShuffled := []error{
|
|
randomErr,
|
|
genericErr,
|
|
workflowErr,
|
|
currentWorkflowErr,
|
|
shardOwnershipLostErr,
|
|
}
|
|
rand.Shuffle(len(errorsCaseShuffled), func(i int, j int) {
|
|
errorsCaseShuffled[i], errorsCaseShuffled[j] = errorsCaseShuffled[j], errorsCaseShuffled[i]
|
|
})
|
|
s.Equal(expectedErrors, sortErrors(errorsCaseShuffled))
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestSortErrors_One() {
|
|
shardOwnershipLostErr := &p.ShardOwnershipLostError{}
|
|
currentWorkflowErr := &p.CurrentWorkflowConditionFailedError{}
|
|
workflowErr := &p.WorkflowConditionFailedError{}
|
|
genericErr := &p.ConditionFailedError{}
|
|
randomErr := errors.New("random error")
|
|
|
|
s.Equal([]error{shardOwnershipLostErr}, sortErrors([]error{shardOwnershipLostErr}))
|
|
s.Equal([]error{currentWorkflowErr}, sortErrors([]error{currentWorkflowErr}))
|
|
s.Equal([]error{workflowErr}, sortErrors([]error{workflowErr}))
|
|
s.Equal([]error{genericErr}, sortErrors([]error{genericErr}))
|
|
s.Equal([]error{randomErr}, sortErrors([]error{randomErr}))
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractShardOwnershipLostError_Failed() {
|
|
rangeID := int64(1234)
|
|
|
|
err := extractShardOwnershipLostError(map[string]any{}, rand.Int31(), rangeID)
|
|
s.NoError(err)
|
|
|
|
t := rowTypeExecution
|
|
err = extractShardOwnershipLostError(map[string]any{
|
|
"type": &t,
|
|
"range_id": rangeID,
|
|
}, rand.Int31(), rangeID)
|
|
s.NoError(err)
|
|
|
|
t = rowTypeShard
|
|
err = extractShardOwnershipLostError(map[string]any{
|
|
"type": &t,
|
|
"range_id": rangeID,
|
|
}, rand.Int31(), rangeID)
|
|
s.NoError(err)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractShardOwnershipLostError_Success() {
|
|
rangeID := int64(1234)
|
|
t := rowTypeShard
|
|
record := map[string]any{
|
|
"type": &t,
|
|
"range_id": rangeID,
|
|
}
|
|
|
|
err := extractShardOwnershipLostError(record, rand.Int31(), rangeID+1)
|
|
s.IsType(&p.ShardOwnershipLostError{}, err)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractCurrentWorkflowConflictError_Failed() {
|
|
runID, _ := uuid.Parse(permanentRunID)
|
|
currentRunID := uuid.New()
|
|
|
|
err := extractCurrentWorkflowConflictError(map[string]any{}, permanentRunID, uuid.New().String())
|
|
s.NoError(err)
|
|
|
|
t := rowTypeShard
|
|
err = extractCurrentWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"current_run_id": gocql.UUID(currentRunID),
|
|
}, permanentRunID, uuid.New().String())
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractCurrentWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID([16]byte{}),
|
|
"current_run_id": gocql.UUID(currentRunID),
|
|
}, permanentRunID, uuid.New().String())
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractCurrentWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"current_run_id": gocql.UUID(currentRunID),
|
|
}, permanentRunID, currentRunID.String())
|
|
s.NoError(err)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractCurrentWorkflowConflictError_Success() {
|
|
requestID := uuid.New()
|
|
runID, _ := uuid.Parse(permanentRunID)
|
|
currentRunID := uuid.New()
|
|
startTime := time.Now().UTC()
|
|
workflowState := &persistencespb.WorkflowExecutionState{
|
|
CreateRequestId: requestID.String(),
|
|
RunId: currentRunID.String(),
|
|
State: enumsspb.WORKFLOW_EXECUTION_STATE_RUNNING,
|
|
Status: enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING,
|
|
StartTime: timestamp.TimePtr(startTime),
|
|
RequestIds: map[string]*persistencespb.RequestIDInfo{
|
|
requestID.String(): {
|
|
EventType: enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED,
|
|
EventId: common.FirstEventID,
|
|
},
|
|
uuid.NewString(): {
|
|
EventType: enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_OPTIONS_UPDATED,
|
|
EventId: common.BufferedEventID,
|
|
},
|
|
},
|
|
}
|
|
blob, err := serialization.NewSerializer().WorkflowExecutionStateToBlob(workflowState)
|
|
lastWriteVersion := rand.Int63()
|
|
s.NoError(err)
|
|
t := rowTypeExecution
|
|
record := map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"current_run_id": gocql.UUID(currentRunID),
|
|
"execution_state": blob.Data,
|
|
"execution_state_encoding": blob.EncodingType.String(),
|
|
"workflow_last_write_version": lastWriteVersion,
|
|
}
|
|
|
|
err = extractCurrentWorkflowConflictError(record, permanentRunID, uuid.New().String())
|
|
if err, ok := err.(*p.CurrentWorkflowConditionFailedError); ok {
|
|
err.Msg = ""
|
|
}
|
|
s.DeepEqual(
|
|
&p.CurrentWorkflowConditionFailedError{
|
|
Msg: "",
|
|
RequestIDs: workflowState.RequestIds,
|
|
RunID: workflowState.RunId,
|
|
State: workflowState.State,
|
|
Status: workflowState.Status,
|
|
LastWriteVersion: lastWriteVersion,
|
|
StartTime: &startTime,
|
|
},
|
|
err,
|
|
)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractWorkflowConflictError_Failed() {
|
|
runID := uuid.New()
|
|
dbVersion := rand.Int63() + 1
|
|
|
|
err := extractWorkflowConflictError(map[string]any{}, runID.String(), dbVersion, rand.Int63())
|
|
s.NoError(err)
|
|
|
|
t := rowTypeShard
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"db_record_version": dbVersion,
|
|
}, runID.String(), dbVersion+1, rand.Int63())
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID([16]byte{}),
|
|
"db_record_version": dbVersion,
|
|
}, runID.String(), dbVersion+1, rand.Int63())
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"db_record_version": dbVersion,
|
|
}, runID.String(), dbVersion, rand.Int63())
|
|
s.NoError(err)
|
|
}
|
|
|
|
func (s *cassandraErrorsSuite) TestExtractWorkflowConflictError_Success() {
|
|
runID := uuid.New()
|
|
dbVersion := rand.Int63() + 1
|
|
t := rowTypeExecution
|
|
record := map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"db_record_version": dbVersion,
|
|
}
|
|
|
|
err := extractWorkflowConflictError(record, runID.String(), dbVersion+1, rand.Int63())
|
|
s.IsType(&p.WorkflowConditionFailedError{}, err)
|
|
}
|
|
|
|
// TODO remove this block once DB version comparison is the default
|
|
func (s *cassandraErrorsSuite) TestExtractWorkflowConflictError_Failed_NextEventID() {
|
|
runID := uuid.New()
|
|
nextEventID := rand.Int63()
|
|
|
|
err := extractWorkflowConflictError(map[string]any{}, runID.String(), 0, nextEventID)
|
|
s.NoError(err)
|
|
|
|
t := rowTypeShard
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"next_event_id": nextEventID + 1,
|
|
}, runID.String(), 0, nextEventID)
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID([16]byte{}),
|
|
"next_event_id": nextEventID + 1,
|
|
}, runID.String(), 0, nextEventID)
|
|
s.NoError(err)
|
|
|
|
t = rowTypeExecution
|
|
err = extractWorkflowConflictError(map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"next_event_id": nextEventID,
|
|
}, runID.String(), 0, nextEventID)
|
|
s.NoError(err)
|
|
}
|
|
|
|
// TODO remove this block once DB version comparison is the default
|
|
func (s *cassandraErrorsSuite) TestExtractWorkflowConflictError_Success_NextEventID() {
|
|
runID := uuid.New()
|
|
nextEventID := int64(1234)
|
|
t := rowTypeExecution
|
|
record := map[string]any{
|
|
"type": &t,
|
|
"run_id": gocql.UUID(runID),
|
|
"next_event_id": nextEventID,
|
|
}
|
|
|
|
err := extractWorkflowConflictError(record, runID.String(), 0, nextEventID+1)
|
|
s.IsType(&p.WorkflowConditionFailedError{}, err)
|
|
}
|