package com.alibaba.csp.sentinel.metric.extension.callback;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
import com.alibaba.csp.sentinel.util.TimeUtil;

import org.junit.Assert;
import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Carpenter Lee
 */
public class MetricExitCallbackTest {

    @Test
    public void onExit() {
        FakeMetricExtension extension = new FakeMetricExtension();
        MetricExtensionProvider.addMetricExtension(extension);

        MetricExitCallback exitCallback = new MetricExitCallback();
        StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
        int count = 2;
        Object[] args = {"args1", "args2"};
        extension.rt = 20;
        extension.success = 6;
        extension.thread = 10;
        Context context = mock(Context.class);
        Entry entry = mock(Entry.class);
        when(entry.getError()).thenReturn(null);
        when(entry.getCreateTime()).thenReturn(TimeUtil.currentTimeMillis() - 100);
        when(context.getCurEntry()).thenReturn(entry);
        exitCallback.onExit(context, resourceWrapper, count, args);
        Assert.assertEquals(120, extension.rt, 10);
        Assert.assertEquals(extension.success, 6 + count);
        Assert.assertEquals(extension.thread, 10 - 1);
    }
}
@@ -1,8 +1,624 @@
+/*%0A * Copyright 1999-2018 Alibaba Group Holding Ltd.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -976,21 +976,34 @@
 nel.
-util.TimeUtil
+test.AbstractTimeBasedTest
 ;%0A%0Ai
@@ -1064,32 +1064,46 @@
  static org.
+powermock.api.
 mockito.
 Mockito.mock
@@ -1082,32 +1082,37 @@
 ock.api.mockito.
+Power
 Mockito.mock;%0Aim
@@ -1127,24 +1127,38 @@
 tic org.
+powermock.api.
 mockito.
 Mockito.
@@ -1149,16 +1149,21 @@
 mockito.
+Power
 Mockito.
@@ -1238,16 +1238,46 @@
 ackTest 
+extends AbstractTimeBasedTest 
 %7B%0A%0A    @
@@ -1676,16 +1676,42 @@
 rgs2%22%7D;%0A
+        long prevRt = 20;%0A
         
@@ -1721,26 +1721,30 @@
 ension.rt = 
-20
+prevRt
 ;%0A        ex
@@ -1874,32 +1874,180 @@
 ck(Entry.class);
+%0A%0A        // Mock current time%0A        long curMillis = System.currentTimeMillis();%0A        setCurrentMillis(curMillis);%0A%0A        int deltaMs = 100;
 %0A        when(en
@@ -2131,42 +2131,27 @@
 urn(
-TimeUtil.currentTime
+cur
 Millis
-()
  - 
-100
+deltaMs
 );%0A 
@@ -2304,11 +2304,24 @@
 als(
-120
+prevRt + deltaMs
 , ex
@@ -2330,20 +2330,16 @@
 nsion.rt
-, 10
 );%0A     
@@ -2454,8 +2454,9 @@
 %0A    %7D%0A%7D
+%0A

Fix unstable test cases in MetricExitCallbackTest