func TestCodec(t *testing.T) { f := trace.NewFrame() sx := sandbox.NewUnreliableTransport(f.Refine("sandbox"), 5, 0, time.Second/3, time.Second/3) hx := chain.NewTransport(f.Refine("chain"), sx) fx := faithful.NewTransport(f.Refine("faithful"), hx) cx := codec.NewTransport(fx, codec.GobCodec{}) bx := NewTransport(f.Refine("session"), cx) // Sync ya, yb := make(chan int), make(chan int) // Accepter go func() { as := bx.Listen(sandbox.Addr("@")).AcceptSession() for i := 0; i < testN; i++ { go testAcceptConn(t, as, ya, yb) } }() // Dialer ds := bx.DialSession(sandbox.Addr("@"), nil) go func() { for i := 0; i < testN; i++ { go testDialConn(t, ds, ya, yb) } }() for i := 0; i < testN; i++ { <-yb } }
func testConn(t *testing.T, mode testMode) { f := trace.NewFrame("testConn") f.Bind(&f) fsx := f.Refine("sandbox") fsx.Bind(&fsx) var x *sandbox.Transport if mode.Random { x = sandbox.NewRandomUnreliableTransport(fsx, mode.NOK, mode.NDrop, exp, exp) } else { x = sandbox.NewUnreliableTransport(fsx, mode.NOK, mode.NDrop, exp, exp) } ready := make(chan int, 3) // Accept side go func() { ax := NewTransport(f.Refine("faithful:a"), chain.NewTransport(f.Refine("chain:a"), x)) l := ax.Listen(sandbox.Addr("@")) ready <- 1 c := l.Accept() switch mode.Kind { case testDWAR: testRead(f, t, c, ready) case testDRAW: testWrite(f, t, c, ready) } }() // Dial side dx := NewTransport(f.Refine("faithful:d"), chain.NewTransport(f.Refine("chain:d"), x)) <-ready c := dx.Dial(sandbox.Addr("@")) setabrt(func() { dbg := c.Debug().(*sandbox.DebugInfo) dbg.Out.Abort() }) switch mode.Kind { case testDWAR: testWrite(f, t, c, ready) case testDRAW: testRead(f, t, c, ready) } _, _ = <-ready, <-ready // One from testRead and one from testWrite }
func testConn(t *testing.T, mode connMode) { // Transport frame := trace.NewFrame() var x Carrier if mode.Random { x = sandbox.NewRandomUnreliableTransport(frame.Refine("sandbox"), mode.NOK, mode.NDrop, 0, 0) } else { x = sandbox.NewUnreliableTransport(frame.Refine("sandbox"), mode.NOK, mode.NDrop, 0, 0) } // Signalling ready := make(chan int, 2) feedfwd := make(chan byte, mode.NWrite()+1) // Accepter endpoint go func() { defer func() { ready <- 1 // SYNC: Notify that accepter-side logic is done }() l := NewListener(frame.Refine("chain", "listener"), x, sandbox.Addr("")) ready <- 1 // SYNC: Notify that listener is accepting switch mode.Kind { case testDWAR: testRead(t, l.Accept(), feedfwd) case testDRAW: testWrite(t, l.Accept(), mode, feedfwd) default: panic("u") } }() // Dialer endpoint <-ready // SYNC: Wait for listener to start accepting d := NewDialer(frame.Refine("chain", "dialer"), x) conn := d.Dial(sandbox.Addr("")) switch mode.Kind { case testDWAR: testWrite(t, conn, mode, feedfwd) case testDRAW: testRead(t, conn, feedfwd) default: panic("u") } <-ready // SYNC: Wait for accepter goroutine to complete }