-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
queue_test.go
131 lines (101 loc) · 2.58 KB
/
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
package centrifuge
import (
"sync"
"testing"
"time"
)
func assertTrue(t *testing.T, condition bool, msg string) {
if !condition {
t.Fatalf("Assertion failed: %s", msg)
}
}
func assertEqual(t *testing.T, expected, actual interface{}, msg string) {
if expected != actual {
t.Fatalf("Assertion failed: %s - expected: %v, got: %v", msg, expected, actual)
}
}
func newTestQueue() *cbQueue {
q := &cbQueue{
closeCh: make(chan struct{}),
}
q.cond = sync.NewCond(&q.mu)
return q
}
func TestCbQueue_PushAndDispatch(t *testing.T) {
q := newTestQueue()
var wg sync.WaitGroup
wg.Add(1)
// Start the dispatcher in a separate goroutine.
go q.dispatch()
startTime := time.Now()
q.push(func(d time.Duration) {
defer wg.Done()
assertTrue(t, d >= 0, "Callback duration should be positive")
})
// Wait for the callback to finish.
wg.Wait()
// Ensure the callback executed quickly.
elapsed := time.Since(startTime)
assertTrue(t, elapsed < 100*time.Millisecond, "Callback should be dispatched immediately")
}
func TestCbQueue_OrderPreservation(t *testing.T) {
q := newTestQueue()
// Start the dispatcher in a separate goroutine.
go q.dispatch()
var results []int
var mu sync.Mutex
expectedResults := []int{1, 2, 3}
for _, i := range expectedResults {
i := i
q.push(func(d time.Duration) {
mu.Lock()
defer mu.Unlock()
results = append(results, i)
})
}
// Allow time for the queue to process.
time.Sleep(100 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
for i, r := range results {
assertEqual(t, expectedResults[i], r, "unexpected result")
}
}
func TestCbQueue_Close(t *testing.T) {
q := newTestQueue()
go q.dispatch()
var executed bool
q.push(func(d time.Duration) {
executed = true
})
q.close()
// Ensure the closeCh channel is closed.
select {
case <-q.closeCh:
// Channel was closed as expected.
case <-time.After(1 * time.Second):
t.Fatal("closeCh was not closed after queue close")
}
assertTrue(t, executed, "Callback should be executed before close")
}
func TestCbQueue_IgnorePushAfterClose(t *testing.T) {
q := newTestQueue()
go q.dispatch()
q.close()
var executed bool
q.push(func(d time.Duration) {
executed = true
})
// Allow some time to see if the callback is executed.
time.Sleep(100 * time.Millisecond)
assertTrue(t, !executed, "Callback should not be executed after queue close")
}
func TestCbQueue_PushNilCallbackPanics(t *testing.T) {
q := newTestQueue()
defer func() {
if r := recover(); r == nil {
t.Fatal("Expected panic when pushing nil callback with close set to false")
}
}()
q.pushOrClose(nil, false)
}