Esempio n. 1
0
// Benchmark the Unmarshal() of the raw gogoprotobuf,
// in order to be compared with the codec.
func BenchmarkGoGoProtobufWithoutReflectUnmarshal(b *testing.B) {
	var err error

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

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

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		m1 := new(example.GoGoProtobufTestMessage1)
		assert.NoError(b, proto.Unmarshal(data[0], m1))

		m2 := new(example.GoGoProtobufTestMessage2)
		assert.NoError(b, proto.Unmarshal(data[1], m2))

		m3 := new(example.GoGoProtobufTestMessage3)
		assert.NoError(b, proto.Unmarshal(data[2], m3))

		m4 := new(example.GoGoProtobufTestMessage4)
		assert.NoError(b, proto.Unmarshal(data[3], m4))
	}
}
Esempio n. 2
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)
}
Esempio n. 3
0
// Benchmark the transporter's Send() and Recv().
func benchmarkTransporter(b *testing.B, s, r Transporter, target string) {
	data := make([]byte, 1024)
	for i := range data {
		data[i] = byte(rand.Int())
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Send.
		assert.NoError(b, s.Send(target, data))
		_, err := r.Recv()
		assert.NoError(b, err)
	}
}
Esempio n. 4
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)
		}
	}
}
Esempio n. 5
0
// 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")
}
Esempio n. 6
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())
}
Esempio n. 7
0
// 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)
}
Esempio n. 8
0
// Benchmark the Unmarshal() of the raw gogoprotobuf marshal,
// in order to be compared with the codec.
func BenchmarkGoGoProtobufWithoutReflectMarshal(b *testing.B) {
	var err error

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

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for j := range messages {
			data[j], err = proto.Marshal(messages[j])
			assert.NoError(b, err)
		}
	}
}
Esempio n. 9
0
// 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())
}
Esempio n. 10
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())
}