guava ratelimit

package com.guavatest.utilites;

import com.google.common.util.concurrent.RateLimiter;
import org.junit.Test;

import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class RatelimitTest {

    /**
     * base on abstraction
     * only focus on use
     */

    /**
     * 参考自:
     * https://www.baeldung.com/guava-rate-limiter
     *
     * https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/util/concurrent/RateLimiter.html
     *
     */


    @Test
    public void test4(){
        RateLimiter rateLimiter = RateLimiter.create(2);
        final boolean b = rateLimiter.tryAcquire();
        doSomeLimitedOperation();
        try {
            Thread.sleep(499);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        final boolean b1 = rateLimiter.tryAcquire();
        doSomeLimitedOperation();
        final boolean b2 = rateLimiter.tryAcquire();
        doSomeLimitedOperation();
        System.out.println(b);
        System.out.println(b1);
        System.out.println(b2);

        rateLimiter.setRate(10);
        final double rate = rateLimiter.getRate();
        System.out.println(rate);
        doSomeLimitedOperation();
        final boolean b3 = rateLimiter.tryAcquire(9);
        doSomeLimitedOperation();
        final boolean b4 = rateLimiter.tryAcquire();
        doSomeLimitedOperation();
        final boolean b5 = rateLimiter.tryAcquire();
        doSomeLimitedOperation();
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
    }


    @Test
    public void test(){
        RateLimiter rateLimiter = RateLimiter.create(2);
        long startTime = ZonedDateTime.now().getSecond();
        rateLimiter.acquire(1);
        doSomeLimitedOperation();
        final boolean b = rateLimiter.tryAcquire();
        System.out.println(b);
        final double acquire = rateLimiter.acquire(1);
        System.out.println(acquire);
        doSomeLimitedOperation();
        long elapsedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;
        System.out.println(elapsedTimeSeconds);
    }

    @Test
    public void givenLimitedResource_whenRequestOnce_thenShouldPermitWithoutBlocking() {
        // given
        RateLimiter rateLimiter = RateLimiter.create(100);

        // when
        long startTime = ZonedDateTime.now().getSecond();
//        rateLimiter.acquire(100);
        rateLimiter.acquire(200);
        doSomeLimitedOperation();
        long elapsedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;

        // then
//        assertThat(elapsedTimeSeconds <= 1);
        System.out.println(elapsedTimeSeconds);
    }

    @Test
    public void givenLimitedResource_whenUseRateLimiter_thenShouldLimitPermits() {
        // given
        RateLimiter rateLimiter = RateLimiter.create(100);

        // when
        long startTime = ZonedDateTime.now().getSecond();
        IntStream.range(0, 1000).forEach(i -> {
            rateLimiter.acquire();
            doSomeLimitedOperation();
        });
        long elapsedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;

        // then
//        assertThat(elapsedTimeSeconds >= 10);
        System.out.println(elapsedTimeSeconds);
    }

    @Test
    public void givenLimitedResource_whenTryAcquire_shouldNotBlockIndefinitely() {
        // given
        RateLimiter rateLimiter = RateLimiter.create(1);

        // when
        rateLimiter.acquire();
        boolean result = rateLimiter.tryAcquire(2, 10, TimeUnit.MILLISECONDS);

        // then
//        assertThat(result).isFalse();
        System.out.println(result);
    }



    private void doSomeLimitedOperation() {
    }


}

你可能感兴趣的:(java,开发语言)