use of io.vertx.core.net.NetSocket in project java-chassis by ServiceComb.
the class TestTcp method testTcpClient.
@Test
public void testTcpClient() throws Exception {
NetClient oNetClient = new NetClient() {
@Override
public boolean isMetricsEnabled() {
// TODO Auto-generated method stub
return true;
}
@Override
public NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler) {
return Mockito.mock(NetClient.class);
}
@Override
public void close() {
// TODO Auto-generated method stub
}
};
TcpClient oTcpClient = new TcpClient(Mockito.mock(Context.class), oNetClient, "highway://127.2.0.1:8080", new TcpClientConfig());
oTcpClient.checkTimeout();
oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
oTcpClient.send(new TcpOutputStream(), 123, Mockito.mock(TcpResonseCallback.class));
Assert.assertNotEquals(null, oTcpClient.getContext());
new MockUp<TcpClientPool>() {
@Mock
protected void startCheckTimeout(TcpClientConfig clientConfig, Context context) {
}
};
TcpClientConfig config = new TcpClientConfig();
TcpClientPool oClientPool = new TcpClientPool(config, Vertx.vertx().getOrCreateContext(), oNetClient);
oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
oClientPool.send("highway://152.2.2.3:8080", new TcpOutputStream(), Mockito.mock(TcpResonseCallback.class));
Assert.assertNotNull(oClientPool);
TcpRequest oTcpRequest = new TcpRequest(1234, Mockito.mock(TcpResonseCallback.class));
oTcpRequest.isTimeout();
oTcpRequest.onReply(Buffer.buffer(), Buffer.buffer(("test").getBytes()));
oTcpRequest.onSendError(new Throwable("test Errorsss"));
Assert.assertNotNull(oTcpRequest);
TcpClientVerticle oTcpClientVerticle = new TcpClientVerticle();
oTcpClientVerticle.init(Vertx.vertx(), Vertx.vertx().getOrCreateContext());
oTcpClientVerticle.createClientPool();
oTcpClientVerticle.createClientPool();
Assert.assertNotNull(oTcpClientVerticle.getVertx());
NetSocket socket = Mockito.mock(NetSocket.class);
Throwable e = Mockito.mock(Throwable.class);
Buffer hBuffer = Mockito.mock(Buffer.class);
Buffer bBuffer = Mockito.mock(Buffer.class);
Deencapsulation.invoke(oTcpClient, "connect");
Deencapsulation.invoke(oTcpClient, "onConnectSuccess", socket);
Mockito.when(socket.localAddress()).thenReturn(new SocketAddressImpl(0, "127.0.0.1"));
Deencapsulation.setField(oTcpClient, "netSocket", socket);
Deencapsulation.invoke(oTcpClient, "onDisconnected", e);
Deencapsulation.invoke(oTcpClient, "tryLogin");
Deencapsulation.invoke(oTcpClient, "onLoginSuccess");
Deencapsulation.invoke(oTcpClient, "onConnectFailed", e);
long l = 10;
Deencapsulation.invoke(oTcpClient, "onReply", l, hBuffer, bBuffer);
oTcpClient.checkTimeout();
Assert.assertNotNull(oTcpClient);
}
use of io.vertx.core.net.NetSocket in project java-chassis by ServiceComb.
the class TestHighwayServerConnection method testRequestError.
@Test
public void testRequestError() throws Exception {
header.setMsgType(MsgType.REQUEST);
Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
Buffer bodyBuffer = Buffer.buffer();
Holder<Boolean> holder = new Holder<>(false);
new MockUp<HighwayServerInvoke>() {
@Mock
public boolean init(NetSocket netSocket, long msgId, RequestHeader header, Buffer bodyBuffer) {
return false;
}
};
connection.handle(0, headerBuffer, bodyBuffer);
Assert.assertEquals(null, connection.getProtocol());
Assert.assertEquals(null, connection.getZipName());
Assert.assertEquals(false, holder.value);
}
use of io.vertx.core.net.NetSocket in project vert.x by eclipse.
the class WebsocketTest method testRaceConditionWithWebsocketClient.
private void testRaceConditionWithWebsocketClient(Context context) {
server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT));
// Handcrafted websocket handshake for sending a frame immediatly after the handshake
server.requestHandler(req -> {
byte[] accept;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] inputBytes = (req.getHeader("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes();
digest.update(inputBytes);
byte[] hashedBytes = digest.digest();
accept = Base64.getEncoder().encode(hashedBytes);
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
return;
}
NetSocket so = req.netSocket();
Buffer data = Buffer.buffer();
data.appendString("HTTP/1.1 101 Switching Protocols\r\n");
data.appendString("Upgrade: websocket\r\n");
data.appendString("Connection: upgrade\r\n");
data.appendString("Sec-WebSocket-Accept: " + new String(accept) + "\r\n");
data.appendString("\r\n");
data.appendBytes(new byte[] { (byte) 0x82, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f });
so.write(data);
});
server.listen(ar -> {
assertTrue(ar.succeeded());
context.runOnContext(v -> {
client.websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {
ws.handler(buf -> {
assertEquals("hello", buf.toString());
testComplete();
});
});
});
});
await();
}
use of io.vertx.core.net.NetSocket in project vert.x by eclipse.
the class WebsocketTest method testContinuationWriteFromConnectHandler.
private void testContinuationWriteFromConnectHandler(WebsocketVersion version) throws Exception {
String path = "/some/path";
String firstFrame = "AAA";
String continuationFrame = "BBB";
server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)).requestHandler(req -> {
NetSocket sock = getUpgradedNetSocket(req, path);
Buffer buff = Buffer.buffer();
buff.appendByte((byte) 0x01);
buff.appendByte((byte) firstFrame.length());
buff.appendString(firstFrame);
sock.write(buff);
buff = Buffer.buffer();
buff.appendByte((byte) (0x00 | 0x80));
buff.appendByte((byte) continuationFrame.length());
buff.appendString(continuationFrame);
sock.write(buff);
});
server.listen(ar -> {
assertTrue(ar.succeeded());
client.websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> {
AtomicBoolean receivedFirstFrame = new AtomicBoolean();
ws.frameHandler(received -> {
Buffer receivedBuffer = Buffer.buffer(received.textData());
if (!received.isFinal()) {
assertEquals(firstFrame, receivedBuffer.toString());
receivedFirstFrame.set(true);
} else if (receivedFirstFrame.get() && received.isFinal()) {
assertEquals(continuationFrame, receivedBuffer.toString());
ws.close();
testComplete();
}
});
});
});
await();
}
use of io.vertx.core.net.NetSocket in project vert.x by eclipse.
the class WebsocketTest method getUpgradedNetSocket.
private NetSocket getUpgradedNetSocket(HttpServerRequest req, String path) {
assertEquals(path, req.path());
assertEquals("upgrade", req.headers().get("Connection"));
NetSocket sock = req.netSocket();
String secHeader = req.headers().get("Sec-WebSocket-Key");
String tmp = secHeader + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String encoded = sha1(tmp);
sock.write("HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: WebSocket\r\n" + "Connection: upgrade\r\n" + "Sec-WebSocket-Accept: " + encoded + "\r\n" + "\r\n");
return sock;
}
Aggregations