forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunlimited_channel_test.go
117 lines (103 loc) · 2.58 KB
/
unlimited_channel_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
//go:build go1.18
package zk
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"testing"
"time"
)
func newEvent(i int) Event {
return Event{Path: fmt.Sprintf("/%d", i)}
}
func TestUnlimitedChannel(t *testing.T) {
names := []string{"notClosedAfterPushes", "closeAfterPushes"}
for i, closeAfterPushes := range []bool{false, true} {
t.Run(names[i], func(t *testing.T) {
ch := NewUnlimitedQueue[Event]()
const eventCount = 10
// check that elements can be pushed without consumers
for i := 0; i < eventCount; i++ {
ch.Push(newEvent(i))
}
if closeAfterPushes {
ch.Close()
}
for events := 0; events < eventCount; events++ {
actual, err := ch.Next(context.Background())
if err != nil {
t.Fatalf("Unexpected error returned from Next (events %d): %+v", events, err)
}
expected := newEvent(events)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Did not receive expected event from queue: actual %+v expected %+v", actual, expected)
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
t.Cleanup(cancel)
_, err := ch.Next(ctx)
if closeAfterPushes {
if err != ErrEventQueueClosed {
t.Fatalf("Did not receive expected error (%v) from Next: %v", ErrEventQueueClosed, err)
}
} else {
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Next did not exit with cancelled context: %+v", err)
}
}
})
}
t.Run("interleaving", func(t *testing.T) {
ch := NewUnlimitedQueue[Event]()
for i := 0; i < 10; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*10)
t.Cleanup(cancel)
expected := newEvent(i)
ctx = &customContext{
Context: ctx,
f: func() {
ch.Push(expected)
},
}
actual, err := ch.Next(ctx)
if err != nil {
t.Fatalf("Received unexpected error from Next: %+v", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("Unexpected event received from Next (expected %+v, actual %+v", expected, actual)
}
}
})
t.Run("multiple consumers", func(t *testing.T) {
ch := NewUnlimitedQueue[Event]()
for i := 0; i < 20; i++ {
ch.Push(newEvent(i))
}
ch.Close()
var wg sync.WaitGroup
wg.Add(20)
for i := 0; i < 5; i++ {
go func() {
for {
_, err := ch.Next(context.Background())
if errors.Is(err, ErrEventQueueClosed) {
return
}
requireNoErrorf(t, err)
wg.Done()
}
}()
}
wg.Wait()
})
}
type customContext struct {
context.Context
f func()
}
func (c *customContext) Done() <-chan struct{} {
c.f()
return c.Context.Done()
}