@cryptotaxi247 / kubo / commits / be01dcf17

mux test stop.

Juan Batiz-Benet committed Sep 11, 2014 at 00:56 UTC be01dcf172b93f0877dc7a698bf00c36ac3e27b4
2 files changed +67 -2
net/mux/mux.go
-2
@@ -81,8 +81,6 @@ func (m *Muxer) handleIncomingMessages(ctx context.Context) {
81 go m.handleIncomingMessage(ctx, msg)
82
83 case <-ctx.Done():
84 - close(m.Incoming)
85 - close(m.Outgoing)
84 return
85 }
86 }
net/mux/mux_test.go
+67
@@ -216,3 +216,70 @@ func TestSimultMuxer(t *testing.T) {
216 }
217
218 }
219 +
220 +func TestStopping(t *testing.T) {
221 +
222 + // setup
223 + p1 := &TestProtocol{Pipe: msg.NewPipe(10)}
224 + p2 := &TestProtocol{Pipe: msg.NewPipe(10)}
225 + pid1 := ProtocolID_Test
226 + pid2 := ProtocolID_Identify
227 + mux1 := &Muxer{
228 + Pipe: msg.NewPipe(10),
229 + Protocols: ProtocolMap{
230 + pid1: p1,
231 + pid2: p2,
232 + },
233 + }
234 + peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
235 + // peer2 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275bbbbbb")
236 +
237 + // run muxer
238 + mux1.Start(context.Background())
239 +
240 + // test outgoing p1
241 + for _, s := range []string{"foo", "bar", "baz"} {
242 + p1.Outgoing <- &msg.Message{Peer: peer1, Data: []byte(s)}
243 + testWrappedMsg(t, <-mux1.Outgoing, pid1, []byte(s))
244 + }
245 +
246 + // test incoming p1
247 + for _, s := range []string{"foo", "bar", "baz"} {
248 + d, err := wrapData([]byte(s), pid1)
249 + if err != nil {
250 + t.Error(err)
251 + }
252 + mux1.Incoming <- &msg.Message{Peer: peer1, Data: d}
253 + testMsg(t, <-p1.Incoming, []byte(s))
254 + }
255 +
256 + mux1.Stop()
257 + if mux1.cancel != nil {
258 + t.Error("mux.cancel should be nil")
259 + }
260 +
261 + // test outgoing p1
262 + for _, s := range []string{"foo", "bar", "baz"} {
263 + p1.Outgoing <- &msg.Message{Peer: peer1, Data: []byte(s)}
264 + select {
265 + case <-mux1.Outgoing:
266 + t.Error("should not have received anything.")
267 + case <-time.After(time.Millisecond):
268 + }
269 + }
270 +
271 + // test incoming p1
272 + for _, s := range []string{"foo", "bar", "baz"} {
273 + d, err := wrapData([]byte(s), pid1)
274 + if err != nil {
275 + t.Error(err)
276 + }
277 + mux1.Incoming <- &msg.Message{Peer: peer1, Data: d}
278 + select {
279 + case <-p1.Incoming:
280 + t.Error("should not have received anything.")
281 + case <-time.After(time.Millisecond):
282 + }
283 + }
284 +
285 +}