Skip to content

Commit

Permalink
test: Fix overrunning test FlowRuleManagerTest.testLoadAndGetRules (#…
Browse files Browse the repository at this point in the history
…1823)

Signed-off-by: Jason Joo <hblzxsj@163.com>
  • Loading branch information
jasonjoo2010 authored Oct 29, 2020
1 parent c00f946 commit d510eb6
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -116,15 +116,16 @@ public void run() {

@Test
public void testOriginFlowRule() {
String RESOURCE_NAME = "testOriginFlowRule";
// normal
FlowRule flowRule = new FlowRule();
flowRule.setResource("testOriginFlowRule");
flowRule.setResource(RESOURCE_NAME);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(0);
flowRule.setLimitApp("other");

FlowRule flowRule2 = new FlowRule();
flowRule2.setResource("testOriginFlowRule");
flowRule2.setResource(RESOURCE_NAME);
flowRule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule2.setCount(1);
flowRule2.setLimitApp("app2");
@@ -134,7 +135,7 @@ public void testOriginFlowRule() {
ContextUtil.enter("node1", "app1");
Entry e = null;
try {
e = SphU.entry("testOriginFlowRule");
e = SphU.entry(RESOURCE_NAME);
fail("Should had failed");
} catch (BlockException e1) {
e1.printStackTrace();
@@ -146,7 +147,7 @@ public void testOriginFlowRule() {
ContextUtil.enter("node1", "app2");
e = null;
try {
e = SphU.entry("testOriginFlowRule");
e = SphU.entry(RESOURCE_NAME);
} catch (BlockException e1) {
fail("Should had failed");
}
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

@@ -44,27 +46,34 @@ public class FlowRuleManagerTest {
}

@Test
public void testLoadAndGetRules(){
public void testLoadAndGetRules() throws InterruptedException{
FlowRuleManager.loadRules(STATIC_RULES_1);
assertEquals(1, FlowRuleManager.getRules().size()); // the initial size
new Thread(loader, "Loader").start();
final CountDownLatch latchStart = new CountDownLatch(1);
final CountDownLatch latchEnd = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
try {
latchStart.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
return;
}
for(int i = 0; i < 10000; i++){
//to guarantee that they're different and change happens
FlowRuleManager.loadRules(i % 2 == 0 ? STATIC_RULES_2 : STATIC_RULES_1);
}
latchEnd.countDown();
}
}).start();

for(int i = 0; i < 10000; i++){
latchStart.countDown();
for (int i = 0; i < 10000; i++) {
//The initial size is 1, and the size after updating should also be 1,
//if the actual size is 0, that must be called after clear(),
// but before putAll() in FlowPropertyListener.configUpdate
assertEquals(1, FlowRuleManager.getRules().size());
}
latchEnd.await(10, TimeUnit.SECONDS);
}

public Runnable loader = new Runnable() {
@Override
public void run() {
for(int i = 0; i < 10000; i++){
//to guarantee that they're different and change happens
FlowRuleManager.loadRules(i % 2 == 0 ? STATIC_RULES_2 : STATIC_RULES_1);
}
}
};

}
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**

0 comments on commit d510eb6

Please sign in to comment.