Example #1
0
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
	}
}
Example #2
0
func TestConnOverTCP(t *testing.T) {
	frame := trace.NewFrame()
	x := NewTransport(frame.Refine("faith"), chain.NewTransport(frame.Refine("chain"), tcp.Transport))

	ready := make(chan int, 2)
	sent, recv := make(map[byte]struct{}), make(map[byte]struct{})

	// Accepter endpoint
	go func() {
		l := x.Listen(tcp.Addr(":17222"))
		ready <- 1 // SYNC: Notify that listener is accepting
		testGreedyRead(t, l.Accept(), recv)
		ready <- 1
	}()

	// Dialer endpoint
	<-ready // SYNC: Wait for listener to start accepting
	conn := x.Dial(tcp.Addr("localhost:17222"))
	testGreedyWrite(t, conn, sent)
	<-ready // SYNC: Wait for accepter goroutine to complete

	// Make sure all marked writes have been received
	if !reflect.DeepEqual(sent, recv) {
		t.Errorf("expected %#v, got %#v", sent, recv)
		failNow()
	}
}
Example #3
0
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
}
Example #4
0
func TestCodec(t *testing.T) {

	// Transport
	f := trace.NewFrame()
	// Carrier
	sx := sandbox.NewRandomUnreliableTransport(f.Refine("sandbox"), 3, 3, time.Second/4, time.Second/4)
	// Chain
	hx := chain.NewTransport(f.Refine("chain"), sx)
	// Faithful
	fx := faithful.NewTransport(f.Refine("faithful"), hx)
	// Codec
	cx := NewTransport(fx, GobCodec{})

	// Sync
	y := make(chan int)

	// Accepter
	go func() {
		l := cx.Listen(sandbox.Addr("@"))
		for i := 0; i < testN; i++ {
			y <- 1
			conn := l.Accept()
			msg := &testMsg{}
			if err := conn.Read(msg); err != nil {
				t.Fatalf("read (%s)", err)
				failNow()
			}
			if msg.Carry != i {
				t.Fatalf("check")
				failNow()
			}
			f.Printf("READ %d/%d CLOSING", i+1, testN)
			conn.Close()
			f.Printf("READ %d/%d √", i+1, testN)
		}
		y <- 1
	}()

	// Dialer
	for i := 0; i < testN; i++ {
		<-y
		conn := cx.Dial(sandbox.Addr("@"))
		if err := conn.Write(&testMsg{i}); err != nil {
			t.Fatalf("write (%s)", err)
			failNow()
		}
		f.Printf("WRITE %d/%d CLOSING", i+1, testN)
		if err := conn.Close(); err != nil {
			t.Fatalf("close (%s)", err)
			failNow()
		}
		f.Printf("WRITE %d/%d √", i+1, testN)
	}
	<-y
}
Example #5
0
func NewStructOverTCP() *blend.Transport {
	f := trace.NewFrame("tele")
	// Carrier
	x0 := tcp.Transport
	// Chain
	x1 := chain.NewTransport(f.Refine("chain"), x0)
	// Faithful
	x2 := faithful.NewTransport(f.Refine("faithful"), x1)
	// Codec
	x3 := codec.NewTransport(x2, codec.GobCodec{})
	// Blend
	return blend.NewTransport(f.Refine("blend"), x3)
}
Example #6
0
func TestClosure(t *testing.T) {
	const testK = 1

	f := trace.NewFrame()
	sx := tcp.Transport
	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)

	a, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:40090")

	// Accepter
	go func() {
		as := bx.Listen(a).AcceptSession()
		for i := 0; i < testK; i++ {
			go testAcceptConn(t, as, ya, yb)
		}
	}()

	// Dialer
	ds := bx.DialSession(a, nil)
	go func() {
		for i := 0; i < testK; i++ {
			go testDialConn(t, ds, ya, yb)
		}
	}()

	for i := 0; i < testK; i++ {
		<-yb
	}

	ds.Close()
	println("hold a minute...")
	time.Sleep(time.Minute)
	println("now check that the test process has no open tcp connections...")
	time.Sleep(time.Minute)
	println("great.")
}