Search in sources :

Example 1 with TcpOutputStream

use of io.servicecomb.foundation.vertx.tcp.TcpOutputStream 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);
}
Also used : NetSocket(io.vertx.core.net.NetSocket) Context(io.vertx.core.Context) Buffer(io.vertx.core.buffer.Buffer) TcpClientVerticle(io.servicecomb.foundation.vertx.client.tcp.TcpClientVerticle) TcpClientConfig(io.servicecomb.foundation.vertx.client.tcp.TcpClientConfig) Handler(io.vertx.core.Handler) MockUp(mockit.MockUp) TcpClientPool(io.servicecomb.foundation.vertx.client.tcp.TcpClientPool) TcpResonseCallback(io.servicecomb.foundation.vertx.client.tcp.TcpResonseCallback) NetClient(io.vertx.core.net.NetClient) TcpRequest(io.servicecomb.foundation.vertx.client.tcp.TcpRequest) TcpClient(io.servicecomb.foundation.vertx.client.tcp.TcpClient) SocketAddressImpl(io.vertx.core.net.impl.SocketAddressImpl) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 2 with TcpOutputStream

use of io.servicecomb.foundation.vertx.tcp.TcpOutputStream in project java-chassis by ServiceComb.

the class TcpClientTest method testCallBack.

@Test
public void testCallBack() {
    instance.getClass();
    TcpResonseCallback callback = Mockito.mock(TcpResonseCallback.class);
    try {
        instance.send(new TcpOutputStream(), 1, callback);
        Assert.assertNotNull(callback);
    } catch (Exception e) {
        Assert.assertEquals("java.lang.NullPointerException", e.getClass().getName());
    }
}
Also used : TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 3 with TcpOutputStream

use of io.servicecomb.foundation.vertx.tcp.TcpOutputStream in project java-chassis by ServiceComb.

the class TestTcpParser method test.

@Test
public void test() throws UnsupportedEncodingException {
    TcpBufferHandler output = new TcpBufferHandler() {

        @Override
        public void handle(long _msgId, Buffer _headerBuffer, Buffer _bodyBuffer) {
            msgId = _msgId;
            headerBuffer = _headerBuffer;
            bodyBuffer = _bodyBuffer;
        }
    };
    byte[] header = new byte[] { 1, 2, 3 };
    byte[] body = new byte[] { 1, 2, 3, 4 };
    TcpOutputStream os = new TcpOutputStream(1);
    os.writeInt(header.length + body.length);
    os.writeInt(header.length);
    os.write(header);
    os.write(body);
    TcpParser parser = new TcpParser(output);
    parser.handle(os.getBuffer());
    os.close();
    Assert.assertEquals(1, msgId);
    Assert.assertArrayEquals(header, headerBuffer.getBytes());
    Assert.assertArrayEquals(body, bodyBuffer.getBytes());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Test(org.junit.Test)

Example 4 with TcpOutputStream

use of io.servicecomb.foundation.vertx.tcp.TcpOutputStream in project java-chassis by ServiceComb.

the class TestHighwayClient method testCreateLogin.

@Test
public void testCreateLogin() throws Exception {
    HighwayClient client = new HighwayClient(false);
    TcpOutputStream os = client.createLogin();
    ByteBuf buf = os.getBuffer().getByteBuf();
    byte[] magic = new byte[TcpParser.TCP_MAGIC.length];
    buf.readBytes(magic);
    Assert.assertArrayEquals(TcpParser.TCP_MAGIC, magic);
    Assert.assertEquals(os.getMsgId(), buf.readLong());
    int start = TcpParser.TCP_HEADER_LENGTH;
    int totalLen = buf.readInt();
    int headerLen = buf.readInt();
    Buffer headerBuffer = os.getBuffer().slice(start, start + headerLen);
    int end = start + totalLen;
    start += headerLen;
    Buffer bodyBuffer = os.getBuffer().slice(start, end);
    RequestHeader header = RequestHeader.readObject(headerBuffer);
    Assert.assertEquals(MsgType.LOGIN, header.getMsgType());
    LoginRequest login = LoginRequest.readObject(bodyBuffer);
    Assert.assertEquals(HighwayTransport.NAME, login.getProtocol());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest) ByteBuf(io.netty.buffer.ByteBuf) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Endpoint(io.servicecomb.core.Endpoint) Test(org.junit.Test)

Example 5 with TcpOutputStream

use of io.servicecomb.foundation.vertx.tcp.TcpOutputStream in project java-chassis by ServiceComb.

the class HighwayClient method send.

public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    TcpClientPool tcpClientPool = clientMgr.findThreadBindClientPool();
    OperationMeta operationMeta = invocation.getOperationMeta();
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    TcpOutputStream os = HighwayCodec.encodeRequest(invocation, operationProtobuf);
    tcpClientPool.send(invocation.getEndpoint().getEndpoint(), os, ar -> {
        invocation.getResponseExecutor().execute(() -> {
            if (ar.failed()) {
                asyncResp.consumerFail(ar.cause());
                return;
            }
            try {
                Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result());
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.consumerFail(e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) Response(io.servicecomb.core.Response) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) OperationMeta(io.servicecomb.core.definition.OperationMeta) TcpClientPool(io.servicecomb.foundation.vertx.client.tcp.TcpClientPool) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream)

Aggregations

TcpOutputStream (io.servicecomb.foundation.vertx.tcp.TcpOutputStream)8 Test (org.junit.Test)6 Buffer (io.vertx.core.buffer.Buffer)4 TcpClientPool (io.servicecomb.foundation.vertx.client.tcp.TcpClientPool)2 ByteBuf (io.netty.buffer.ByteBuf)1 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Endpoint (io.servicecomb.core.Endpoint)1 Response (io.servicecomb.core.Response)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 TcpClient (io.servicecomb.foundation.vertx.client.tcp.TcpClient)1 TcpClientConfig (io.servicecomb.foundation.vertx.client.tcp.TcpClientConfig)1 TcpClientVerticle (io.servicecomb.foundation.vertx.client.tcp.TcpClientVerticle)1 TcpRequest (io.servicecomb.foundation.vertx.client.tcp.TcpRequest)1 TcpResonseCallback (io.servicecomb.foundation.vertx.client.tcp.TcpResonseCallback)1 LoginRequest (io.servicecomb.transport.highway.message.LoginRequest)1 RequestHeader (io.servicecomb.transport.highway.message.RequestHeader)1 Context (io.vertx.core.Context)1 Handler (io.vertx.core.Handler)1 NetClient (io.vertx.core.net.NetClient)1