func TestWriteMessage(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 = proto.WriteMessage(Encoder, Message) assert.Nil( err, "bytes not written", ) Decoder := NewReader(B) Received := make([]byte, 4*ByteSize) n, err := Decoder.Read(Received) assert.Nil( err, "no error", ) Received = Received[:n] assert.Equal( Message, Received, "bytes match", ) }
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", ) } }