Untitled

 avatar
unknown
java
2 years ago
3.7 kB
3
Indexable
@Test
    @Disabled("Проверка проксей с двойным пингом и выводом времени ответа")
    void testHttpExecutor() throws InterruptedException {
        Config config = new Config(new String[]{"--env=local"});
        Postgres postgres = new Postgres(config);
        RedissonCacheProvider redissonCacheProvider = new RedissonCacheProvider(config, postgres);

        config.setProxyConnectionTimeoutMs(5000); // Connect timed out
        config.setProxyResponseTimeoutMs(600000); // Read timed out

        HttpExecutor httpExecutor = new HttpExecutor(config, redissonCacheProvider);

        Request request = Request.unsigned("https://api.binance.com", "/api/v3/ping");

        List<Proxy> proxies = redissonCacheProvider.getProxies();
        Proxy selected = proxies.stream().filter(proxy -> proxy.getHost().equalsIgnoreCase("88.99.95.69")).findFirst().orElse(null);

        AtomicInteger counter = new AtomicInteger(0);

        int iterNeed = 3000;

        for (int i = 0; i < iterNeed; i++) {
            Thread.sleep(20);
            int finalI = i;
            new Thread(() -> {
                try {
                    for (Proxy proxy : List.of(selected)) {
                        long start = System.currentTimeMillis();
                        ThreadTask thread = new ThreadTask(() -> {
                            try {
                                httpExecutor.get(request, proxy);
                            } catch (Exception ignored) {
                            }
                        });

                        thread.start();

                        while (true) {
                            if (thread.isEndTask()) {
                                int locCount = counter.incrementAndGet();

                                if (locCount >= iterNeed) {
                                    System.out.println("ALL DONE!!!!!!!!!!");
                                }
                                System.out.println(finalI + ". Time: " + (System.currentTimeMillis() - start) + " ms. Counter: " + counter.get());
                                break;
                            } else {
                                // Тут проверять что так долго
                                if (System.currentTimeMillis() - start > 5000) {
                                    InetAddress address = new InetSocketAddress(proxy.getHost(), proxy.getPort()).getAddress();
                                    boolean reachable = address.isReachable(3000);

                                    if (reachable) {
                                        System.out.println("Proxy server is reachable.");
                                        break;
                                    } else {
                                        System.out.println("Unable to reach the proxy server. Host: " + proxy.getHost());
                                    }
                                } else {
                                    //System.out.println("Wait...");
                                }
                            }

                            Thread.sleep(500);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

        Thread.sleep(2000000);
    }
}

class ThreadTask extends Thread {

    private final Runnable runnable;
    private boolean endTask = false;

    public ThreadTask(Runnable runnable) {
        this.runnable = runnable;
    }

    @Override
    public void run() {
        runnable.run();
        endTask = true;
    }

    public boolean isEndTask() {
        return endTask;
    }
}