com.pepaproch.gtswsdl.client.RateLimitTest.java Source code

Java tutorial

Introduction

Here is the source code for com.pepaproch.gtswsdl.client.RateLimitTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pepaproch.gtswsdl.client;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.util.concurrent.ListenableFutureTask;

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import static junit.framework.Assert.assertEquals;

/**
 *
 * @author pepa
 */
public class RateLimitTest {

    public RateLimitTest() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    /**
     * Test of consumeSlot method, of class RateLimit.
     */
    @Test
    public void testConsumeSlot() {
        //        System.out.println("consumeSlot");
        //        RateLimit instance = new RateLimit(3, 3, ChronoUnit.SECONDS);
        //        Long consumeSlot1 = instance.consumeSlot();
        //        Long exp1 = 0l;
        //        assertEquals(consumeSlot1, exp1);
        //        Long consumeSlot2 = instance.consumeSlot();
        //        Long exp2 = 0l;
        //        assertEquals(consumeSlot2, exp2);
        //        Long consumeSlot3 = instance.consumeSlot();
        //        Long exp3 = 0l;
        //        assertEquals(consumeSlot3, exp3);
        //        Long consumeSlot4 = instance.consumeSlot();
        //        Long exp4 = 0l;

    }

    @Test
    public void testCOnsumeSLotAsync() {
        RateLimit rate = new RateLimit(5, 10, ChronoUnit.SECONDS);
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
        final AtomicInteger cc = new AtomicInteger(0);
        Instant start = Instant.now();
        final Instant[] end = new Instant[1];

        Runnable r = () -> {
            for (int i = 0; i < 21; i++) {
                addTask(cc, scheduler, rate, end);
            }
        };
        Runnable r1 = () -> {
            for (int i = 0; i < 9; i++) {
                addTask(cc, scheduler, rate, end);

            }
        };

        r1.run();
        r.run();

        try {

            scheduler.awaitTermination(5, TimeUnit.SECONDS);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Duration.between(start, end[0]).toMillis());
    }

    private synchronized void addTask(AtomicInteger cc, ScheduledExecutorService schelduler, RateLimit rate,
            Instant[] end) {

        Callable<Integer> callable = (Callable<Integer>) () -> {

            return cc.get();
        };
        ListenableFutureTask request = new ListenableFutureTask(callable);

        schelduler.schedule(() -> {
            FutureTask<?> schelduledTask = request;
            if (!request.isCancelled() && !request.isDone()) {
                schelduledTask.run();
            }

        }, rate.consumeSlot(), TimeUnit.MILLISECONDS);

        request.addCallback(new ListenableFutureCallback<Integer>() {

            @Override
            public void onSuccess(Integer result) {
                cc.incrementAndGet();
                end[0] = Instant.now();
                System.out.println("FINISHEDLISTENBALE: " + result + end[0].toString());
            }

            @Override
            public void onFailure(Throwable ex) {
                System.out.println("FAILURE");
            }
        });

    }

}