-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscheduler_test.go
179 lines (148 loc) · 3.59 KB
/
scheduler_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
package storage
import (
"testing"
"time"
"github.com/ironsmile/nedomi/mock"
"github.com/ironsmile/nedomi/types"
)
const DELTA = 10 * time.Millisecond
var fooKey = keyFromString("foo")
func writeFunc(ch chan string, str string) types.ScheduledCallback {
return func(types.Logger) {
ch <- str
}
}
func keyFromString(s string) types.ObjectIDHash {
var h = types.ObjectIDHash{}
copy(h[:], s)
return h
}
func TestAddingEvent(t *testing.T) {
t.Parallel()
defer func() {
if v := recover(); v != nil {
t.Errorf("Panic: %s", v)
}
}()
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
expected := "bar"
ch := make(chan string)
mp.AddEvent(fooKey, writeFunc(ch, expected), time.Millisecond)
found := <-ch
if found != expected {
t.Error("Found key was different than the expected")
}
}
func TestMultipleSetsOnTheSameKey(t *testing.T) {
t.Parallel()
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
var bad, good = "bad", "good"
ch := make(chan string)
mp.AddEvent(fooKey, writeFunc(ch, bad), time.Second)
mp.AddEvent(fooKey, writeFunc(ch, good), 100*time.Millisecond)
got := waitAround(t, ch, 100*time.Millisecond)
if !t.Failed() {
return
}
if got != good {
t.Errorf("expected '%s' got '%s'", good, got)
}
select {
case got := <-ch:
t.Errorf("expected nothing got '%s'", got)
case <-time.After(1*time.Second + DELTA):
}
}
func waitAround(t *testing.T, ch chan string, around time.Duration) string {
var tooSoon = true
for {
select {
case got := <-ch:
if tooSoon {
t.Fatal("return too fast")
return ""
}
return got
case <-time.After(around - DELTA):
tooSoon = false
case <-time.After(around + DELTA):
t.Fatal("too slow")
return ""
}
}
}
func TestContainsMethod(t *testing.T) {
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
mp.AddEvent(fooKey, nil, 3*time.Second)
if mp.Contains(fooKey) == false {
t.Errorf("Contains: false negative with foo")
}
if mp.Contains(keyFromString("baba")) {
t.Errorf("Contains: false positive with baba")
}
}
func TestCleaningUpTheMap(t *testing.T) {
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
expected := "pesho"
ch := make(chan string)
mp.AddEvent(fooKey, writeFunc(ch, expected), 10*time.Millisecond)
mp.Cleanup()
select {
case got := <-ch:
t.Errorf("expected nothing got '%s'", got)
case <-time.After(1*time.Second + DELTA):
}
}
func TestPanics(t *testing.T) {
t.Parallel()
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
mp.AddEvent(fooKey, func(types.Logger) {
panic("bar")
}, time.Millisecond)
<-time.After(1*time.Millisecond + DELTA)
if mp.Contains(fooKey) {
t.Error("the panicing function has not expired")
}
}
func TestLogger(t *testing.T) {
t.Parallel()
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
mp.AddEvent(fooKey, func(l types.Logger) {
if l != logger {
t.Fatalf("Expected logger %+v got %+v", logger, l)
}
}, time.Millisecond)
<-time.After(1*time.Millisecond + DELTA)
if mp.Contains(fooKey) {
t.Error("the log checking function has not expired")
}
}
func TestChangeLogger(t *testing.T) {
t.Parallel()
logger := mock.NewLogger()
mp := NewScheduler(logger)
defer mp.Destroy()
logger2 := mock.NewLogger()
mp.AddEvent(fooKey, func(l types.Logger) {
if l != logger2 {
t.Fatalf("Expected logger %+v got %+v", logger, l)
}
}, 10*time.Millisecond)
mp.SetLogger(logger2)
<-time.After(10*time.Millisecond + DELTA)
if mp.Contains(fooKey) {
t.Error("the log checking function has not expired")
}
}