-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
easer_test.go
102 lines (86 loc) · 2.15 KB
/
easer_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
package caches
import (
"sync"
"testing"
"time"
)
func TestEase(t *testing.T) {
t.Run("same queries", func(t *testing.T) {
queue := &sync.Map{}
myTask := &mockTask{
delay: 1 * time.Second,
expRes: "expect-this",
id: "unique-id",
}
myDupTask := &mockTask{
delay: 1 * time.Second,
expRes: "not-this",
id: "unique-id",
}
wg := &sync.WaitGroup{}
wg.Add(2)
var (
myTaskRes *mockTask
myDupTaskRes *mockTask
)
// Both queries will run at the same time, the second one will run half a second later
go func() {
myTaskRes = ease(myTask, queue).(*mockTask)
wg.Done()
}()
go func() {
time.Sleep(500 * time.Millisecond)
myDupTaskRes = ease(myDupTask, queue).(*mockTask)
wg.Done()
}()
wg.Wait()
if myTaskRes.actRes != myTaskRes.expRes {
t.Error("expected first query to be executed")
}
if myTaskRes.actRes != myDupTaskRes.actRes {
t.Errorf("expected same result from both tasks, expected: %s, actual: %s",
myTaskRes.actRes, myDupTaskRes.actRes)
}
})
t.Run("different queries", func(t *testing.T) {
queue := &sync.Map{}
myTask := &mockTask{
delay: 1 * time.Second,
expRes: "expect-this",
id: "unique-id",
}
myDupTask := &mockTask{
delay: 1 * time.Second,
expRes: "not-this",
id: "other-unique-id",
}
wg := &sync.WaitGroup{}
wg.Add(2)
var (
myTaskRes *mockTask
myDupTaskRes *mockTask
)
// Both queries will run at the same time, the second one will run half a second later
go func() {
myTaskRes = ease(myTask, queue).(*mockTask)
wg.Done()
}()
go func() {
time.Sleep(500 * time.Millisecond)
myDupTaskRes = ease(myDupTask, queue).(*mockTask)
wg.Done()
}()
wg.Wait()
if myTaskRes.actRes != myTaskRes.expRes {
t.Errorf("expected first query to be executed, expected: %s, actual: %s",
myTaskRes.actRes, myTaskRes.expRes)
}
if myTaskRes.actRes == myDupTaskRes.actRes {
t.Errorf("expected different result from both tasks, expected: %s, actual: %s",
myTaskRes.actRes, myDupTaskRes.actRes)
}
if myDupTaskRes.actRes != myDupTaskRes.expRes {
t.Error("expected second query to be executed")
}
})
}