Skip to content

Commit

Permalink
Fix more flaky tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <[email protected]>
  • Loading branch information
artursouza committed Mar 11, 2024
1 parent 28fda86 commit f2b7548
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ func TestStopCausesJobsToNotRun(t *testing.T) {
// Add a job, start cron, expect it runs.
func TestAddBeforeRunning(t *testing.T) {
wg := &sync.WaitGroup{}
calledAlready := false
wg.Add(1)

cron, err := New(
WithNamespace(randomNamespace()),
WithTriggerFunc(func(ctx context.Context, s string, p *anypb.Any) error {
if calledAlready {
return nil
}

calledAlready = true
wg.Done()
return nil
}))
Expand All @@ -82,14 +88,14 @@ func TestAddBeforeRunning(t *testing.T) {
}
cron.AddJob(Job{
Name: "test-add-before-running",
Rhythm: "* * * * * ?",
Rhythm: "* * * * * *",
})
cron.Start(context.Background())
defer cron.Stop()

// Give cron 2 seconds to run our job (which is always activated).
select {
case <-time.After(ONE_SECOND):
case <-time.After(2 * ONE_SECOND):
t.FailNow()
case <-wait(wg):
}
Expand Down Expand Up @@ -251,7 +257,7 @@ func TestMultipleEntries(t *testing.T) {
defer cron.Stop()

select {
case <-time.After(ONE_SECOND):
case <-time.After(2 * ONE_SECOND):
t.FailNow()
case <-wait(wg):
}
Expand Down Expand Up @@ -382,11 +388,16 @@ func TestLocalTimezone(t *testing.T) {
// Simple test using Runnables.
func TestJob(t *testing.T) {
wg := &sync.WaitGroup{}
calledAlready := false
wg.Add(1)

cron, err := New(
WithNamespace(randomNamespace()),
WithTriggerFunc(func(ctx context.Context, s string, p *anypb.Any) error {
if calledAlready {
return nil
}
calledAlready = true
wg.Done()
return nil
}))
Expand Down Expand Up @@ -421,7 +432,7 @@ func TestJob(t *testing.T) {
defer cron.Stop()

select {
case <-time.After(ONE_SECOND):
case <-time.After(2 * ONE_SECOND):
t.FailNow()
case <-wait(wg):
}
Expand Down

0 comments on commit f2b7548

Please sign in to comment.