@Test public void shouldEncodeAndDecodeClientMessageCorrectly_withPayLoadData_multipleMessages() throws UnsupportedEncodingException { SafeBuffer byteBuffer = new SafeBuffer(new byte[1024]); ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0); cmEncode .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(1) .setPartitionId(77); cmEncode.set(BYTE_DATA); cmEncode.updateFrameLength(); final int calculatedFrame1Size = ClientMessage.HEADER_SIZE + ParameterUtil.calculateByteArrayDataSize(BYTE_DATA); final int nexMessageOffset = cmEncode.getFrameLength(); ClientMessage cmEncode2 = TestClientMessage.createForEncode(byteBuffer, nexMessageOffset); cmEncode2 .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(2) .setPartitionId(77); cmEncode2.set(BYTE_DATA); cmEncode2.updateFrameLength(); final int calculatedFrame2Size = ClientMessage.HEADER_SIZE + ParameterUtil.calculateByteArrayDataSize(BYTE_DATA); ClientMessage cmDecode1 = ClientMessage.createForDecode(byteBuffer, 0); final byte[] cmDecodeVarData = cmDecode1.getByteArray(); assertEquals(7, cmDecode1.getMessageType()); assertEquals(3, cmDecode1.getVersion()); assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode1.getFlags()); assertEquals(1, cmDecode1.getCorrelationId()); assertEquals(77, cmDecode1.getPartitionId()); assertEquals(calculatedFrame1Size, cmDecode1.getFrameLength()); assertArrayEquals(cmDecodeVarData, BYTE_DATA); ClientMessage cmDecode2 = ClientMessage.createForDecode(byteBuffer, cmDecode1.getFrameLength()); byte[] cmDecodeVarData2 = cmDecode2.getByteArray(); assertEquals(7, cmDecode2.getMessageType()); assertEquals(3, cmDecode2.getVersion()); assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode2.getFlags()); assertEquals(2, cmDecode2.getCorrelationId()); assertEquals(77, cmDecode2.getPartitionId()); assertEquals(calculatedFrame2Size, cmDecode2.getFrameLength()); assertArrayEquals(cmDecodeVarData2, BYTE_DATA); }
@Test public void shouldEncodeWithOldVersionAndDecodeWithNewVersionCorrectly_withPayLoadData() throws UnsupportedEncodingException { SafeBuffer byteBuffer = new SafeBuffer(new byte[1024]); ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0); cmEncode .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(66) .setPartitionId(77); final int calculatedFrameSize = ClientMessage.HEADER_SIZE + ParameterUtil.calculateByteArrayDataSize(BYTE_DATA); cmEncode.set(BYTE_DATA); cmEncode.updateFrameLength(); ClientMessage cmDecode = FutureClientMessage.createForDecode(byteBuffer, 0); final byte[] cmDecodeVarData1 = cmDecode.getByteArray(); assertEquals(7, cmDecode.getMessageType()); assertEquals(3, cmDecode.getVersion()); assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode.getFlags()); assertEquals(66, cmDecode.getCorrelationId()); assertEquals(77, cmDecode.getPartitionId()); assertEquals(calculatedFrameSize, cmDecode.getFrameLength()); assertArrayEquals(cmDecodeVarData1, BYTE_DATA); }
@Test public void shouldEncodeAndDecodeClientMessageCorrectly_withPayLoadData_fromOffset() throws UnsupportedEncodingException { SafeBuffer byteBuffer = new SafeBuffer(new byte[150]); int offset = 100; ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, offset); cmEncode .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(66) .setPartitionId(77); byte[] bytes = VAR_DATA_STR_1.getBytes(); final int calculatedFrameSize = ClientMessage.HEADER_SIZE + Bits.INT_SIZE_IN_BYTES + ParameterUtil.calculateByteArrayDataSize(bytes); cmEncode.set(1); cmEncode.set(bytes); cmEncode.updateFrameLength(); ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, offset); assertEquals(1, cmDecode.getInt()); assertArrayEquals(bytes, cmDecode.getByteArray()); assertEquals(calculatedFrameSize, cmDecode.getFrameLength()); }
@Test public void shouldEncodeAndDecodeClientMessageCorrectly_withPayLoadData() throws UnsupportedEncodingException { SafeBuffer byteBuffer = new SafeBuffer(new byte[1024]); ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0); cmEncode .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(66) .setPartitionId(77); final byte[] data1 = VAR_DATA_STR_1.getBytes(DEFAULT_ENCODING); final int calculatedFrameSize = ClientMessage.HEADER_SIZE + ParameterUtil.calculateByteArrayDataSize(data1); cmEncode.set(data1); cmEncode.updateFrameLength(); ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0); byte[] cmDecodeVarData1 = cmDecode.getByteArray(); assertEquals(calculatedFrameSize, cmDecode.getFrameLength()); assertArrayEquals(cmDecodeVarData1, data1); }
@Test public void shouldEncodeDecodeCorrectly_PUT() { final int calculatedSize = MapPutCodec.RequestParameters.calculateDataSize(NAME, DATA, DATA, THE_LONG, THE_LONG); ClientMessage cmEncode = MapPutCodec.encodeRequest(NAME, DATA, DATA, THE_LONG, THE_LONG); cmEncode .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(Long.MAX_VALUE) .setPartitionId(77); assertTrue(calculatedSize > cmEncode.getFrameLength()); byteBuffer = cmEncode.buffer(); ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0); MapPutCodec.RequestParameters decodeParams = MapPutCodec.decodeRequest(cmDecode); assertEquals(MapPutCodec.REQUEST_TYPE.id(), cmDecode.getMessageType()); assertEquals(3, cmDecode.getVersion()); assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode.getFlags()); assertEquals(Long.MAX_VALUE, cmDecode.getCorrelationId()); assertEquals(77, cmDecode.getPartitionId()); assertEquals(NAME, decodeParams.name); assertEquals(DATA, decodeParams.key); assertEquals(DATA, decodeParams.value); assertEquals(THE_LONG, decodeParams.threadId); assertEquals(THE_LONG, decodeParams.ttl); }
@Test public void shouldAccumulateClientMessageCorrectly() { ClientMessage accumulator = ClientMessage.create(); final ByteBuffer inBuffer = ByteBuffer.wrap(BYTE_DATA); accumulator.readFrom(inBuffer); final ByteBuffer byteBuffer = accumulatedByteBuffer(accumulator.buffer(), accumulator.index()); assertEquals(0, byteBuffer.position()); assertEquals(accumulator.getFrameLength(), byteBuffer.limit()); for (int i = 0; i < byteBuffer.limit(); i++) { assertEquals(BYTE_DATA[i], byteBuffer.get()); } assertTrue(accumulator.isComplete()); }
@Test public void shouldEncodeAndDecodeClientMessageCorrectly() { SafeBuffer byteBuffer = new SafeBuffer(new byte[512]); ClientMessage cmEncode = TestClientMessage.createForEncode(byteBuffer, 0); cmEncode .setMessageType(7) .setVersion((short) 3) .addFlag(ClientMessage.BEGIN_AND_END_FLAGS) .setCorrelationId(66) .setPartitionId(77); ClientMessage cmDecode = ClientMessage.createForDecode(byteBuffer, 0); assertEquals(7, cmDecode.getMessageType()); assertEquals(3, cmDecode.getVersion()); assertEquals(ClientMessage.BEGIN_AND_END_FLAGS, cmDecode.getFlags()); assertEquals(66, cmDecode.getCorrelationId()); assertEquals(77, cmDecode.getPartitionId()); assertEquals(ClientMessage.HEADER_SIZE, cmDecode.getFrameLength()); }