Skip to content

Commit

Permalink
Make build faster by reducing fixed waiting time in tests (alibaba#449)
Browse files Browse the repository at this point in the history
- Apply awaitility to core tests
- Cache maven local repo in Travis
  • Loading branch information
aalmiray authored and sczyh30 committed Jan 29, 2019
1 parent 9e1b5a9 commit e893dd8
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 84 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ matrix:
- env: BUILD_JDK=ORACLE_JDK_11

after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)

cache:
directories:
- $HOME/.m2/
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<!-- Test libs -->
<junit.version>4.12</junit.version>
<mockito.version>2.21.0</mockito.version>
<awaitility.version>3.1.5</awaitility.version>

<!-- Build -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -155,6 +156,18 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
20 changes: 18 additions & 2 deletions sentinel-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-libray</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@
*/
package com.alibaba.csp.sentinel;

import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.context.ContextTestUtil;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.node.Node;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.fail;

/**
* Integration test for asynchronous entry, including common scenarios.
Expand Down Expand Up @@ -175,37 +177,59 @@ public void testAsyncEntryUnderSyncEntry() throws Exception {
ContextUtil.exit();
}

TimeUnit.SECONDS.sleep(15);
// we keep the original timeout of 15 seconds although the test should
// complete in less than 6 seconds
await().timeout(15, TimeUnit.SECONDS)
.until(new Callable<DefaultNode>() {
@Override
public DefaultNode call() throws Exception {
return queryInvocationTree(false);
}
}, CoreMatchers.notNullValue());

testInvocationTreeCorrect();
queryInvocationTree(true);
}

private void testInvocationTreeCorrect() {
private DefaultNode queryInvocationTree(boolean check) {
DefaultNode root = Constants.ROOT;
DefaultNode entranceNode = shouldHasChildFor(root, contextName);
DefaultNode testTopNode = shouldHasChildFor(entranceNode, "test-top");
DefaultNode testAsyncNode = shouldHasChildFor(testTopNode, "test-async");
shouldHasChildFor(testTopNode, "test-sync");
shouldHasChildFor(testAsyncNode, "test-sync-in-async");
DefaultNode anotherAsyncInAsyncNode = shouldHasChildFor(testAsyncNode, "test-another-async");
shouldHasChildFor(anotherAsyncInAsyncNode, "test-another-in-async");
DefaultNode entranceNode = shouldHasChildFor(root, contextName, check);
DefaultNode testTopNode = shouldHasChildFor(entranceNode, "test-top", check);
DefaultNode testAsyncNode = shouldHasChildFor(testTopNode, "test-async", check);
shouldHasChildFor(testTopNode, "test-sync", check);
shouldHasChildFor(testAsyncNode, "test-sync-in-async", check);
DefaultNode anotherAsyncInAsyncNode = shouldHasChildFor(testAsyncNode, "test-another-async", check);
return shouldHasChildFor(anotherAsyncInAsyncNode, "test-another-in-async", check);
}

private DefaultNode shouldHasChildFor(DefaultNode root, String resourceName) {
private DefaultNode shouldHasChildFor(DefaultNode root, String resourceName, boolean check) {
if (root == null) {
if (check) {
fail("Root node should not be empty");
} else {
return null;
}
}
Set<Node> nodeSet = root.getChildList();
if (nodeSet == null || nodeSet.isEmpty()) {
fail("Child nodes should not be empty: " + root.getId().getName());
if (check) {
fail("Child nodes should not be empty: " + root.getId().getName());
} else {
return null;
}
}
for (Node node : nodeSet) {
if (node instanceof DefaultNode) {
DefaultNode dn = (DefaultNode)node;
DefaultNode dn = (DefaultNode) node;
if (dn.getId().getName().equals(resourceName)) {
return dn;
}
}
}
fail(String.format("The given node <%s> does not have child for resource <%s>",
root.getId().getName(), resourceName));

if (check) {
fail(String.format("The given node <%s> does not have child for resource <%s>",
root.getId().getName(), resourceName));
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testListWindowsNewBucket() throws Exception {
windowWraps.add(leapArray.currentWindow(time));
windowWraps.add(leapArray.currentWindow(time + windowLengthInMs));

Thread.sleep(intervalInSec * 1000 + windowLengthInMs * 3);
Thread.sleep(intervalInMs + windowLengthInMs * 3);

List<WindowWrap<MetricBucket>> list = leapArray.list();
for (WindowWrap<MetricBucket> wrap : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.csp.sentinel.slots.block.flow;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -57,8 +58,8 @@ public void testQPSGrade() {
}
}

@Test
public void testThreadGrade() throws InterruptedException {
@Test(expected = BlockException.class)
public void testThreadGrade() throws InterruptedException, BlockException {
FlowRule flowRule = new FlowRule();
flowRule.setResource("testThreadGrade");
flowRule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
Expand All @@ -73,17 +74,15 @@ public void run() {
Entry e = null;
try {
e = SphU.entry("testThreadGrade");
assertTrue(true);
synchronized (sequence) {
System.out.println("notify up");
sequence.notify();
}
Thread.sleep(1000);
Thread.sleep(100);
} catch (BlockException e1) {
assertTrue(false);
fail("Should had failed");
} catch (InterruptedException e1) {
assertTrue(false);
e1.printStackTrace();
fail("Should had failed");
}
e.exit();
}
Expand All @@ -92,20 +91,14 @@ public void run() {
Thread thread = new Thread(runnable);
thread.start();

Entry e = null;
synchronized (sequence) {
System.out.println("sleep");
sequence.wait();
System.out.println("wake up");
}
try {
e = SphU.entry("testThreadGrade");
assertTrue(false);
} catch (BlockException e1) {
assertTrue(true);
}
System.out.println("done");

SphU.entry("testThreadGrade");
System.out.println("done");
}

@Test
Expand All @@ -129,10 +122,9 @@ public void testOriginFlowRule() {
Entry e = null;
try {
e = SphU.entry("testOriginFlowRule");
assertTrue(false);
fail("Should had failed");
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(true);
}
assertTrue(e == null);

Expand All @@ -142,10 +134,8 @@ public void testOriginFlowRule() {
e = null;
try {
e = SphU.entry("testOriginFlowRule");
assertTrue(true);
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(false);
fail("Should had failed");
}

e.exit();
Expand All @@ -165,17 +155,14 @@ public void testFlowRule_other() {
Entry e = null;
try {
e = SphU.entry("testOther");
assertTrue(true);
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(false);
e1.printStackTrace();fail("Should had failed");
}

if (e != null) {
assertTrue(true);
e.exit();
} else {
assertTrue(false);
fail("Should had failed");
}
}

Expand All @@ -194,10 +181,9 @@ public void testStrategy() {
Entry e = null;
try {
e = SphU.entry("testStrategy");
assertTrue(false);
fail("Should had failed");
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(true);
}

ContextUtil.exit();
Expand All @@ -215,10 +201,9 @@ public void testStrategy() {
ContextUtil.enter("entry1");
try {
e = SphU.entry("testStrategy");
assertTrue(true);
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(false);
fail("Should had failed");
}
e.exit();
ContextUtil.exit();
Expand All @@ -238,10 +223,9 @@ public void testStrategy_chain() {
ContextUtil.enter("entry1");
try {
e = SphU.entry("entry2");
assertTrue(false);
fail("Should had failed");
} catch (BlockException e1) {
e1.printStackTrace();
assertTrue(true);
}

ContextUtil.exit();
Expand All @@ -250,13 +234,11 @@ public void testStrategy_chain() {
ContextUtil.enter("entry3");
try {
e = SphU.entry("entry2");
assertTrue(true);
} catch (BlockException e1) {
assertTrue(false);
fail("Should had failed");
}
e.exit();

ContextUtil.exit();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.junit.Test;

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

/**
* @author jialiang.linjl
Expand All @@ -49,7 +48,7 @@ public void testWarmUp() throws InterruptedException {
when(node.previousPassQps()).thenReturn(10L);

for (int i = 0; i < 100; i++) {
Thread.sleep(1000);
Thread.sleep(100);
warmupController.canPass(node, 1);
}
when(node.passQps()).thenReturn(8L);
Expand Down
Loading

0 comments on commit e893dd8

Please sign in to comment.