-
Notifications
You must be signed in to change notification settings - Fork 0
/
slist_test.go
85 lines (72 loc) · 1.58 KB
/
slist_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
package gorqs
import (
"testing"
)
type fakeJobber struct{}
func (f *fakeJobber) Run() error {
return nil
}
func (f *fakeJobber) getID() int64 {
return -1
}
func TestSyncList_IsEmpty(t *testing.T) {
t.Run("check empty slist", func(t *testing.T) {
l := list()
yes := l.isEmpty()
if !yes {
t.Error("initialized slist should be empty")
}
})
t.Run("check non-empty slist", func(t *testing.T) {
l := list()
l.push(&fakeJobber{})
yes := l.isEmpty()
if yes {
t.Error("slist contains one item. should not be empty")
}
})
}
func TestSyncList_Push(t *testing.T) {
l := list()
count := 5
for c := 0; c < count; c++ {
l.push(&fakeJobber{})
}
got := l.count.Load()
if got != int64(count) {
t.Errorf("pushed %d jobs but slist contains %d jobs", count, got)
}
}
func TestSyncList_Pop(t *testing.T) {
t.Run("pop empty slist", func(t *testing.T) {
l := list()
got := l.pop()
if got != nil {
t.Errorf("pop initialized slist should return <nil>, but got %v", got)
return
}
if count := l.count.Load(); count != 0 {
t.Errorf("pop initialized slist have 0 item, but got %d", count)
}
})
t.Run("pop non-empty slist", func(t *testing.T) {
l := list()
jobA := &fakeJobber{}
jobB := &fakeJobber{}
l.push(jobA)
l.push(jobB)
got := l.pop()
if got != jobB {
t.Errorf("got %v but expected %v", got, jobA)
return
}
got = l.pop()
if got != jobA {
t.Errorf("got %v but expected %v", got, jobB)
return
}
if count := l.count.Load(); count != 0 {
t.Errorf("slist all items of slist. expect 0 remaining item, but got %d", count)
}
})
}