Skip to content

Commit

Permalink
test(middleware): optimize test (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Oct 26, 2024
1 parent b3146d4 commit 43f1443
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion chain.go → middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions chain_test.go → middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,45 @@ 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)
}
})

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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 0 additions & 8 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 43f1443

Please sign in to comment.