package com.jiaxy.tgd;

import org.junit.Test;

import java.util.Date;

/**
 * Title: <br>
 * <p>
 * Description: <br>
 * </p>
 * <br>
 *
 * @author <a href=mailto:taobaorun@gmail.com>taobaorun</a>
 *         <br>
 * @since 2016/04/26 22:19
 */
public class RateLimiterTest {


    @Test
    public void testTBGetToken() throws Exception {
        RateLimiter limiter = RateLimiter.builder().
                withToekPerSecond(1).
                withType(RateLimiter.RateLimiterType.TB).
                build();
        for ( int i = 0 ;i < 10;i++){
            limiter.getToken(1);
            System.out.println(i+".================="+new Date());
        }
    }

    @Test
    public void testFFTBGetToken() throws Exception {
        RateLimiter limiter = RateLimiter.builder().
                withToekPerSecond(1).
                withType(RateLimiter.RateLimiterType.FFTB).
                build();
        for ( int i = 0 ;i < 1000;i++){
            try {
                limiter.getToken(1);
                System.out.println(i+".================="+new Date());
            } catch (Exception e){
                Thread.sleep(500);
//                e.printStackTrace()
                System.out.println(e.getMessage());
            }
        }
    }
}

@@ -908,35 +908,36 @@
 ;%0A        for ( 
-int
+long
  i = 0 ;i %3C 1000
@@ -936,12 +936,22 @@
 i %3C 
-1000
+Long.MAX_VALUE
 ;i++
@@ -1235,24 +1235,24 @@
 Message());%0A
-
             
@@ -1268,11 +1268,934 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void testMultiFFBGetToken() throws Exception %7B%0A        final RateLimiter limiter = RateLimiter.builder().%0A                withToekPerSecond(1).%0A                withType(RateLimiter.RateLimiterType.FFTB).%0A                build();%0A        for ( int j = 0 ;j %3C 3;j++)%7B%0A             new Thread(new Runnable() %7B%0A                public void run() %7B%0A                     for ( long i = 0 ;i %3C Long.MAX_VALUE;i++)%7B%0A                        try %7B%0A                            limiter.getToken(1);%0A                            System.out.println(i+%22.=================%22+new Date());%0A                        %7D catch (Exception e)%7B%0A                            try %7B%0A                                Thread.sleep(500);%0A                            %7D catch (InterruptedException e1) %7B%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D).start();%0A        %7D%0A    %7D
 %0A%7D%0A

add fail fast token bucket