func TestEndpointsRequestGoroutine(t *testing.T) {

	var wg sync.WaitGroup
	var tests = loadList(tReqBody, SizeTestList)

	wg.Add(2)
	go func() {
		for _, test := range tests {
			_, e1 := s1.Send(test)
			assert.Nil(t, e1, "Send request should work")
		}
		wg.Done()
	}()

	go func() {
		for _, _ = range tests {
			req := new(message.Request)
			_, e2 := s2.Read(req)
			assert.Nil(t, e2, "Read request should work")

			if req.MexType == message.Identity {
				assert.Nil(t, testutils.CompareRequests(tReqId, req), "Requests should be the same")
			} else if req.MexType == message.List {
				assert.Nil(t, testutils.CompareRequests(tReqList, req), "Requests should be the same")
			} else if req.MexType == message.Relay {
				assert.Nil(t, testutils.CompareRequests(tReqBody, req), "Requests should be the same")
			} else {
				t.Error("mextype unknown")
			}
		}
		wg.Done()
	}()

	wg.Wait()
}
func TestRequestConversion(t *testing.T) {

	assert := assert.New(t)

	m1 := message.NewRequest(message.Identity)
	m2 := message.NewRequest(message.List)
	m3 := message.NewRelayRequest(testList, testBody)
	m4 := message.NewRelayRequest(testutils.GenList(message.MAX_RECEIVERS+1), testBody)
	m5 := message.NewRelayRequest(testList, testutils.GenPayload(message.MAX_PAYLOAD+1))

	assert.Nil(m4, "Messages with too many receivers not allowed")
	assert.Nil(m5, "Messages with payload too big not allowed")

	b1 := m1.ToByteArray()
	b2 := m2.ToByteArray()
	b3 := m3.ToByteArray()

	c1 := new(message.Request)
	c2 := new(message.Request)
	c3 := new(message.Request)
	c4 := new(message.Request)

	e1 := c1.FromByteArray(b1)
	e2 := c2.FromByteArray(b2)
	e3 := c3.FromByteArray(b3)
	e4 := c4.FromByteArray(nil)
	e5 := c4.FromByteArray(make([]byte, 0))

	assert.Nil(e1, "No error in conversion (1)")
	assert.Nil(e2, "No error in conversion (2)")
	assert.Nil(e3, "No error in conversion (3)")
	assert.NotNil(e4, "Conversion from nil throws an error (4)")
	assert.NotNil(e5, "Conversion from empty throws an error (4)")

	assert.Nil(testutils.CompareRequests(m1, c1), "Identity request conversion wrong")
	assert.Nil(testutils.CompareRequests(m2, c2), "List request conversion wrong")
	assert.Nil(testutils.CompareRequests(m3, c3), "Relay request conversion wrong")
}
func TestEndpointsRequest(t *testing.T) {
	req := new(message.Request)

	var tests = loadList(tReqBody, SizeTestList)

	for _, test := range tests {
		_, e1 := s1.Send(test)
		assert.Nil(t, e1, "Send request should work")

		_, e2 := s2.Read(req)
		assert.Nil(t, e2, "Read request should work")
		assert.Nil(t, testutils.CompareRequests(test.(*message.Request), req), "Requests should be the same")
	}
}
func TestEndpointsServices(t *testing.T) {

	go s1.StartReadService(mexsocket.ModeServer)
	go s1.StartWriteService()

	go s2.StartReadService(mexsocket.ModeClient)
	go s2.StartWriteService()

	go func() { s2.Outgoing() <- tReqBody }()
	req := <-s1.Incoming()
	assert.Nil(t, testutils.CompareRequests(tReqBody, req.(*message.Request)), "request should be received correctly")

	go func() { s1.Outgoing() <- tAnsBody }()
	ans := <-s2.Incoming()
	assert.Nil(t, testutils.CompareAnswer(tAnsBody, ans.(*message.Answer)), "answer should be received correctly")
}