-
Notifications
You must be signed in to change notification settings - Fork 7
/
pump_test.go
220 lines (175 loc) · 5.73 KB
/
pump_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package streams_test
import (
"errors"
"testing"
"time"
"github.com/msales/streams/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestSyncPump_Accept(t *testing.T) {
mon := new(MockMonitor)
mon.On("Processed", mock.Anything, mock.Anything, mock.Anything).Return(nil)
msg := streams.NewMessage("test", "test")
processor := new(MockProcessor)
processor.On("Process", msg).Return(nil)
processor.On("Close").Maybe().Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
pipe.On("Reset")
pipe.On("Duration").Return(time.Duration(0))
p := streams.NewSyncPump(mon, node, pipe)
defer p.Close()
err := p.Accept(msg)
assert.NoError(t, err)
processor.AssertExpectations(t)
mon.AssertExpectations(t)
}
func TestSyncPump_AcceptError(t *testing.T) {
msg := streams.NewMessage("test", "test")
processor := new(MockProcessor)
processor.On("Process", msg).Return(errors.New("test"))
processor.On("Close").Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
pipe.On("Reset")
pipe.On("Duration").Return(time.Duration(0))
p := streams.NewSyncPump(&fakeMonitor{}, node, pipe)
defer p.Close()
err := p.Accept(msg)
assert.Error(t, err)
}
func TestSyncPump_Close(t *testing.T) {
processor := new(MockProcessor)
processor.On("Close").Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
p := streams.NewSyncPump(&fakeMonitor{}, node, pipe)
err := p.Close()
assert.NoError(t, err)
processor.AssertExpectations(t)
}
func TestSyncPump_CloseError(t *testing.T) {
processor := new(MockProcessor)
processor.On("Close").Return(errors.New("test"))
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
p := streams.NewSyncPump(&fakeMonitor{}, node, pipe)
err := p.Close()
assert.Error(t, err)
}
func TestAsyncPump_Accept(t *testing.T) {
mon := new(MockMonitor)
mon.On("Processed", mock.Anything, mock.Anything, mock.Anything).Maybe().Return(nil)
msg := streams.NewMessage("test", "test")
processor := new(MockProcessor)
processor.On("Process", msg).Return(nil)
processor.On("Close").Maybe().Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
pipe.On("Reset")
pipe.On("Duration").Return(time.Duration(0))
p := streams.NewAsyncPump(mon, node, pipe, func(error) {})
defer p.Close()
err := p.Accept(msg)
time.Sleep(3 * time.Millisecond)
assert.NoError(t, err)
processor.AssertExpectations(t)
mon.AssertExpectations(t)
}
func TestAsyncPump_AcceptError(t *testing.T) {
var err error
msg := streams.NewMessage("test", "test")
processor := new(MockProcessor)
processor.On("Process", msg).Return(errors.New("test"))
processor.On("Close").Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
pipe.On("Reset")
pipe.On("Duration").Return(time.Duration(0))
p := streams.NewAsyncPump(&fakeMonitor{}, node, pipe, func(e error) {
err = e
})
defer p.Close()
p.Accept(msg)
time.Sleep(time.Millisecond)
assert.Error(t, err)
}
func TestAsyncPump_Close(t *testing.T) {
processor := new(MockProcessor)
processor.On("Close").Return(nil)
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
p := streams.NewAsyncPump(&fakeMonitor{}, node, pipe, func(error) {})
err := p.Close()
assert.NoError(t, err)
processor.AssertExpectations(t)
}
func TestAsyncPump_CloseError(t *testing.T) {
processor := new(MockProcessor)
processor.On("Close").Return(errors.New("test"))
node := streams.NewProcessorNode("test", processor)
pipe := new(MockTimedPipe)
p := streams.NewAsyncPump(&fakeMonitor{}, node, pipe, func(error) {})
err := p.Close()
assert.Error(t, err)
}
func TestNewSourcePump(t *testing.T) {
source := new(MockSource)
source.On("Close").Return(nil)
pump := new(MockPump)
p := streams.NewSourcePump(&fakeMonitor{}, "test", source, []streams.Pump{pump}, func(error) {})
assert.Implements(t, (*streams.SourcePump)(nil), p)
p.Close()
}
func TestSourcePump_CanConsume(t *testing.T) {
mon := new(MockMonitor)
mon.On("Processed", mock.Anything, mock.Anything, mock.Anything).Return(nil)
msg := streams.NewMessage("test", "test")
source := new(MockSource)
source.On("Consume").Maybe().Return(msg, nil)
source.On("Close").Return(nil)
pump := new(MockPump)
pump.On("Accept", msg).Return(nil)
p := streams.NewSourcePump(mon, "test", source, []streams.Pump{pump}, func(error) {})
defer p.Close()
defer p.Stop()
time.Sleep(time.Millisecond)
pump.AssertExpectations(t)
mon.AssertExpectations(t)
}
func TestSourcePump_HandlesPumpError(t *testing.T) {
gotError := false
msg := streams.NewMessage("test", "test")
source := new(MockSource)
source.On("Consume").Maybe().Return(msg, nil)
source.On("Close").Return(nil)
pump := new(MockPump)
pump.On("Accept", msg).Return(errors.New("test"))
p := streams.NewSourcePump(&fakeMonitor{}, "test", source, []streams.Pump{pump}, func(error) {
gotError = true
})
defer p.Close()
defer p.Stop()
time.Sleep(time.Millisecond)
assert.True(t, gotError)
}
func TestSourcePump_Close(t *testing.T) {
source := new(MockSource)
source.On("Consume").Maybe().Return(streams.NewMessage("test", "test"), nil)
source.On("Close").Return(nil)
p := streams.NewSourcePump(&fakeMonitor{}, "test", source, []streams.Pump{}, func(error) {})
p.Stop()
err := p.Close()
assert.NoError(t, err)
source.AssertExpectations(t)
}
func TestSourcePump_CloseError(t *testing.T) {
source := new(MockSource)
source.On("Consume").Return(streams.NewMessage("test", "test"), nil)
source.On("Close").Return(errors.New("test"))
p := streams.NewSourcePump(&fakeMonitor{}, "test", source, []streams.Pump{}, func(error) {})
p.Stop()
err := p.Close()
assert.Error(t, err)
}