-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelayoverlapping_test.go
84 lines (69 loc) · 1.58 KB
/
delayoverlapping_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package delayoverlapping
import (
"context"
"sync"
"testing"
"time"
"github.com/flc1125/go-cron/crontest/v4/logger"
"github.com/flc1125/go-cron/v4"
"github.com/stretchr/testify/assert"
)
var (
ctx = context.Background()
buf = logger.NewBuffer()
wg = sync.WaitGroup{}
)
func TestDelayOverlapping(t *testing.T) {
buf.Reset()
var (
delayOverlapping = New(
WithLogger(logger.NewBufferLogger(buf)),
WithReminderTime(1*time.Millisecond),
)
ch = make(chan struct{}, 100)
job = delayOverlapping(cron.JobFunc(func(context.Context) error {
ch <- struct{}{}
time.Sleep(2 * time.Millisecond)
return nil
}))
)
starting := time.Now()
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, job.Run(ctx))
}()
}
wg.Wait()
assert.True(t, len(ch) == 100)
assert.Contains(t, buf.String(), "delay")
assert.Greater(t, time.Since(starting).Milliseconds(), int64(100))
}
func TestDelayOverlapping_Chain(t *testing.T) {
buf.Reset()
var (
delayOverlapping = New(
WithLogger(logger.NewBufferLogger(buf)),
WithReminderTime(1*time.Millisecond),
)
ch = make(chan struct{}, 100)
job = cron.Chain(delayOverlapping)(cron.JobFunc(func(context.Context) error {
ch <- struct{}{}
time.Sleep(2 * time.Millisecond)
return nil
}))
)
starting := time.Now()
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
assert.NoError(t, job.Run(ctx))
}()
}
wg.Wait()
assert.True(t, len(ch) == 100)
assert.Contains(t, buf.String(), "delay")
assert.Greater(t, time.Since(starting).Milliseconds(), int64(100))
}