forked from gree/futurama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched_add_test.go
200 lines (173 loc) · 5.47 KB
/
sched_add_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package futurama
import (
"github.com/stretchr/testify/assert"
"math/rand"
"sync"
"testing"
"time"
)
func TestScheduler_Add_Normal(t *testing.T) {
cfg := DefaultConfig()
TestOnly_ResetDb(&cfg.MySQLConfig)
q, testChan := SetupQueue(cfg)
defer q.Stop()
assert := assert.New(t)
triggerTime := time.Now().Add(2 * time.Second)
evId := q.Create(Test_TriggerType_Default, triggerTime, "")
select {
case id := <-testChan:
assert.Equal(id, evId)
assert.WithinDuration(time.Now(), triggerTime, 150*time.Millisecond)
case <-time.After(time.Millisecond * 2100):
assert.Fail("event is not triggered")
}
time.Sleep(500 * time.Millisecond)
assert.Len(TestOnly_SelectEvents(&cfg.MySQLConfig), 0)
stat := q.GetStat()
assert.EqualValues(stat["futurama.MySQLStore.nbComplete"], 1)
assert.EqualValues(stat["futurama.MySQLStore.nbSave"], 1)
assert.EqualValues(stat["futurama.Scheduler.nbTriggered"], 1)
}
func TestScheduler_Add_UnknownTrigger(t *testing.T) {
cfg := DefaultConfig()
TestOnly_ResetDb(&cfg.MySQLConfig)
q, testChan := SetupQueue(cfg)
defer q.Stop()
assert := assert.New(t)
triggerTime := time.Now().Add(2 * time.Second)
q.Create("_unknown_trigger_type_", triggerTime, "")
select {
case <-testChan:
assert.Fail("No events should be triggered")
case <-time.After(time.Millisecond * 2100):
}
time.Sleep(500 * time.Millisecond)
assert.Len(TestOnly_SelectEvents(&cfg.MySQLConfig), 0)
stat := q.GetStat()
assert.EqualValues(stat["futurama.MySQLStore.nbComplete"], 1)
assert.EqualValues(stat["futurama.MySQLStore.nbSave"], 1)
assert.EqualValues(stat["futurama.Scheduler.nbTriggered"], 1)
}
func TestScheduler_Add_EventLockTimeout(t *testing.T) {
cfg := DefaultConfig()
cfg.ConsumerLockTimeoutSec = 5
TestOnly_ResetDb(&cfg.MySQLConfig)
assert := assert.New(t)
before := time.Now()
evId1 := func() string {
cfg.ConsumerName = "q1"
q, _ := SetupQueue(cfg)
defer q.Stop()
triggerTime := before.Add(2 * time.Second)
evId := q.Create(Test_TriggerType_Default, triggerTime, "")
time.Sleep(500 * time.Millisecond)
events := TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(events, 1)
assert.Equal(events[0].Id, evId)
return evId
}()
cfg.ConsumerName = "q2"
q, testChan := SetupQueue(cfg)
defer q.Stop()
triggerTime := before.Add(3 * time.Second)
evId := q.Create(Test_TriggerType_Default, triggerTime, "")
doneChan := make(chan bool)
go func() {
id2 := <-testChan
assert.Equal(id2, evId)
assert.WithinDuration(time.Now(), triggerTime, 150*time.Millisecond)
id1 := <-testChan
assert.Equal(id1, evId1)
assert.True(time.Now().After(before.Add(time.Duration(cfg.ConsumerLockTimeoutSec) * time.Second)))
close(doneChan)
}()
select {
case <-doneChan:
time.Sleep(500 * time.Millisecond)
assert.Len(TestOnly_SelectEvents(&cfg.MySQLConfig), 0)
case <-time.After(time.Duration(cfg.ConsumerLockTimeoutSec+2) * time.Second):
assert.Fail("timeout")
}
stat := q.GetStat()
assert.EqualValues(stat["futurama.MySQLStore.nbComplete"], 2)
assert.EqualValues(stat["futurama.MySQLStore.nbSave"], 1)
assert.EqualValues(stat["futurama.Scheduler.nbTriggered"], 2)
assert.EqualValues(stat["futurama.Scheduler.nbDelayed"], 1)
}
func TestScheduler_Add_MultiProducers(t *testing.T) {
rand.Seed(time.Now().UnixNano())
cfg := DefaultConfig()
TestOnly_ResetDb(&cfg.MySQLConfig)
q, testChan := SetupQueue(cfg)
defer q.Stop()
assert := assert.New(t)
groupSize := 8
nbGroup := 15
total := groupSize * nbGroup
var evListMutex sync.Mutex
evList := make(map[string]time.Time)
before := time.Now()
for i := 1; i <= nbGroup; i++ {
go func(groupId int) {
for j := 0; j < groupSize; j++ {
r := rand.Intn(119)
triggerTime := before.Add(time.Duration(groupId)*time.Second +
time.Duration(j*r)*time.Millisecond)
evId := q.Create(Test_TriggerType_Default, triggerTime, "")
evListMutex.Lock()
evList[evId] = triggerTime
evListMutex.Unlock()
time.Sleep(100 * time.Millisecond)
}
}(i)
}
doneChan := make(chan bool)
go func() {
for id := range testChan {
triggerTime, ok := evList[id]
assert.True(ok)
if ok {
assert.WithinDuration(time.Now(), triggerTime, 150*time.Millisecond)
}
total--
if total == 0 {
close(doneChan)
return
}
}
}()
select {
case <-doneChan:
case <-time.After(time.Duration(nbGroup)*time.Second + 3500*time.Millisecond):
assert.Fail("timeout")
}
assert.Len(TestOnly_SelectEvents(&cfg.MySQLConfig), 0)
stat := q.GetStat()
assert.EqualValues(stat["futurama.MySQLStore.nbError"], 0)
assert.EqualValues(stat["futurama.Scheduler.nbDelayed"], 0)
}
func TestScheduler_Add_Panic(t *testing.T) {
cfg := DefaultConfig()
TestOnly_ResetDb(&cfg.MySQLConfig)
q, testChan := SetupQueue(cfg)
defer q.Stop()
assert := assert.New(t)
q.Create(Test_TriggerType_Panic, time.Now().Add(time.Second), "")
time.Sleep(2 * time.Second)
triggerTime := time.Now().Add(2 * time.Second)
evId := q.Create(Test_TriggerType_Default, triggerTime, "")
select {
case id := <-testChan:
assert.Equal(id, evId)
assert.WithinDuration(time.Now(), triggerTime, 150*time.Millisecond)
case <-time.After(time.Millisecond * 2100):
assert.Fail("event is not triggered")
}
time.Sleep(500 * time.Millisecond)
assert.Len(TestOnly_SelectEvents(&cfg.MySQLConfig), 0)
stat := q.GetStat()
assert.EqualValues(stat["futurama.MySQLStore.nbComplete"], 2)
assert.EqualValues(stat["futurama.MySQLStore.nbSave"], 2)
assert.EqualValues(stat["futurama.Scheduler.nbTriggered"], 1)
assert.EqualValues(stat["futurama.Scheduler.nbRecovered"], 1)
}