diff --git a/chain.go b/middleware.go similarity index 96% rename from chain.go rename to middleware.go index 3981af6..aad19da 100644 --- a/chain.go +++ b/middleware.go @@ -8,7 +8,7 @@ import ( "time" ) -// Middleware is a chainable behavior modifier for jobs. +// Middleware is a function that wraps a Job to provide additional functionality. type Middleware func(Job) Job // Chain is a helper function to compose Middlewares. It returns a Middleware that diff --git a/chain_test.go b/middleware_test.go similarity index 90% rename from chain_test.go rename to middleware_test.go index 4725dc5..242e01c 100644 --- a/chain_test.go +++ b/middleware_test.go @@ -113,12 +113,20 @@ func TestChainDelayIfStillRunning(t *testing.T) { t.Run("second run immediate if first done", func(t *testing.T) { var j countJob wrappedJob := Chain(DelayIfStillRunning(DiscardLogger))(&j) + wg := sync.WaitGroup{} + wg.Add(2) go func() { - go wrappedJob.Run(context.Background()) //nolint:errcheck + go func() { + defer wg.Done() + wrappedJob.Run(context.Background()) //nolint:errcheck + }() time.Sleep(time.Millisecond) - go wrappedJob.Run(context.Background()) //nolint:errcheck + go func() { + defer wg.Done() + wrappedJob.Run(context.Background()) //nolint:errcheck + }() }() - time.Sleep(3 * time.Millisecond) // Give both jobs 3ms to complete. + wg.Wait() if c := j.Done(); c != 2 { t.Errorf("expected job run twice, immediately, got %d", c) } @@ -126,24 +134,24 @@ func TestChainDelayIfStillRunning(t *testing.T) { t.Run("second run delayed if first not done", func(t *testing.T) { var j countJob - j.delay = 10 * time.Millisecond + j.delay = 100 * time.Millisecond wrappedJob := Chain(DelayIfStillRunning(DiscardLogger))(&j) go func() { go wrappedJob.Run(context.Background()) //nolint:errcheck - time.Sleep(time.Millisecond) + time.Sleep(10 * time.Millisecond) go wrappedJob.Run(context.Background()) //nolint:errcheck }() - // After 5ms, the first job is still in progress, and the second job was + // After 50ms, the first job is still in progress, and the second job was // run but should be waiting for it to finish. - time.Sleep(5 * time.Millisecond) + time.Sleep(50 * time.Millisecond) started, done := j.Started(), j.Done() if started != 1 || done != 0 { t.Error("expected first job started, but not finished, got", started, done) } // Verify that the second job completes. - time.Sleep(25 * time.Millisecond) + time.Sleep(250 * time.Millisecond) started, done = j.Started(), j.Done() if started != 2 || done != 2 { t.Error("expected both jobs done, got", started, done) @@ -156,7 +164,7 @@ func TestChainSkipIfStillRunning(t *testing.T) { var j countJob wrappedJob := Chain(SkipIfStillRunning(DiscardLogger))(&j) go wrappedJob.Run(context.Background()) //nolint:errcheck - time.Sleep(2 * time.Millisecond) // Give the job 2ms to complete. + time.Sleep(20 * time.Millisecond) // Give the job 2ms to complete. if c := j.Done(); c != 1 { t.Errorf("expected job run once, immediately, got %d", c) } @@ -167,10 +175,10 @@ func TestChainSkipIfStillRunning(t *testing.T) { wrappedJob := Chain(SkipIfStillRunning(DiscardLogger))(&j) go func() { go wrappedJob.Run(context.Background()) //nolint:errcheck - time.Sleep(time.Millisecond) + time.Sleep(10 * time.Millisecond) go wrappedJob.Run(context.Background()) //nolint:errcheck }() - time.Sleep(3 * time.Millisecond) // Give both jobs 3ms to complete. + time.Sleep(30 * time.Millisecond) // Give both jobs 3ms to complete. if c := j.Done(); c != 2 { t.Errorf("expected job run twice, immediately, got %d", c) } diff --git a/option.go b/option.go index 58ae75b..c6401ed 100644 --- a/option.go +++ b/option.go @@ -37,14 +37,6 @@ func WithParser(p ScheduleParser) Option { } } -// // WithChain specifies Job wrappers to apply to all jobs added to this cron. -// // Refer to the Chain* functions in this package for provided wrappers. -// func WithChain(wrappers ...JobWrapper) Option { -// return func(c *Cron) { -// c.chain = NewChain(wrappers...) -// } -// } - // WithMiddleware specifies Middleware to apply to all jobs added to this cron. func WithMiddleware(middlewares ...Middleware) Option { return func(c *Cron) {