2023-02-27 16:44:26 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-02-27 16:44:26 +01:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2024-03-18 10:12:46 +01:00
|
|
|
"runtime"
|
2023-02-27 16:44:26 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2024-03-03 10:35:01 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-27 16:44:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScheduler_Performance(t *testing.T) {
|
|
|
|
scheduler := NewDefaultScheduler()
|
|
|
|
n := 500
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(n)
|
|
|
|
maxMs := 500
|
|
|
|
minMs := 50
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
millis := time.Duration(rand.Intn(maxMs-minMs)+minMs) * time.Millisecond
|
2024-07-03 11:33:02 +02:00
|
|
|
go scheduler.Schedule(context.Background(), millis, fmt.Sprintf("test-scheduler-job-%d", i), func() (nextRunIn time.Duration, reschedule bool) {
|
2023-02-27 16:44:26 +01:00
|
|
|
time.Sleep(millis)
|
|
|
|
wg.Done()
|
|
|
|
return 0, false
|
|
|
|
})
|
|
|
|
}
|
2024-03-18 10:12:46 +01:00
|
|
|
timeout := 3 * time.Second
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// sleep and ticker are slower on windows see https://github.com/golang/go/issues/44343
|
|
|
|
timeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
failed := waitTimeout(wg, timeout)
|
2023-02-27 16:44:26 +01:00
|
|
|
if failed {
|
|
|
|
t.Fatal("timed out while waiting for test to finish")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Len(t, scheduler.jobs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScheduler_Cancel(t *testing.T) {
|
|
|
|
jobID1 := "test-scheduler-job-1"
|
|
|
|
jobID2 := "test-scheduler-job-2"
|
|
|
|
scheduler := NewDefaultScheduler()
|
2024-03-03 10:35:01 +01:00
|
|
|
tChan := make(chan struct{})
|
|
|
|
p := []string{jobID1, jobID2}
|
2024-03-18 10:12:46 +01:00
|
|
|
scheduletime := 2 * time.Millisecond
|
|
|
|
sleepTime := 4 * time.Millisecond
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// sleep and ticker are slower on windows see https://github.com/golang/go/issues/44343
|
|
|
|
sleepTime = 20 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.Schedule(context.Background(), scheduletime, jobID1, func() (nextRunIn time.Duration, reschedule bool) {
|
2024-03-03 10:35:01 +01:00
|
|
|
tt := p[0]
|
|
|
|
<-tChan
|
|
|
|
t.Logf("job %s", tt)
|
2024-03-18 10:12:46 +01:00
|
|
|
return scheduletime, true
|
2023-02-27 16:44:26 +01:00
|
|
|
})
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.Schedule(context.Background(), scheduletime, jobID2, func() (nextRunIn time.Duration, reschedule bool) {
|
2024-03-18 10:12:46 +01:00
|
|
|
return scheduletime, true
|
2023-02-27 16:44:26 +01:00
|
|
|
})
|
|
|
|
|
2024-03-18 10:12:46 +01:00
|
|
|
time.Sleep(sleepTime)
|
2023-02-27 16:44:26 +01:00
|
|
|
assert.Len(t, scheduler.jobs, 2)
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.Cancel(context.Background(), []string{jobID1})
|
2024-03-03 10:35:01 +01:00
|
|
|
close(tChan)
|
|
|
|
p = []string{}
|
2024-03-18 10:12:46 +01:00
|
|
|
time.Sleep(sleepTime)
|
2023-02-27 16:44:26 +01:00
|
|
|
assert.Len(t, scheduler.jobs, 1)
|
|
|
|
assert.NotNil(t, scheduler.jobs[jobID2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScheduler_Schedule(t *testing.T) {
|
|
|
|
jobID := "test-scheduler-job-1"
|
|
|
|
scheduler := NewDefaultScheduler()
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
// job without reschedule should be triggered once
|
|
|
|
job := func() (nextRunIn time.Duration, reschedule bool) {
|
|
|
|
wg.Done()
|
|
|
|
return 0, false
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.Schedule(context.Background(), 300*time.Millisecond, jobID, job)
|
2023-02-27 16:44:26 +01:00
|
|
|
failed := waitTimeout(wg, time.Second)
|
|
|
|
if failed {
|
|
|
|
t.Fatal("timed out while waiting for test to finish")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// job with reschedule should be triggered at least twice
|
|
|
|
wg = &sync.WaitGroup{}
|
|
|
|
mx := &sync.Mutex{}
|
|
|
|
scheduledTimes := 0
|
|
|
|
wg.Add(2)
|
|
|
|
job = func() (nextRunIn time.Duration, reschedule bool) {
|
|
|
|
mx.Lock()
|
|
|
|
defer mx.Unlock()
|
|
|
|
// ensure we repeat only twice
|
|
|
|
if scheduledTimes < 2 {
|
|
|
|
wg.Done()
|
|
|
|
scheduledTimes++
|
|
|
|
return 300 * time.Millisecond, true
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.Schedule(context.Background(), 300*time.Millisecond, jobID, job)
|
2023-02-27 16:44:26 +01:00
|
|
|
failed = waitTimeout(wg, time.Second)
|
|
|
|
if failed {
|
|
|
|
t.Fatal("timed out while waiting for test to finish")
|
|
|
|
return
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
scheduler.cancel(context.Background(), jobID)
|
2023-02-27 16:44:26 +01:00
|
|
|
|
|
|
|
}
|