mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? - Visibility writes are added to the Scheduler components. ## How did you test it? - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [x] added new unit test(s) - [ ] added new functional test(s)
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package scheduler_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
schedulepb "go.temporal.io/api/schedule/v1"
|
|
workflowpb "go.temporal.io/api/workflow/v1"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/chasm"
|
|
"go.temporal.io/server/chasm/lib/scheduler"
|
|
"go.temporal.io/server/common/backoff"
|
|
"go.temporal.io/server/common/clock"
|
|
"go.temporal.io/server/common/testing/testlogger"
|
|
"go.temporal.io/server/common/testing/testvars"
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
|
)
|
|
|
|
const (
|
|
namespace = "ns"
|
|
namespaceID = "ns-id"
|
|
scheduleID = "sched-id"
|
|
|
|
defaultInterval = 1 * time.Minute
|
|
defaultCatchupWindow = 5 * time.Minute
|
|
)
|
|
|
|
// defaultSchedule returns a protobuf definition for a schedule matching this
|
|
// package's other testing defaults.
|
|
func defaultSchedule() *schedulepb.Schedule {
|
|
return &schedulepb.Schedule{
|
|
Spec: &schedulepb.ScheduleSpec{
|
|
Interval: []*schedulepb.IntervalSpec{
|
|
{
|
|
Interval: durationpb.New(defaultInterval),
|
|
Phase: durationpb.New(0),
|
|
},
|
|
},
|
|
},
|
|
Action: &schedulepb.ScheduleAction{
|
|
Action: &schedulepb.ScheduleAction_StartWorkflow{
|
|
StartWorkflow: &workflowpb.NewWorkflowExecutionInfo{
|
|
WorkflowId: "scheduled-wf",
|
|
WorkflowType: &commonpb.WorkflowType{Name: "scheduled-wf-type"},
|
|
},
|
|
},
|
|
},
|
|
Policies: &schedulepb.SchedulePolicies{
|
|
CatchupWindow: durationpb.New(defaultCatchupWindow),
|
|
},
|
|
State: &schedulepb.ScheduleState{
|
|
Paused: false,
|
|
LimitedActions: false,
|
|
RemainingActions: 0,
|
|
},
|
|
}
|
|
}
|
|
|
|
func defaultConfig() *scheduler.Config {
|
|
return &scheduler.Config{
|
|
Tweakables: func(_ string) scheduler.Tweakables {
|
|
return scheduler.DefaultTweakables
|
|
},
|
|
ServiceCallTimeout: func() time.Duration {
|
|
return 5 * time.Second
|
|
},
|
|
RetryPolicy: func() backoff.RetryPolicy {
|
|
return backoff.NewExponentialRetryPolicy(1 * time.Second)
|
|
},
|
|
}
|
|
}
|
|
|
|
func setupSchedulerForTest(t *testing.T) (*scheduler.Scheduler, chasm.MutableContext, *chasm.Node) {
|
|
nodeBackend := &chasm.MockNodeBackend{}
|
|
logger := testlogger.NewTestLogger(t, testlogger.FailOnExpectedErrorOnly)
|
|
nodePathEncoder := chasm.DefaultPathEncoder
|
|
|
|
registry := chasm.NewRegistry(logger)
|
|
err := registry.Register(&chasm.CoreLibrary{})
|
|
if err != nil {
|
|
t.Fatalf("failed to register core library: %v", err)
|
|
}
|
|
err = registry.Register(&scheduler.Library{})
|
|
if err != nil {
|
|
t.Fatalf("failed to register scheduler library: %v", err)
|
|
}
|
|
|
|
timeSource := clock.NewEventTimeSource()
|
|
timeSource.Update(time.Now())
|
|
|
|
tv := testvars.New(t)
|
|
nodeBackend.HandleNextTransitionCount = func() int64 { return 2 }
|
|
nodeBackend.HandleGetCurrentVersion = func() int64 { return 1 }
|
|
nodeBackend.HandleGetWorkflowKey = tv.Any().WorkflowKey
|
|
nodeBackend.HandleIsWorkflow = func() bool { return false }
|
|
nodeBackend.HandleCurrentVersionedTransition = func() *persistencespb.VersionedTransition {
|
|
return &persistencespb.VersionedTransition{
|
|
NamespaceFailoverVersion: 1,
|
|
TransitionCount: 1,
|
|
}
|
|
}
|
|
|
|
node := chasm.NewEmptyTree(registry, timeSource, nodeBackend, nodePathEncoder, logger)
|
|
ctx := chasm.NewMutableContext(context.Background(), node)
|
|
sched := scheduler.NewScheduler(ctx, namespace, namespaceID, scheduleID, defaultSchedule(), nil)
|
|
node.SetRootComponent(sched)
|
|
_, err = node.CloseTransaction()
|
|
if err != nil {
|
|
t.Fatalf("failed to close initial transaction: %v", err)
|
|
}
|
|
|
|
ctx = chasm.NewMutableContext(context.Background(), node)
|
|
|
|
return sched, ctx, node
|
|
}
|