Beispiel #1
0
func TestGoGoProtobufCodec(t *testing.T) {
	c := NewGoGoProtobufCodec()
	assert.NotNil(t, c)
	assert.NoError(t, c.Initial())

	// Register messages.
	assert.NoError(t, c.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
	assert.NoError(t, c.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
	assert.NoError(t, c.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
	assert.NoError(t, c.RegisterMessage(&example.GoGoProtobufTestMessage4{}))

	// Should fail because we have already registered once.
	assert.Error(t, c.RegisterMessage(&example.GoGoProtobufTestMessage4{}))

	// Try to marshal/unmarshal messages.
	messages := generateGoGoProtobufMessages()
	for i := range messages {
		testMarshalUnmarshal(t, c, messages[i])
	}

	// Try to marshal an unregistered message, should fail.
	_, err := c.Marshal(&example.GoGoProtobufTestMessage5{})
	assert.Error(t, err)

	assert.NoError(t, c.Destroy())
}
Beispiel #2
0
// Benchmark the Unmarshal() of the gogoprotobuf codec.
func BenchmarkGoGoProtoBufCodecUnmarshal(b *testing.B) {
	var err error

	messages := generateGoGoProtobufMessages()
	data := make([][]byte, len(messages))

	c := NewGoGoProtobufCodec()
	assert.NotNil(b, c)
	assert.NoError(b, c.Initial())

	// Register messages.
	assert.NoError(b, c.RegisterMessage(&example.GoGoProtobufTestMessage1{}))
	assert.NoError(b, c.RegisterMessage(&example.GoGoProtobufTestMessage2{}))
	assert.NoError(b, c.RegisterMessage(&example.GoGoProtobufTestMessage3{}))
	assert.NoError(b, c.RegisterMessage(&example.GoGoProtobufTestMessage4{}))

	for j := range messages {
		data[j], err = c.Marshal(messages[j])
		assert.NoError(b, err)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for j := range data {
			_, err := c.Unmarshal(data[j])
			assert.NoError(b, err)
		}
	}
}
// Test the HTTPTransporter.
func TestHTTPTransporter(t *testing.T) {
	sender := NewHTTPTransporter("localhost:8080")
	assert.NotNil(t, sender)

	receiver := NewHTTPTransporter("localhost:8081")
	assert.NotNil(t, receiver)

	go func() {
		assert.NoError(t, sender.Start())
	}()
	go func() {
		assert.NoError(t, receiver.Start())
	}()

	time.Sleep(time.Second)

	testTransporter(t, sender, receiver, "localhost:8081")
}
Beispiel #4
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)
}
// Benchmark the HTTPTransporter.
func BenchmarkHTTPTransporter(b *testing.B) {
	// Use random port to avoid port collision (hopefully).
	port := rand.Intn(100) + 8000
	s := fmt.Sprintf("localhost:%d", port)
	r := fmt.Sprintf("localhost:%d", port+1)
	sender := NewHTTPTransporter(s)
	assert.NotNil(b, sender)

	receiver := NewHTTPTransporter(r)
	assert.NotNil(b, receiver)

	go func() {
		assert.NoError(b, sender.Start())
	}()
	go func() {
		assert.NoError(b, receiver.Start())
	}()

	time.Sleep(time.Second)

	benchmarkTransporter(b, sender, receiver, r)
}
Beispiel #6
0
// 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())
}