Exemple #1
0
func testMarshalUnmarshal(t *testing.T, c Codec, msg interface{}) {
	b, err := c.Marshal(msg)
	assert.NoError(t, err)
	assert.NotNil(t, b)

	m, err := c.Unmarshal(b)
	assert.NoError(t, err)
	assert.Equal(t, msg, m)
}
// Test the transporter's interface.
func testTransporter(t *testing.T, s, r Transporter, target string) {
	expectedData := generateRandomBytes(2048, 1024)
	actualData := make([][]byte, len(expectedData))

	done := make(chan struct{})
	// Send.
	go func() {
		for i := range expectedData {
			assert.NoError(t, s.Send(target, expectedData[i]))
		}
	}()

	// Receive.
	go func() {
		for i := 0; i < len(expectedData); i++ {
			b, err := r.Recv()
			assert.NoError(t, err)
			actualData[i] = b
		}
		close(done)
	}()

	select {
	case <-time.After(time.Second * 10):
		t.Fatal("Not enough messages received, waited 10s")
	case <-done:
		assert.Equal(t, expectedData, actualData)
	}

	// Make sure there is no pending messages.
	finish := make(chan struct{})
	go func() {
		r.Recv()
		close(finish)
	}()

	select {
	case <-finish:
		t.Fatal("Receiving unexpected message")
	case <-time.After(time.Second * 5):
	}

	assert.NoError(t, s.Stop())
	assert.NoError(t, s.Destroy())
	assert.NoError(t, r.Stop())
	assert.NoError(t, r.Destroy())
}
// Test Send() and Recv() of the messenger.
func TestSendRecv(t *testing.T) {
	// Create the sender.
	c := codec.NewGoGoProtobufCodec()
	assert.NotNil(t, c)
	tr := transporter.NewHTTPTransporter("localhost:8008")

	// Should fail to create the messenger.
	assert.Nil(t, New(c, tr, false, false))
	m := New(c, tr, true, true)
	assert.NotNil(t, m)

	assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
	assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
	assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
	assert.NoError(t, m.RegisterMessage(&example.GoGoProtobufTestMessage4{}))

	assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage1{}, handler1))
	assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage2{}, handler2))
	assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage3{}, handler3))
	assert.NoError(t, m.RegisterHandler(&example.GoGoProtobufTestMessage4{}, handler4))

	// Create the echo server.
	c = codec.NewGoGoProtobufCodec()
	assert.NotNil(t, c)
	tr = transporter.NewHTTPTransporter("localhost:8009")

	n := New(c, tr, false, true)
	assert.NotNil(t, n)

	e := &echoServer{
		m:        n,
		peerAddr: "localhost:8008",
	}

	assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
	assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
	assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
	assert.NoError(t, n.RegisterMessage(&example.GoGoProtobufTestMessage4{}))

	assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage1{}, e.msgHandler))
	assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage2{}, e.msgHandler))
	assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage3{}, e.msgHandler))
	assert.NoError(t, n.RegisterHandler(&example.GoGoProtobufTestMessage4{}, e.msgHandler))

	assert.NoError(t, m.Start())
	assert.NoError(t, n.Start())

	cnt := 10
	messages := generateMessages(cnt)

	go func() {
		for i := range messages {
			m.Send("localhost:8009", messages[i])
		}
	}()

	var recvMessages []interface{}

	wait := make(chan struct{})
	go func() {
		for {
			select {
			case <-wait:
				return
			default:
			}
			msg, err := m.Recv()
			assert.NoError(t, err)

			recvMessages = append(recvMessages, msg)
		}
	}()
	<-time.After(time.Second * 5)

	for i := range messages {
		assert.Equal(t, messages[i], recvMessages[i])

	}

	// Verify that the handlers are called.
	assert.Equal(t, cnt, count1)
	assert.Equal(t, cnt, count2)
	assert.Equal(t, cnt, count3)
	assert.Equal(t, cnt, count4)

	assert.NoError(t, m.Stop())
	assert.NoError(t, n.Stop())

	assert.NoError(t, m.Destroy())
	assert.NoError(t, n.Destroy())
}