Exemplo n.º 1
0
func TestRelayTwo(t *testing.T) {

	if len(allCliId) < 2 {
		return
	}

	val, _ := climap.Get(allCliId[0])
	c1 := val.(*client.Client)
	val, _ = climap.Get(allCliId[1])
	c2 := val.(*client.Client)

	var wg sync.WaitGroup
	wg.Add(MessagesPerCLient)
	var counter int

	req := message.NewRelayRequest([]uint64{c2.Id()}, testBody)
	buf := make([]byte, 8)
	for i := 0; i < MessagesPerCLient; i++ {

		message.Uint64ToByteArray(buf, uint64(i))
		req.Body = buf
		c1.Send(req)
	}

	for i := 0; i < MessagesPerCLient; i++ {

		counter++
		wg.Done()
	}

	wg.Wait()
	assert.Equal(t, MessagesPerCLient, counter, "Client2 should have received all messages")

}
Exemplo n.º 2
0
func TestClearRequest(t *testing.T) {

	m := message.NewRelayRequest(testList, testBody)
	m.Clear()

	assert.Equal(t, m.Type(), message.Empty, "Cleared message type should be Empty")
	assert.Nil(t, m.Receivers, "Cleared Receivers should be Nil")
	assert.Nil(t, m.Body, "Cleared Body should be Nil")
}
Exemplo n.º 3
0
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")
}
Exemplo n.º 4
0
func writeLoop(cli *client.Client, finish *sync.WaitGroup, ready *sync.WaitGroup) {

	list := cli.List()
	if len(list) > 255 {
		list = list[:255]
	}
	binary := message.NewRelayRequest(list, payload).ToByteArray()
	ready.Wait()

	for i := 0; i < mexNum; i++ {
		cli.SendBytes(binary)
		if showStat {
			allStats.OutgoingMessages.Increase(1)
		}
		time.Sleep(interval)
	}
	finish.Done()
}
Exemplo n.º 5
0
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")

}
Exemplo n.º 6
0
func ForwardAndListen(cli *client.Client) int {

	var received int
	req := message.NewRelayRequest(cli.List(), testBody)
	incoming := cli.IncomingRelay()
	var expectedIncoming int = MessagesPerCLient * (ClientsNum - 1)

	for i := 0; i < MessagesPerCLient; i++ {
		go cli.Send(req)
	}

	for i := 0; i < expectedIncoming; i++ {
		select {
		case <-incoming:
			received++
		case <-time.After(TimeoutTime):
			log.Println(cli.Id(), "timed out")
			return received
		}
	}

	return received

}
Exemplo n.º 7
0
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++ {
		message.ByteArrayToUint64Array(testListByte)