一个循环测试grpc接口脚本

  //这行根据需求发挥
  ExecutorService cachedPool = new ThreadPoolExecutor(6, 1000,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(true));

    ConcurrentLinkedQueue<FutureTask<?>> queue=new ConcurrentLinkedQueue<>();

    public  CheckLoginResponse testIt(){
        XxxRequest build = XxxRequest.newBuilder()

                .build();
        String host="xxxxxxxxx";
        int port=7777;
        ManagedChannel channel= ManagedChannelBuilder.forAddress(host,port).usePlaintext().build();
        final XXXServiceGrpc.XXXServiceBlockingStub xxxServiceBlockingStub =
                XxxServiceGrpc.newBlockingStub(
                channel);
        return xxxServiceBlockingStub.checkXxx(build);

    }

    @Test
    public  void tet() throws InterruptedException {
        CountDownLatch countDownLatch=new  CountDownLatch(1);
        final Thread thread = new Thread(() -> {
            while (true) {
                FutureTask<?> futureTask = new FutureTask<>(this::testIt);
                cachedPool.submit(futureTask);
                queue.offer(futureTask);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
        });
        final Thread thread1 = new Thread(() -> {
            while (true) {
                if (queue.peek() != null) {
                    FutureTask<?> poll = queue.poll();
                    try {
                        System.out.println(poll.get());
                    } catch (InterruptedException | ExecutionException e) {
                        System.out.println(e);
                    }
                }
                try {
                    Thread.sleep(900);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
        });
        thread.start();
        thread1.start();

        countDownLatch.await();
    }
posted @ 2023-09-06 16:57  大哥超帅  阅读(15)  评论(0编辑  收藏  举报