-
Notifications
You must be signed in to change notification settings - Fork 8
/
delay_queue_test.go
144 lines (130 loc) · 4.19 KB
/
delay_queue_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package kreconciler
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sort"
"sync"
"testing"
"time"
)
type entry struct {
t time.Time
v string
}
type rcv struct {
sync.Mutex
items []entry
}
func (r *rcv) OnItem(t time.Time, i interface{}) {
r.Lock()
defer r.Unlock()
r.items = append(r.items, entry{t: t, v: i.(string)})
}
func assertRcv(t *testing.T, r *rcv, elts ...string) {
r.Lock()
defer r.Unlock()
var items []string
var times []time.Time
for _, v := range r.items {
items = append(items, v.v)
times = append(times, v.t)
}
assert.Equal(t, elts, items)
assert.True(t, sort.SliceIsSorted(times, func(i, j int) bool {
return times[i].Before(times[j])
}), "Items didn't trigger in good order")
}
func TestDelay(t *testing.T) {
t.Parallel()
t.Run("delay 0", func(t *testing.T) {
dq := newQueue(10, 10*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
require.NoError(t, dq.schedule("1", 0))
time.Sleep(time.Millisecond * 20)
cancel()
assertRcv(t, r, "1")
})
t.Run("delay long then short", func(t *testing.T) {
dq := newQueue(10, 10*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
require.NoError(t, dq.schedule("1", time.Millisecond*50))
require.NoError(t, dq.schedule("2", time.Millisecond*20))
time.Sleep(time.Millisecond * 100)
cancel()
assertRcv(t, r, "2", "1")
})
t.Run("delay same order", func(t *testing.T) {
dq := newQueue(10, 10*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
require.NoError(t, dq.schedule("1", time.Millisecond*20))
require.NoError(t, dq.schedule("2", time.Millisecond*40))
time.Sleep(time.Millisecond * 100)
cancel()
assertRcv(t, r, "1", "2")
})
t.Run("delay goes to empty and grows again", func(t *testing.T) {
dq := newQueue(2, 10*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
require.NoError(t, dq.schedule("1", time.Millisecond*20))
require.NoError(t, dq.schedule("2", time.Millisecond*40))
time.Sleep(time.Millisecond * 100)
assertRcv(t, r, "1", "2")
require.NoError(t, dq.schedule("3", time.Millisecond*20))
require.NoError(t, dq.schedule("4", time.Millisecond*40))
time.Sleep(time.Millisecond * 100)
assertRcv(t, r, "1", "2", "3", "4")
cancel()
})
t.Run("delay resolution is wrapped", func(t *testing.T) {
dq := newQueue(10, 50*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
now := time.Now().Truncate(50 * time.Millisecond)
require.NoError(t, dq.scheduleOnTime("1", now.Add(time.Millisecond*75)))
require.NoError(t, dq.scheduleOnTime("2", now.Add(time.Millisecond*60)))
time.Sleep(time.Millisecond * 200)
assertRcv(t, r, "1", "2")
cancel()
assert.Equal(t, r.items[0].t.String(), r.items[1].t.String())
})
t.Run("delay resolution not wrapped", func(t *testing.T) {
dq := newQueue(10, 50*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
now := time.Now().Truncate(50 * time.Millisecond)
require.NoError(t, dq.scheduleOnTime("1", now.Add(time.Millisecond*130)))
require.NoError(t, dq.scheduleOnTime("2", now.Add(time.Millisecond*80)))
time.Sleep(time.Millisecond * 300)
assertRcv(t, r, "2", "1")
cancel()
assert.NotEqual(t, r.items[0].t.String(), r.items[1].t.String())
})
t.Run("if queue is full errpr", func(t *testing.T) {
dq := newQueue(2, 10*time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
r := &rcv{}
go dq.run(ctx, r.OnItem)
require.NoError(t, dq.schedule("1", time.Millisecond*20))
require.NoError(t, dq.schedule("2", time.Millisecond*40))
// Extra item can't be enqueued
require.Error(t, dq.schedule("3", time.Millisecond*40))
time.Sleep(time.Millisecond * 100)
assertRcv(t, r, "1", "2")
require.NoError(t, dq.schedule("3", time.Millisecond*20))
require.NoError(t, dq.schedule("4", time.Millisecond*40))
time.Sleep(time.Millisecond * 100)
assertRcv(t, r, "1", "2", "3", "4")
cancel()
})
}