forked from optiopay/kafka
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultiplexer_test.go
151 lines (129 loc) · 3.09 KB
/
multiplexer_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
package kafka
import (
"errors"
"testing"
"time"
"github.com/optiopay/kafka/proto"
)
type fetcher struct {
messages []*proto.Message
errors []error
}
func (f *fetcher) Consume() (*proto.Message, error) {
// sleep a bit to let the other's work
time.Sleep(time.Microsecond * 500)
if len(f.messages) > 0 {
msg := f.messages[0]
f.messages = f.messages[1:]
return msg, nil
}
if len(f.errors) > 0 {
err := f.errors[0]
f.errors = f.errors[1:]
return nil, err
}
panic("not implemented")
}
func TestMultiplexerConsume(t *testing.T) {
fetchers := []Consumer{
&fetcher{
messages: []*proto.Message{
{Value: []byte("first")},
{Value: []byte("second")},
},
errors: []error{
errors.New("e first"),
errors.New("e second"),
errors.New("e third"),
},
},
&fetcher{
messages: []*proto.Message{
{Value: []byte("1")},
{Value: []byte("2")},
},
errors: []error{
errors.New("e 1"),
errors.New("e 2"),
errors.New("e 3"),
},
},
}
results := make(map[string]bool)
mx := Merge(fetchers...)
defer mx.Close()
for i := 0; i < 8; i++ {
msg, err := mx.Consume()
if err != nil {
results[err.Error()] = true
} else {
results[string(msg.Value)] = true
}
}
expected := []string{
"first", "second", "e first", "e second",
"1", "2", "e 1", "e 2",
}
// expected 4 messages and 2 errors
if len(results) != len(expected) {
t.Errorf("expected %d results, got %d", len(expected), len(results))
}
for _, name := range expected {
if results[name] != true {
t.Errorf("%q not found: %#v", name, results)
}
}
}
func TestClosingMultiplexer(t *testing.T) {
fetchers := []Consumer{
&fetcher{errors: []error{errors.New("a1")}},
&fetcher{errors: []error{errors.New("b1")}},
&fetcher{errors: []error{errors.New("c1")}},
}
mx := Merge(fetchers...)
// closing more than once should be fine
for i := 0; i < 4; i++ {
go mx.Close()
}
mx.Close()
mx.Close()
if _, err := mx.Consume(); err != ErrMxClosed {
t.Fatalf("expected %s, got %s", ErrMxClosed, err)
}
}
type blockingFetcher struct {
stop chan struct{}
}
func (f *blockingFetcher) Consume() (*proto.Message, error) {
<-f.stop
return nil, errors.New("blocking fetcher is done")
}
func (f *blockingFetcher) Close() {
close(f.stop)
}
func TestClosingMultiplexerWithBlockingWorkers(t *testing.T) {
f1 := &blockingFetcher{make(chan struct{})}
defer f1.Close()
f2 := &blockingFetcher{make(chan struct{})}
defer f2.Close()
mx := Merge(f1, f2)
// close should be instant - without waiting for workers to finish
mx.Close()
if _, err := mx.Consume(); err != ErrMxClosed {
t.Fatalf("expected %s, got %s", ErrMxClosed, err)
}
}
func TestErrNoDataCloseMultiplexer(t *testing.T) {
fetchers := []Consumer{
&fetcher{errors: []error{ErrNoData}},
&fetcher{errors: []error{ErrNoData}, messages: []*proto.Message{{}}},
&fetcher{errors: []error{ErrNoData}},
}
mx := Merge(fetchers...)
if _, err := mx.Consume(); err != nil {
t.Fatalf("first consume should succeed, got %s", err)
}
if _, err := mx.Consume(); err != ErrMxClosed {
t.Fatalf("expected %s, got %s", ErrMxClosed, err)
}
}