Skip to content

Commit

Permalink
Scheduler Schedule test
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 17, 2024
1 parent 9c52d41 commit 6d16724
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (s *scheduler) Start(ctx context.Context) error {
s.setJobTimer(job.Name, timer)
_ = timer.Start(period, handler)
}()

}

return nil
Expand Down
70 changes: 70 additions & 0 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,76 @@ func TestScheduler_RegisterJobHandler(t *testing.T) {
checkJobHandlerRegistered(sch, jobName2)
}

func TestScheduler_Schedule(t *testing.T) {
type testCase struct {
storage func(context.Context, *gomock.Controller, clockwork.Clock, *testCase) Storage
existingJob *Job
jobName string
period time.Duration
expectedErr error
}

tests := map[string]testCase{
"OK: New job": {
storage: func(ctx context.Context, ctrl *gomock.Controller, clock clockwork.Clock, tt *testCase) Storage {
storage := NewMockStorage(ctrl)
storageSession := &dummySession{}
storage.EXPECT().NewSession(ctx).Return(storageSession, nil)
storage.EXPECT().GetJobByName(ctx, storageSession, tt.jobName).Return(nil, ErrNotFound)
storage.EXPECT().UpsertJob(ctx, storageSession, &Job{
Name: tt.jobName,
ExecuteAt: clock.Now().Add(tt.period),
Period: tt.period,
}).Return(nil)
storage.EXPECT().Commit(storageSession).Return(nil)
storage.EXPECT().Abort(storageSession)
return storage
},
jobName: "new_job",
period: 10 * time.Second,
},
"OK: Existing job": {
storage: func(ctx context.Context, ctrl *gomock.Controller, clock clockwork.Clock, tt *testCase) Storage {
storage := NewMockStorage(ctrl)
storageSession := &dummySession{}
storage.EXPECT().NewSession(ctx).Return(storageSession, nil)
storage.EXPECT().GetJobByName(ctx, storageSession, tt.jobName).Return(tt.existingJob, nil)
storage.EXPECT().UpsertJob(ctx, storageSession, &Job{
Name: tt.jobName,
ExecuteAt: tt.existingJob.ExecuteAt,
Period: tt.period,
}).Return(nil)
storage.EXPECT().Commit(storageSession).Return(nil)
storage.EXPECT().Abort(storageSession)
return storage
},
existingJob: &Job{
Name: "existing_job",
ExecuteAt: time.Now(),
Period: 10 * time.Second,
},
jobName: "existing_job",
period: 15 * time.Second,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
clock := clockwork.NewFakeClock()

sch := New(
zap.NewNop().Sugar(),
tt.storage(ctx, gomock.NewController(t), clock, &tt),
clock,
).(*scheduler)

err := sch.Schedule(ctx, tt.period, tt.jobName)
require.ErrorIs(t, err, tt.expectedErr)
})
}
}

type dummySession struct{}

func (d *dummySession) Commit() error {
Expand Down

0 comments on commit 6d16724

Please sign in to comment.