<commit_before>package com.yuzhouwan.bigdata.redis.rate.limit;

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

import static org.junit.Assert.assertEquals;

/**
 * Copyright @ 2018 yuzhouwan.com
 * All right reserved.
 * Function：Guava Rate Limiter Test
 *
 * @author <NAME>
 * @since 2018/1/9
 */
public class GuavaRateLimiterTest {

    private static final int PERMITS_PER_SECOND = 2;
    private static final int PERMITS_NEEDS_PER_OPERATION = 1;
    private static final int SHOULD_WAIT_MILLISECONDS = 1000;

    @Test
    public void normal() {
        RateLimiter rateLimiter = RateLimiter.create(PERMITS_PER_SECOND);
        long startTime = System.currentTimeMillis();
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 1.");
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 2.");
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 3.");
        long usedTime = System.currentTimeMillis() - startTime;
        System.out.println(usedTime + " ms");
        assertEquals(true, usedTime >= SHOULD_WAIT_MILLISECONDS);
        System.out.println(String.format("%s >= %s" + " ms", usedTime, SHOULD_WAIT_MILLISECONDS));
    }
}<commit_msg>Add fault tolerant time into GuavaRateLimiter testcase<commit_after>package com.yuzhouwan.bigdata.redis.rate.limit;

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

import static org.junit.Assert.assertEquals;

/**
 * Copyright @ 2018 yuzhouwan.com
 * All right reserved.
 * Function：Guava Rate Limiter Test
 *
 * @author <NAME>
 * @since 2018/1/9
 */
public class GuavaRateLimiterTest {

    private static final int PERMITS_PER_SECOND = 2;
    private static final int PERMITS_NEEDS_PER_OPERATION = 1;
    private static final int SHOULD_WAIT_MILLISECONDS = 1000;
    private static final int FOR_FAULT_TOLERANT_MILLISECONDS = 200;

    @Test
    public void normal() {
        RateLimiter rateLimiter = RateLimiter.create(PERMITS_PER_SECOND);
        long startTime = System.currentTimeMillis();
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 1.");
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 2.");
        rateLimiter.acquire(PERMITS_NEEDS_PER_OPERATION);
        System.out.println("Operation 3.");
        long usedTime = System.currentTimeMillis() - startTime;
        System.out.println(usedTime + " ms");
        assertEquals(true, usedTime + FOR_FAULT_TOLERANT_MILLISECONDS >= SHOULD_WAIT_MILLISECONDS);
        System.out.println(String.format("%s >= %s" + " ms", usedTime, SHOULD_WAIT_MILLISECONDS));
    }
}
{'fraction_non_alphanumeric': 0.10329829648423342, 'fraction_numerical': 0.014860456687205509, 'mean_word_length': 3.825174825174825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4251662', 'n_tokens_mistral': 944, 'n_tokens_neox': 896, 'n_words': 167}