func TestPostDisconnect(t *testing.T) {
	assert := assert.New(t)

	cli := client.NewClient()
	err := cli.Connect(Addr, Port)
	assert.Nil(err, "new client should connect")

	//wait for id
	<-cli.IncomingId()

	cli.Send(message.NewRequest(message.List))

	//wait for list
	<-cli.IncomingList()

	if len(cli.List()) > 0 {
		time.Sleep(time.Millisecond * 200)
		cli.Send(message.NewRequest(message.List))
		//wait for list
		<-cli.IncomingList()
	}

	assert.Len(cli.List(), 0, "Server should return empty list")

	cli.Disconnect()
}
func GetList() {

	var wg sync.WaitGroup

	//create one list request for all clients
	listReq := message.NewRequest(message.List).ToByteArray()

	wg.Add(cliNum)
	for _, id := range allCliId {
		go func(id uint64) {

			//send request for list
			item, _ := climap.Get(id)
			cli := item.(*client.Client)
			cli.SendBytes(listReq)

			//wait server reply
			<-cli.IncomingList()

			wg.Done()
		}(id)
	}

	//wait all clients to complete
	wg.Wait()
}
func TestList(t *testing.T) {

	assert := assert.New(t)
	var wg sync.WaitGroup

	mex := message.NewRequest(message.List)

	wg.Add(ClientsNum)
	for i := 0; i < ClientsNum; i++ {
		val, _ := climap.Get(allCliId[i])
		go func(cli *client.Client) {

			cli.Send(mex)
			ans := <-cli.IncomingList()
			assert.NotNil(ans, "Should receive an answer from List channel")

			assert.False(testutils.IsInList(cli.Id(), cli.List()))

			diff := testutils.DiffLists(allCliId, cli.List())
			assert.Len(diff, 1, "diff list should contain only the client ID")
			assert.Equal(cli.Id(), diff[0], "diff list should contain only the client ID")

			wg.Done()
		}(val.(*client.Client))
	}

	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 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")

}
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")
}
Exemple #7
0
func (c *Client) Connect(address string, port int) error {

	//already connected or pending connection
	if c.socket != nil {
		return errors.New("client connection already open")
	}

	//dial the connection to the server
	conn, err := net.Dial("tcp", address+":"+strconv.Itoa(port))
	if err != nil {
		return err
	}

	//enable connection on client
	c.socket = mexsocket.New(0, conn)

	go c.handleConnection()

	idMex := message.NewRequest(message.Identity)
	_, err = c.socket.Send(idMex)
	return err
}
)

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)
	}
}

func BenchmarkUint64ArrayToByteArray(b *testing.B) {
	for n := 0; n < b.N; n++ {
	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

//for benchmarks
var bServ, bCli *mexsocket.MexSocket

func BenchmarkSendRequest1(b *testing.B) {
	for n := 0; n < b.N; n++ {