func TestConversion(t *testing.T) { buf := make([]byte, 8) message.Uint64ToByteArray(buf, testId) id := message.ByteArrayToUint64(buf) assert.Equal(t, testId, id, "uint64 conversion is wrong!") buf = make([]byte, 4) message.Uint32ToByteArray(buf, testNum) n := message.ByteArrayToUint32(buf) assert.Equal(t, testNum, n, "uint32 conversion is wrong!") a1 := message.Uint64ArrayToByteArray(nil) a2 := message.Uint64ArrayToByteArray(make([]uint64, 0)) a3 := message.Uint64ArrayToByteArray(testList) assert.Nil(t, a1, "Should return nil with nil as input") assert.Len(t, a2, 0, "Should give empty byte list") assert.Len(t, a3, len(testList)*8, "One element takes 8 bytes of space") l1 := message.ByteArrayToUint64Array(nil) l2 := message.ByteArrayToUint64Array(make([]byte, 7)) l3 := message.ByteArrayToUint64Array(make([]byte, 0)) l4 := message.ByteArrayToUint64Array(a3) assert.Nil(t, l1, "Should return nil with nil as input") assert.Nil(t, l2, "Should return nil with incorrect length") assert.Len(t, l3, 0, "Should give empty byte list") assert.Nil(t, testutils.CompareList(l4, testList), "Lists should be the same") }
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") }
func convertSetList(list []interface{}) []byte { out := make([]byte, 0, len(list)*8) //temporary container for conversion uint64 --> [8]byte buff := make([]byte, 8, 8) //one by one convert the receiver ID into bytearray and append it to the byte message for _, el := range list { //BigEndian implementation ensures a 8-byte conversion message.Uint64ToByteArray(buff, el.(uint64)) out = append(out, buff...) } return out }