Ejemplo n.º 1
0
func TestReadMessage(t *testing.T) {
	assert := assert.New(t)

	ByteSize := 1000

	Message, err := randomBytes(ByteSize)

	assert.Nil(
		err,
		"could not create random bytes",
	)

	B := bytes.NewBuffer(nil)

	Encoder := NewWriter(B)

	_, err = Encoder.Write(Message)

	assert.Nil(
		err,
		"bytes not written",
	)

	_, err = Encoder.Write(nil)

	assert.Nil(
		err,
		"bytes not written",
	)

	Decoder := NewReader(B)

	Received, err := proto.ReadMessage(Decoder)

	assert.Nil(
		err,
		"bytes not read",
	)

	assert.Equal(
		Message,
		Received,
		"bytes match",
	)
}
Ejemplo n.º 2
0
func TestCopyMessages(t *testing.T) {
	assert := assert.New(t)
	require := require.New(t)

	ByteSize := 1000
	L := 10

	MessagesEncoded := bytes.NewBuffer(nil)
	Encoder := NewWriter(MessagesEncoded)

	Messages := make([][]byte, L)

	for i := range Messages {
		// Expected Bytes Per Message
		Message, err := randomBytes(ByteSize)

		assert.Nil(
			err,
			"could not create random bytes",
		)

		_, err = proto.WriteMessage(Encoder, Message)

		assert.Nil(
			err,
			"bytes not written",
		)

		Messages[i] = Message
	}

	Decoder := NewReader(MessagesEncoded)

	MessagesEncodedAgain := bytes.NewBuffer(nil)
	EncoderAgain := NewWriter(MessagesEncodedAgain)

	B := make([]byte, 512)

	_, err := proto.CopyMessages(EncoderAgain, Decoder, B, L-1)

	assert.Nil(
		err,
		"there is no error",
	)

	_, err = proto.CopyMessages(EncoderAgain, Decoder, B, 1)

	assert.Nil(
		err,
		"there is no error",
	)

	_, err = proto.CopyMessages(EncoderAgain, Decoder, B, 1)

	assert.Equal(
		io.EOF,
		err,
		"hit EOF",
	)

	DecoderAgain := NewReader(MessagesEncodedAgain)

	for _, Sent := range Messages {
		Received, err := proto.ReadMessage(DecoderAgain)

		assert.Nil(
			err,
			"bytes not read",
		)

		require.Equal(
			Sent,
			Received,
			"bytes match",
		)
	}
}