/** * Creates test client. * * @param sock Socket to use. * @param addr Address to connect to. * @param port Port to connect to. * @param connTimeout Connection timeout. * @throws IgniteCheckedException If connect failed. */ private TestClient(Socket sock, InetAddress addr, int port, int connTimeout) throws IgniteCheckedException { this.sock = sock; try { sock.connect(new InetSocketAddress(addr, port), connTimeout); if (sock instanceof SSLSocket) ((SSLSocket)sock).startHandshake(); out = sock.getOutputStream(); in = sock.getInputStream(); } catch (IOException e) { close(); throw new IgniteCheckedException(e); } }
@Override public void run() { TestClient client = null; try { client = createClient(U.getLocalHost(), srvr.port(), U.getLocalHost()); while (cntr.getAndIncrement() < MSG_CNT * THREAD_CNT) { MessageWithId msg = new MessageWithId(idProvider.getAndIncrement()); byte[] data = serializeMessage(msg); long start = System.currentTimeMillis(); deliveryDurations.put(msg.getId(), start); client.sendMessage(data, data.length); long end = System.currentTimeMillis(); sndTimes.put(msg.getId(), end - start); } } catch (Exception e) { error("Failed to send message.", e); assert false : "Message sending failed: " + e; } finally { if (client != null) client.close(); } }
client.close();
/** * @throws Exception If test failed. */ @Test public void testSendReceive() throws Exception { CountDownLatch latch = new CountDownLatch(10); NioListener lsnr = new NioListener(latch); GridNioServer<?> srvr = startServer(new GridBufferedParser(true, ByteOrder.nativeOrder()), lsnr); TestClient client = null; try { for (int i = 0; i < 5; i++) { client = createClient(U.getLocalHost(), srvr.port(), U.getLocalHost()); client.sendMessage(createMessage(), MSG_SIZE); client.sendMessage(createMessage(), MSG_SIZE); client.close(); } assert latch.await(30, SECONDS); assertEquals("Unexpected message count", 10, lsnr.getMessageCount()); } finally { srvr.stop(); if (client != null) client.close(); } }
@Override public void run() { TestClient client = null; try { client = createClient(U.getLocalHost(), srvr.port(), U.getLocalHost()); for (int i = 0; i < MSG_CNT; i++) client.sendMessage(data, data.length); } catch (Exception e) { error("Failed to send message.", e); assert false : "Message sending failed: " + e; } finally { if (client != null) client.close(); } }