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 (server *MockServer) newConnection(id uint64, s *mexsocket.MexSocket) { var req *message.Request for { req = new(message.Request) _, err := s.Read(req) if err != nil { log.Println("MocServer read error: ", err) break } if req.MexType == message.Identity { server.WriteTo(id, message.NewAnswerIdentity(id)) } } }
func TestCreateIdentity(t *testing.T) { req := message.NewRequest(message.Identity) ans := message.NewAnswerIdentity(testId) if assert.NotNil(t, req, "Identity Request should not be nil") { assert.Equal(t, req.Type(), message.Identity, "MessageType should be Identity") } assert.Nil(t, req.Receivers, "Receivers should be nil") assert.Nil(t, req.Body, "Body should be nil") if assert.NotNil(t, ans, "Identity Request should not be nil") { assert.Equal(t, ans.Type(), message.Identity, "MessageType should be Identity") } assert.Equal(t, testId, ans.Id(), "Id not encoded correctly") assert.Nil(t, ans.List(), "List should return invalid value") assert.Nil(t, ans.Body(), "Body should return invalid value") }
const ( addr = "localhost" port = 9999 ListLength = 255 delayTime = time.Millisecond * 0 PayloadLength = 1024 * 1000 SizeTestList = 1 ) var testId = uint64(12345) var testList = testutils.GenList(ListLength) var testBody = testutils.GenPayload(PayloadLength) var tAnsId = message.NewAnswerIdentity(testId) var tAnsList = message.NewAnswerList(testList) var tAnsBody = message.NewAnswerRelay(testBody) var emptyAns = new(message.Answer) var tReqId = message.NewRequest(message.Identity) var tReqList = message.NewRequest(message.List) var tReqBody = message.NewRelayRequest(testList, testBody) var emptyReq = new(message.Request) var listener net.Listener var newConn = make(chan net.Conn) //for testing var s1, s2 *mexsocket.MexSocket