@cryptotaxi247 / kubo / commits / fc5b0c299

close listeners. + multiconn test

Juan Batiz-Benet committed Oct 19, 2014 at 03:51 UTC fc5b0c299469ecc8c83bda1372aea3a6f2e73069
1 file changed +82
net/conn/multiconn_test.go
+82
@@ -146,11 +146,16 @@ func setupMultiConns(t *testing.T, ctx context.Context) (a, b *MultiConn) {
146 t.Fatal(err)
147 }
148
149 + p1l.Close()
150 + p2l.Close()
151 +
152 log.Info("did you make multiconns?")
153 return c1.(*MultiConn), c2.(*MultiConn)
154 }
155
156 func TestMulticonnSend(t *testing.T) {
157 + // t.Skip("fooo")
158 +
159 log.Info("TestMulticonnSend")
160 ctx := context.Background()
161 ctxC, cancel := context.WithCancel(ctx)
@@ -211,3 +216,80 @@ func TestMulticonnSend(t *testing.T) {
216 msgsFrom2.CheckDone(t)
217 <-time.After(100 * time.Millisecond)
218 }
219 +
220 +func TestMulticonnSendUnderlying(t *testing.T) {
221 + // t.Skip("fooo")
222 +
223 + log.Info("TestMulticonnSendUnderlying")
224 + ctx := context.Background()
225 + ctxC, cancel := context.WithCancel(ctx)
226 +
227 + c1, c2 := setupMultiConns(t, ctx)
228 +
229 + log.Info("gen msgs")
230 + num := 100
231 + msgsFrom1 := genMessages(num, "from p1 to p2")
232 + msgsFrom2 := genMessages(num, "from p2 to p1")
233 +
234 + var wg sync.WaitGroup
235 +
236 + send := func(c *MultiConn, msgs *msgMap) {
237 + defer wg.Done()
238 +
239 + conns := make([]Conn, 0, len(c.conns))
240 + for _, c1 := range c.conns {
241 + conns = append(conns, c1)
242 + }
243 +
244 + i := 0
245 + for _, m := range msgs.msgs {
246 + log.Info("send: %s", m.payload)
247 + switch i % 3 {
248 + case 0:
249 + conns[0].Out() <- []byte(m.payload)
250 + case 1:
251 + conns[1].Out() <- []byte(m.payload)
252 + case 2:
253 + c.Out() <- []byte(m.payload)
254 + }
255 + msgs.Sent(t, m.payload)
256 + <-time.After(time.Microsecond * 10)
257 + i++
258 + }
259 + }
260 +
261 + recv := func(ctx context.Context, c *MultiConn, msgs *msgMap) {
262 + defer wg.Done()
263 +
264 + for {
265 + select {
266 + case payload := <-c.In():
267 + msgs.Received(t, string(payload))
268 + log.Info("recv: %s", payload)
269 + if msgs.recv == len(msgs.msgs) {
270 + return
271 + }
272 +
273 + case <-ctx.Done():
274 + return
275 +
276 + }
277 + }
278 +
279 + }
280 +
281 + log.Info("msg send + recv")
282 +
283 + wg.Add(4)
284 + go send(c1, msgsFrom1)
285 + go send(c2, msgsFrom2)
286 + go recv(ctxC, c1, msgsFrom2)
287 + go recv(ctxC, c2, msgsFrom1)
288 + wg.Wait()
289 + cancel()
290 + c1.Close()
291 + c2.Close()
292 +
293 + msgsFrom1.CheckDone(t)
294 + msgsFrom2.CheckDone(t)
295 +}