func TestAnswerConversion(t *testing.T) { a1 := message.NewAnswerIdentity(testId) a2 := message.NewAnswerList(testList) a3 := message.NewAnswerRelay(testBody) b1 := a1.ToByteArray() b2 := a2.ToByteArray() b3 := a3.ToByteArray() c1 := new(message.Answer) c2 := new(message.Answer) c3 := new(message.Answer) c4 := new(message.Answer) e1 := c1.FromByteArray(b1) e2 := c2.FromByteArray(b2) e3 := c3.FromByteArray(b3) e4 := c4.FromByteArray(nil) assert.Nil(t, e1, "No error in conversion (1)") assert.Nil(t, e2, "No error in conversion (2)") assert.Nil(t, e3, "No error in conversion (3)") assert.NotNil(t, e4, "Conversion from nil throws an error (4)") assert.Nil(t, testutils.CompareAnswer(a1, c1), "Identity answer conversion wrong") assert.Nil(t, testutils.CompareAnswer(a2, c2), "List answer conversion wrong") assert.Nil(t, testutils.CompareAnswer(a3, c3), "Relay answer conversion wrong") }
func TestCreateList(t *testing.T) { req := message.NewRequest(message.List) ans := message.NewAnswerList(testList) ans2 := message.NewAnswerList(make([]uint64, 0)) if assert.NotNil(t, req, "List Request should not be nil") { assert.Equal(t, req.Type(), message.List, "MessageType should be List") } assert.Nil(t, req.Receivers, "Receivers should be nil") assert.Nil(t, req.Body, "Body should be nil") if assert.NotNil(t, ans, "List Answer should not be nil") { assert.Equal(t, ans.Type(), message.List, "MessageType should be List") } assert.Nil(t, testutils.CompareList(testList, ans.List()), "List not encoded correctly") assert.Equal(t, uint64(0), ans.Id(), "ID should return invalid value") assert.Len(t, ans.List(), len(testList), "List length shoud match") assert.Len(t, ans2.List(), 0, "Empty List should stay empty") assert.Nil(t, ans.Body(), "Body should return invalid value") }
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) { for n := 0; n < b.N; n++ {