Esempio n. 1
0
func BytesTest(t *testing.T, session *Session) {
	for i := 0; i < 2000; i++ {
		msg1 := RandBytes(512)
		err := session.Send(msg1)
		unitest.AssertNotError(t, err)

		var msg2 = make([]byte, len(msg1))
		err = session.Receive(msg2)
		unitest.AssertNotError(t, err)
		unitest.AssertBytes(t, msg1, msg2)
	}
}
Esempio n. 2
0
func ObjectTest(t *testing.T, session *Session) {
	for i := 0; i < 2000; i++ {
		msg1 := RandObject()
		err := session.Send(&msg1)
		unitest.AssertNotError(t, err)

		var msg2 TestObject
		err = session.Receive(&msg2)
		unitest.AssertNotError(t, err)
		unitest.Assert(t, msg1 == msg2, msg1, msg2)
	}
}
Esempio n. 3
0
func SessionTest(t *testing.T, codecType CodecType, test func(*testing.T, *Session)) {
	server, err := Serve("tcp", "0.0.0.0:0", TestCodec{})
	unitest.AssertNotError(t, err)
	addr := server.listener.Addr().String()

	serverWait := new(sync.WaitGroup)
	go func() {
		for {
			session, err := server.Accept()
			if err != nil {
				break
			}
			serverWait.Add(1)
			go func() {
				io.Copy(session.conn, session.conn)
				serverWait.Done()
			}()
		}
	}()

	clientWait := new(sync.WaitGroup)
	for i := 0; i < 60; i++ {
		clientWait.Add(1)
		go func() {
			session, err := Connect("tcp", addr, codecType)
			unitest.AssertNotError(t, err)
			test(t, session)
			session.Close()
			clientWait.Done()
		}()
	}
	clientWait.Wait()

	server.Stop()
	serverWait.Wait()
}