Search in sources :

Example 16 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ProtocolV1SerializerTest method testAll.

@Test
public void testAll() {
    ProtocolV1Server server = new ProtocolV1Server();
    ProtocolV1Client client = new ProtocolV1Client();
    try {
        server.start();
        client.connect("127.0.0.1", 8811, 500);
        Assertions.assertTrue(client.channel.isActive());
        Map<String, String> head = new HashMap<>();
        head.put("tracerId", "xxadadadada");
        head.put("token", "adadadad");
        head.put("hello", null);
        BranchCommitRequest body = new BranchCommitRequest();
        body.setBranchId(12345L);
        body.setApplicationData("application");
        body.setBranchType(BranchType.AT);
        body.setResourceId("resource-1234");
        body.setXid("xid-1234");
        // test run times
        int runTimes = 100000;
        final int threads = 50;
        final CountDownLatch cnt = new CountDownLatch(runTimes);
        final AtomicInteger tag = new AtomicInteger(0);
        final AtomicInteger success = new AtomicInteger(0);
        // no queue
        final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), new NamedThreadFactory("client-", false));
        for (int i = 0; i < threads; i++) {
            service1.execute(() -> {
                while (tag.getAndIncrement() < runTimes) {
                    try {
                        Future future = client.sendRpc(head, body);
                        RpcMessage resp = (RpcMessage) future.get(10, TimeUnit.SECONDS);
                        if (resp != null) {
                            success.incrementAndGet();
                        }
                    } catch (Exception e) {
                        LOGGER.error("Client send error", e);
                    } finally {
                        cnt.countDown();
                    }
                }
            });
        }
        cnt.await();
        LOGGER.info("success {}/{}", success.get(), runTimes);
        Assertions.assertEquals(success.get(), runTimes);
    } catch (InterruptedException e) {
        LOGGER.error("Thread interrupted", e);
    } finally {
        client.close();
        server.stop();
    }
}
Also used : HashMap(java.util.HashMap) NamedThreadFactory(io.seata.common.thread.NamedThreadFactory) BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) CountDownLatch(java.util.concurrent.CountDownLatch) RpcMessage(io.seata.core.protocol.RpcMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.jupiter.api.Test)

Aggregations

RpcMessage (io.seata.core.protocol.RpcMessage)16 Channel (io.netty.channel.Channel)6 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)3 HashMap (java.util.HashMap)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 DefaultPromise (io.netty.util.concurrent.DefaultPromise)2 FrameworkException (io.seata.common.exception.FrameworkException)2 Compressor (io.seata.core.compressor.Compressor)2 HeartbeatMessage (io.seata.core.protocol.HeartbeatMessage)2 MessageFuture (io.seata.core.protocol.MessageFuture)2 BranchCommitRequest (io.seata.core.protocol.transaction.BranchCommitRequest)2 Serializer (io.seata.core.serializer.Serializer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Future (java.util.concurrent.Future)2 TimeoutException (java.util.concurrent.TimeoutException)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 FrameworkErrorCode (io.seata.common.exception.FrameworkErrorCode)1 EnhancedServiceLoader (io.seata.common.loader.EnhancedServiceLoader)1