@@ -19,6 +19,7 @@ package broadcast
19
19
import (
20
20
"fmt"
21
21
"testing"
22
+ "time"
22
23
23
24
"github.com/hyperledger/fabric/orderer/common/filter"
24
25
cb "github.com/hyperledger/fabric/protos/common"
@@ -73,21 +74,13 @@ func (mm *mockSupportManager) ProposeChain(configTx *cb.Envelope) cb.Status {
73
74
filter .EmptyRejectRule ,
74
75
filter .AcceptRule ,
75
76
}),
76
- queue : make (chan * cb.Envelope ),
77
77
}
78
78
return cb .Status_SUCCESS
79
79
}
80
80
81
- func (mm * mockSupportManager ) halt () {
82
- for _ , chain := range mm .chains {
83
- chain .halt ()
84
- }
85
- }
86
-
87
81
type mockSupport struct {
88
- filters * filter.RuleSet
89
- queue chan * cb.Envelope
90
- done bool
82
+ filters * filter.RuleSet
83
+ rejectEnqueue bool
91
84
}
92
85
93
86
func (ms * mockSupport ) Filters () * filter.RuleSet {
@@ -96,16 +89,7 @@ func (ms *mockSupport) Filters() *filter.RuleSet {
96
89
97
90
// Enqueue sends a message for ordering
98
91
func (ms * mockSupport ) Enqueue (env * cb.Envelope ) bool {
99
- ms .queue <- env
100
- return ! ms .done
101
- }
102
-
103
- func (ms * mockSupport ) halt () {
104
- ms .done = true
105
- select {
106
- case <- ms .queue :
107
- default :
108
- }
92
+ return ! ms .rejectEnqueue
109
93
}
110
94
111
95
func makeConfigMessage (chainID string ) * cb.Envelope {
@@ -137,29 +121,31 @@ func makeMessage(chainID string, data []byte) *cb.Envelope {
137
121
}
138
122
}
139
123
140
- func getMultichainManager () * mockSupportManager {
124
+ func getMockSupportManager () ( * mockSupportManager , * mockSupport ) {
141
125
filters := filter .NewRuleSet ([]filter.Rule {
142
126
filter .EmptyRejectRule ,
143
127
filter .AcceptRule ,
144
128
})
145
129
mm := & mockSupportManager {
146
130
chains : make (map [string ]* mockSupport ),
147
131
}
148
- mm . chains [ string ( systemChain )] = & mockSupport {
132
+ mSysChain : = & mockSupport {
149
133
filters : filters ,
150
- queue : make (chan * cb.Envelope ),
151
134
}
152
- return mm
135
+ mm .chains [string (systemChain )] = mSysChain
136
+ return mm , mSysChain
153
137
}
154
138
155
- func TestQueueOverflow (t * testing.T ) {
156
- mm := getMultichainManager ()
157
- defer mm .halt ()
158
- bh := NewHandlerImpl (mm , 2 )
139
+ func TestEnqueueFailure (t * testing.T ) {
140
+ mm , mSysChain := getMockSupportManager ()
141
+ bh := NewHandlerImpl (mm )
159
142
m := newMockB ()
160
143
defer close (m .recvChan )
161
- b := newBroadcaster (bh .(* handlerImpl ))
162
- go b .queueEnvelopes (m )
144
+ done := make (chan struct {})
145
+ go func () {
146
+ bh .Handle (m )
147
+ close (done )
148
+ }()
163
149
164
150
for i := 0 ; i < 2 ; i ++ {
165
151
m .recvChan <- makeMessage (systemChain , []byte ("Some bytes" ))
@@ -169,86 +155,77 @@ func TestQueueOverflow(t *testing.T) {
169
155
}
170
156
}
171
157
158
+ mSysChain .rejectEnqueue = true
172
159
m .recvChan <- makeMessage (systemChain , []byte ("Some bytes" ))
173
160
reply := <- m .sendChan
174
161
if reply .Status != cb .Status_SERVICE_UNAVAILABLE {
175
162
t .Fatalf ("Should not have successfully queued the message" )
176
163
}
177
164
178
- }
179
-
180
- func TestMultiQueueOverflow (t * testing.T ) {
181
- mm := getMultichainManager ()
182
- defer mm .halt ()
183
- bh := NewHandlerImpl (mm , 2 )
184
- ms := []* mockB {newMockB (), newMockB (), newMockB ()}
185
-
186
- for _ , m := range ms {
187
- defer close (m .recvChan )
188
- b := newBroadcaster (bh .(* handlerImpl ))
189
- go b .queueEnvelopes (m )
190
- }
191
-
192
- for _ , m := range ms {
193
- for i := 0 ; i < 2 ; i ++ {
194
- m .recvChan <- makeMessage (systemChain , []byte ("Some bytes" ))
195
- reply := <- m .sendChan
196
- if reply .Status != cb .Status_SUCCESS {
197
- t .Fatalf ("Should have successfully queued the message" )
198
- }
199
- }
200
- }
201
-
202
- for _ , m := range ms {
203
- m .recvChan <- makeMessage (systemChain , []byte ("Some bytes" ))
204
- reply := <- m .sendChan
205
- if reply .Status != cb .Status_SERVICE_UNAVAILABLE {
206
- t .Fatalf ("Should not have successfully queued the message" )
207
- }
165
+ select {
166
+ case <- done :
167
+ case <- time .After (time .Second ):
168
+ t .Fatalf ("Should have terminated the stream" )
208
169
}
209
170
}
210
171
211
172
func TestEmptyEnvelope (t * testing.T ) {
212
- mm := getMultichainManager ()
213
- defer mm .halt ()
214
- bh := NewHandlerImpl (mm , 2 )
173
+ mm , _ := getMockSupportManager ()
174
+ bh := NewHandlerImpl (mm )
215
175
m := newMockB ()
216
176
defer close (m .recvChan )
217
- go bh .Handle (m )
177
+ done := make (chan struct {})
178
+ go func () {
179
+ bh .Handle (m )
180
+ close (done )
181
+ }()
218
182
219
183
m .recvChan <- & cb.Envelope {}
220
184
reply := <- m .sendChan
221
185
if reply .Status != cb .Status_BAD_REQUEST {
222
186
t .Fatalf ("Should have rejected the null message" )
223
187
}
224
188
189
+ select {
190
+ case <- done :
191
+ case <- time .After (time .Second ):
192
+ t .Fatalf ("Should have terminated the stream" )
193
+ }
225
194
}
226
195
227
196
func TestBadChainID (t * testing.T ) {
228
- mm := getMultichainManager ()
229
- defer mm .halt ()
230
- bh := NewHandlerImpl (mm , 2 )
197
+ mm , _ := getMockSupportManager ()
198
+ bh := NewHandlerImpl (mm )
231
199
m := newMockB ()
232
200
defer close (m .recvChan )
233
- go bh .Handle (m )
201
+ done := make (chan struct {})
202
+ go func () {
203
+ bh .Handle (m )
204
+ close (done )
205
+ }()
234
206
235
207
m .recvChan <- makeMessage ("Wrong chain" , []byte ("Some bytes" ))
236
208
reply := <- m .sendChan
237
209
if reply .Status != cb .Status_NOT_FOUND {
238
210
t .Fatalf ("Should have rejected message to a chain which does not exist" )
239
211
}
240
212
213
+ select {
214
+ case <- done :
215
+ case <- time .After (time .Second ):
216
+ t .Fatalf ("Should have terminated the stream" )
217
+ }
241
218
}
242
219
243
220
func TestNewChainID (t * testing.T ) {
244
- mm := getMultichainManager ()
245
- defer mm .halt ()
246
- bh := NewHandlerImpl (mm , 2 )
221
+ mm , _ := getMockSupportManager ()
222
+ bh := NewHandlerImpl (mm )
247
223
m := newMockB ()
248
224
defer close (m .recvChan )
249
225
go bh .Handle (m )
226
+ newChainID := "New Chain"
250
227
251
- m .recvChan <- makeConfigMessage ("New chain" )
228
+ m .recvChan <- makeConfigMessage (newChainID )
252
229
reply := <- m .sendChan
253
230
if reply .Status != cb .Status_SUCCESS {
254
231
t .Fatalf ("Should have created a new chain, got %d" , reply .Status )
@@ -258,9 +235,9 @@ func TestNewChainID(t *testing.T) {
258
235
t .Fatalf ("Should have created a new chain" )
259
236
}
260
237
261
- m .recvChan <- makeMessage ("New chain" , []byte ("Some bytes" ))
238
+ m .recvChan <- makeMessage (newChainID , []byte ("Some bytes" ))
262
239
reply = <- m .sendChan
263
240
if reply .Status != cb .Status_SUCCESS {
264
- t .Fatalf ("Should have successfully sent message to new chain" )
241
+ t .Fatalf ("Should have successfully sent message to new chain, got %v" , reply )
265
242
}
266
243
}
0 commit comments