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 (hub *Hub) processRequest(socket *mexsocket.MexSocket, req *message.Request) { if req == nil { return } switch req.MexType { //create a new answer and send it over the channel case message.Identity: answer := message.NewAnswerIdentity(socket.Id) socket.Send(answer) //get the list of connected clients, remove the current one and send it over the channel case message.List: //the resulting list is []interface{} list := set.Difference(hub.idSet, set.New(socket.Id)).List() //create new answer answer := new(message.Answer) answer.MexType = message.List answer.Payload = convertSetList(list) socket.Send(answer) case message.Relay: //create an answer containing the payload answer := message.NewAnswerRelay(req.Body) //call hub to send the message to the list of clients provided hub.Multicast(req.Receivers, answer) } }
func TestClearAnswer(t *testing.T) { a := message.NewAnswerRelay(testBody) a.Clear() assert.Equal(t, message.Empty, a.Type(), "Cleared message type should be Empty") assert.Nil(t, a.Payload, "Cleared Payload should be Nil") }
func TestCreateRelay(t *testing.T) { req := message.NewRelayRequest(testList, testBody) ans := message.NewAnswerRelay(testBody) if assert.NotNil(t, req, "Relay Request should not be nil") { assert.Equal(t, req.Type(), message.Relay, "MessageType should be Relay") } if assert.NotNil(t, req.Receivers, "Receivers should not be nil") { if err := testutils.CompareList(testList, req.Receivers); err != nil { t.Error(err) } } if assert.NotNil(t, req.Body, "Body should not be nil") { assert.Nil(t, testutils.CompareBytes(testBody, req.Body), "Body should be the same of the one provided") } assert.Nil(t, testutils.CompareBytes(testBody, ans.Body()), "Body should be the same of the one provided") assert.Equal(t, uint64(0), ans.Id(), "ID should return invalid value") assert.Nil(t, ans.List(), "List 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++ { message.Uint64ArrayToByteArray(testList)