use of org.apache.ignite.internal.util.nio.impl.MockNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testSimplePacketParsing.
/**
* @throws Exception If failed.
*/
@Test
public void testSimplePacketParsing() throws Exception {
GridNioSession ses = new MockNioSession();
GridTcpRestParser parser = new GridTcpRestParser(false);
byte hdr = MEMCACHE_REQ_FLAG;
byte[] opCodes = { 0x01, 0x02, 0x03 };
byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
String key = "key";
String val = "value";
for (byte opCode : opCodes) {
ByteBuffer raw = rawPacket(hdr, opCode, opaque, key.getBytes(), val.getBytes(), EXTRAS);
GridClientMessage msg = parser.decode(ses, raw);
assertTrue(msg instanceof GridMemcachedMessage);
GridMemcachedMessage packet = (GridMemcachedMessage) msg;
assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
assertEquals("Invalid opcode", opCode, packet.operationCode());
assertEquals("Invalid key", key, packet.key());
assertEquals("Invalid value", val, packet.value());
}
}
use of org.apache.ignite.internal.util.nio.impl.MockNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testMixedParsing.
/**
* @throws Exception If failed.
*/
@Test
public void testMixedParsing() throws Exception {
GridNioSession ses1 = new MockNioSession();
GridNioSession ses2 = new MockNioSession();
ses1.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
ses2.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
GridClientCacheRequest req = new GridClientCacheRequest(CAS);
req.key("key");
String val = "value";
req.value(val);
req.value2(val);
req.clientId(UUID.randomUUID());
byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
String key = "key";
ByteBuffer raw1 = rawPacket(MEMCACHE_REQ_FLAG, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), EXTRAS);
ByteBuffer raw2 = clientRequestPacket(req);
raw1.mark();
raw2.mark();
int splits = Math.min(raw1.remaining(), raw2.remaining());
for (int i = 1; i < splits; i++) {
ByteBuffer[] packet1 = split(raw1, i);
ByteBuffer[] packet2 = split(raw2, i);
GridClientMessage msg = parser.decode(ses1, packet1[0]);
assertNull(msg);
msg = parser.decode(ses2, packet2[0]);
assertNull(msg);
msg = parser.decode(ses1, packet1[1]);
assertTrue(msg instanceof GridMemcachedMessage);
assertEquals(key, ((GridMemcachedMessage) msg).key());
assertEquals(val, ((GridMemcachedMessage) msg).value());
msg = parser.decode(ses2, packet2[1]);
assertTrue(msg instanceof GridClientCacheRequest);
assertEquals(val, ((GridClientCacheRequest) msg).value());
assertEquals(val, ((GridClientCacheRequest) msg).value2());
raw1.reset();
raw2.reset();
}
}
use of org.apache.ignite.internal.util.nio.impl.MockNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testParseContinuousSplit.
/**
* @throws Exception If failed.
*/
@Test
public void testParseContinuousSplit() throws Exception {
ByteBuffer tmp = ByteBuffer.allocate(10 * 1024);
GridClientCacheRequest req = new GridClientCacheRequest(CAS);
req.key("key");
req.value(1);
req.value2(2);
req.clientId(UUID.randomUUID());
for (int i = 0; i < 5; i++) tmp.put(clientRequestPacket(req));
tmp.flip();
for (int splitPos = 0; splitPos < tmp.remaining(); splitPos++) {
ByteBuffer[] split = split(tmp, splitPos);
tmp.flip();
GridNioSession ses = new MockNioSession();
ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
Collection<GridClientCacheRequest> lst = new ArrayList<>(5);
for (ByteBuffer buf : split) {
GridClientCacheRequest r;
while (buf.hasRemaining() && (r = (GridClientCacheRequest) parser.decode(ses, buf)) != null) lst.add(r);
assertTrue("Parser has left unparsed bytes.", buf.remaining() == 0);
}
assertEquals(5, lst.size());
for (GridClientCacheRequest res : lst) {
assertEquals("Invalid operation", req.operation(), res.operation());
assertEquals("Invalid clientId", req.clientId(), res.clientId());
assertEquals("Invalid key", req.key(), res.key());
assertEquals("Invalid value 1", req.value(), res.value());
assertEquals("Invalid value 2", req.value2(), res.value2());
}
}
}
use of org.apache.ignite.internal.util.nio.impl.MockNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testIncorrectPackets.
/**
* @throws Exception If failed.
*/
@Test
public void testIncorrectPackets() throws Exception {
final GridNioSession ses = new MockNioSession();
final GridTcpRestParser parser = new GridTcpRestParser(false);
final byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
final String key = "key";
final String val = "value";
GridTestUtils.assertThrows(log(), new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
parser.decode(ses, rawPacket((byte) 0x01, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), EXTRAS));
return null;
}
}, IOException.class, null);
GridTestUtils.assertThrows(log(), new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
parser.decode(ses, rawPacket(MEMCACHE_REQ_FLAG, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), null));
return null;
}
}, IOException.class, null);
GridTestUtils.assertThrows(log(), new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
ByteBuffer fake = ByteBuffer.allocate(21);
fake.put(IGNITE_REQ_FLAG);
fake.put(U.intToBytes(-5));
fake.put(U.longToBytes(0));
fake.put(U.longToBytes(0));
fake.flip();
parser.decode(ses, fake);
return null;
}
}, IOException.class, null);
}
use of org.apache.ignite.internal.util.nio.impl.MockNioSession in project ignite by apache.
the class TcpRestParserSelfTest method testParseClientHandshake.
/**
* Tests correct parsing of client handshake packets.
*
* @throws Exception If failed.
*/
@Test
public void testParseClientHandshake() throws Exception {
for (int splitPos = 1; splitPos < 5; splitPos++) {
log.info("Checking split position: " + splitPos);
ByteBuffer tmp = clientHandshakePacket();
ByteBuffer[] split = split(tmp, splitPos);
GridNioSession ses = new MockNioSession();
ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
Collection<GridClientMessage> lst = new ArrayList<>(1);
for (ByteBuffer buf : split) {
GridClientMessage r;
while (buf.hasRemaining() && (r = parser.decode(ses, buf)) != null) lst.add(r);
assertTrue("Parser has left unparsed bytes.", buf.remaining() == 0);
}
assertEquals(1, lst.size());
GridClientHandshakeRequest req = (GridClientHandshakeRequest) F.first(lst);
assertNotNull(req);
assertEquals(U.bytesToShort(new byte[] { 5, 0 }, 0), req.version());
}
}
Aggregations