Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Sep 30, 2023
1 parent 2f0f9c0 commit 383c712
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 66 deletions.
12 changes: 6 additions & 6 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ func (e *executor) start() {

case <-e.schCtx.Done():
e.cancel()
c1 := make(chan struct{})
c2 := make(chan struct{})
waitForJobs := make(chan struct{})
waitForSingletons := make(chan struct{})
go func() {
wg.Wait()
close(c1)
waitForJobs <- struct{}{}
}()
go func() {
for _, sr := range e.singletonRunners {
<-sr.done
}
close(c2)
waitForSingletons <- struct{}{}
}()

var timedOut bool
Expand All @@ -101,9 +101,9 @@ func (e *executor) start() {
select {
case <-time.After(e.shutdownTimeout):
timedOut = true
case <-c1:
case <-waitForJobs:
count++
case <-c2:
case <-waitForSingletons:
count++
}
}
Expand Down
65 changes: 5 additions & 60 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,10 @@ import (
"github.com/stretchr/testify/require"
)

func TestScheduler_Cron_Start_Stop(t *testing.T) {
s, err := NewScheduler()
require.NoError(t, err)

jobRan := make(chan struct{})

id, err := s.NewJob(
CronJob(
"* * * * * *",
true,
Task{
Function: func() {
close(jobRan)
},
},
),
)
require.NoError(t, err)

s.Start()

select {
case <-jobRan:
case <-time.After(2 * time.Second):
t.Errorf("job failed to run")
}

lastRun, err := s.GetJobLastRun(id)
assert.NotZero(t, lastRun)
err = s.Stop()
assert.NoError(t, err)
}

func TestScheduler(t *testing.T) {
t.Parallel()

cronNoOptionsCh := make(chan struct{})
cronSingletonCh := make(chan struct{})
durationNoOptionsCh := make(chan struct{})

type testJob struct {
Expand Down Expand Up @@ -95,31 +61,7 @@ func TestScheduler(t *testing.T) {

1,
time.Millisecond * 1,
time.Second,
},
{
"cron - singleton mode",
[]testJob{
{
"cron",
cronSingletonCh,
CronJob(
"* * * * * *",
true,
Task{
Function: func() {
time.Sleep(2 * time.Second)
cronSingletonCh <- struct{}{}
},
},
SingletonMode(),
),
},
},
nil,
2,
time.Second * 2,
time.Second * 6,
time.Millisecond * 1500,
},
}

Expand Down Expand Up @@ -160,5 +102,8 @@ func TestScheduler(t *testing.T) {
})
}
}

}

// TODO tests for singleton mode and other limit mode
// need to handle case where some jobs are waiting for on shutdown
// so can't use a channel because then the job is blocked trying to send

0 comments on commit 383c712

Please sign in to comment.