package com.alibaba.csp.sentinel.slots.block.flow.controller;

import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.node.StatisticNode;
import com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpRateLimiterController;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author CarpenterLee
 */
public class WarmUpRateLimiterControllerTest {

    @Test
    public void testPace() throws InterruptedException {
        WarmUpRateLimiterController controller = new WarmUpRateLimiterController(10, 10, 1000, 3);

        Node node = mock(Node.class);

        when(node.passQps()).thenReturn(100d);
        when(node.previousPassQps()).thenReturn(100d);

        assertTrue(controller.canPass(node, 1));

        long start = System.currentTimeMillis();
        assertTrue(controller.canPass(node, 1));
        long cost = System.currentTimeMillis() - start;
        assertTrue(cost >= 100 && cost <= 120);
    }

    @Test
    public void testPaceCanNotPass() throws InterruptedException {
        WarmUpRateLimiterController controller = new WarmUpRateLimiterController(10, 10, 10, 3);

        Node node = mock(Node.class);

        when(node.passQps()).thenReturn(100d);
        when(node.previousPassQps()).thenReturn(100d);

        assertTrue(controller.canPass(node, 1));

        assertFalse(controller.canPass(node, 1));
    }
}
@@ -884,49 +884,234 @@
     
-long start = System.currentTimeMillis();%0A
+// Easily fail in single request testing, so we increase it to 10 requests and test the average time%0A        long start = System.currentTimeMillis();%0A        int requests = 10;%0A        for (int i = 0; i %3C requests; i++) %7B%0A    
     
@@ -1167,20 +1167,31 @@
     
-long
+%7D%0A        float
  cost = 
 Syst
@@ -1182,24 +1182,25 @@
 loat cost = 
+(
 System.curre
@@ -1221,16 +1221,35 @@
  - start
+) / 1.0f / requests
 ;%0A      
@@ -1265,33 +1265,32 @@
 rue(
+Math.abs(
 cost 
-%3E=
+-
  100
- && cost
+)
  %3C
-=
  1
-2
 0);%0A

Make WarmUpRateLimiterControllerTest stable