Skip to content

Commit

Permalink
test: Fix unstable test cases in MetricExitCallbackTest
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Dec 24, 2019
1 parent f6badb0 commit e9d730c
Showing 1 changed file with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 com.alibaba.csp.sentinel.test.AbstractTimeBasedTest;

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

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

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

@Test
public void onExit() {
Expand All @@ -27,17 +42,24 @@ public void onExit() {
StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
int count = 2;
Object[] args = {"args1", "args2"};
extension.rt = 20;
long prevRt = 20;
extension.rt = prevRt;
extension.success = 6;
extension.thread = 10;
Context context = mock(Context.class);
Entry entry = mock(Entry.class);

// Mock current time
long curMillis = System.currentTimeMillis();
setCurrentMillis(curMillis);

int deltaMs = 100;
when(entry.getError()).thenReturn(null);
when(entry.getCreateTime()).thenReturn(TimeUtil.currentTimeMillis() - 100);
when(entry.getCreateTime()).thenReturn(curMillis - deltaMs);
when(context.getCurEntry()).thenReturn(entry);
exitCallback.onExit(context, resourceWrapper, count, args);
Assert.assertEquals(120, extension.rt, 10);
Assert.assertEquals(prevRt + deltaMs, extension.rt);
Assert.assertEquals(extension.success, 6 + count);
Assert.assertEquals(extension.thread, 10 - 1);
}
}
}

0 comments on commit e9d730c

Please sign in to comment.