func TestConversion(t *testing.T) {
	buf := make([]byte, 8)
	message.Uint64ToByteArray(buf, testId)
	id := message.ByteArrayToUint64(buf)
	assert.Equal(t, testId, id, "uint64 conversion is wrong!")

	buf = make([]byte, 4)
	message.Uint32ToByteArray(buf, testNum)
	n := message.ByteArrayToUint32(buf)
	assert.Equal(t, testNum, n, "uint32 conversion is wrong!")

	a1 := message.Uint64ArrayToByteArray(nil)
	a2 := message.Uint64ArrayToByteArray(make([]uint64, 0))
	a3 := message.Uint64ArrayToByteArray(testList)

	assert.Nil(t, a1, "Should return nil with nil as input")
	assert.Len(t, a2, 0, "Should give empty byte list")
	assert.Len(t, a3, len(testList)*8, "One element takes 8 bytes of space")

	l1 := message.ByteArrayToUint64Array(nil)
	l2 := message.ByteArrayToUint64Array(make([]byte, 7))
	l3 := message.ByteArrayToUint64Array(make([]byte, 0))
	l4 := message.ByteArrayToUint64Array(a3)

	assert.Nil(t, l1, "Should return nil with nil as input")
	assert.Nil(t, l2, "Should return nil with incorrect length")
	assert.Len(t, l3, 0, "Should give empty byte list")
	assert.Nil(t, testutils.CompareList(l4, testList), "Lists should be the same")
}
Exemple #2
0
func (bucket *StatBucket) ToByteArray() []byte {

	arr := []uint64{
		bucket.TimeAlive.Get(),
		bucket.ClientsConnected.Get(),
		bucket.ClientsDisconnected.Get(),
		bucket.IncomingMessages.Get(),
		bucket.OutgoingMessages.Get(),
		bucket.ByteRead.Get(),
		bucket.ByteWritten.Get(),
	}

	return message.Uint64ArrayToByteArray(arr)
}
/* Benchmark List Conversions */
func BenchmarkByteArrayToUint64Array(b *testing.B) {
	for n := 0; n < b.N; n++ {
		message.Uint64ArrayToByteArray(testList)
	}
}
package message_test

import (
	"github.com/sech90/go-message-hub/message"
	"github.com/sech90/go-message-hub/testutils"
	"github.com/stretchr/testify/assert"
	"testing"
)

var testId = uint64(12345)
var testNum = uint32(12345)
var testList = testutils.GenList(255)
var testBody = testutils.GenPayload(1024 * 1000)
var testListByte = message.Uint64ArrayToByteArray(testList)

var tAns = new(message.Answer)
var tAnsList = message.NewAnswerList(testList)
var tAnsBody = message.NewAnswerRelay(testBody)
var tAnsListBytes = tAnsList.ToByteArray()
var tAnsBodyBytes = tAnsBody.ToByteArray()

var tReq = new(message.Request)
var tReqList = message.NewRequest(message.List)
var tReqBody = message.NewRelayRequest(testList, testBody)
var tReqListBytes = tReqList.ToByteArray()
var tReqBodyBytes = tReqBody.ToByteArray()

var mockRW = testutils.NewMockRW(nil, true)

/* Benchmark List Conversions */
func BenchmarkByteArrayToUint64Array(b *testing.B) {