Browse Source

Update integration test and demo for async entry

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 6 years ago
parent
commit
7c179bb592
2 changed files with 39 additions and 13 deletions
  1. +30
    -6
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/AsyncEntryIntegrationTest.java
  2. +9
    -7
      sentinel-demo/sentinel-demo-basic/src/main/java/com/alibaba/csp/sentinel/demo/AsyncEntryDemo.java

+ 30
- 6
sentinel-core/src/test/java/com/alibaba/csp/sentinel/AsyncEntryIntegrationTest.java View File

@@ -175,20 +175,44 @@ public class AsyncEntryIntegrationTest {
ContextUtil.exit(); ContextUtil.exit();
} }


TimeUnit.SECONDS.sleep(10); TimeUnit.SECONDS.sleep(15);
testTreeCorrect(); testInvocationTreeCorrect();
} }


private void testTreeCorrect() { private void testInvocationTreeCorrect() {
DefaultNode root = Constants.ROOT; DefaultNode root = Constants.ROOT;
Set<Node> childListForRoot = root.getChildList(); DefaultNode entranceNode = shouldHasChildFor(root, contextName);
// TODO: check child tree 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");
}

private DefaultNode shouldHasChildFor(DefaultNode root, String resourceName) {
Set<Node> nodeSet = root.getChildList();
if (nodeSet == null || nodeSet.isEmpty()) {
fail("Child nodes should not be empty: " + root.getId().getName());
}
for (Node node : nodeSet) {
if (node instanceof DefaultNode) {
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));
return null;
} }


@After @After
public void shutdown() { public void shutdown() {
pool.shutdownNow(); pool.shutdownNow();
ContextUtil.exit(); ContextTestUtil.cleanUpContext();
} }


private void runAsync(Runnable f) { private void runAsync(Runnable f) {


+ 9
- 7
sentinel-demo/sentinel-demo-basic/src/main/java/com/alibaba/csp/sentinel/demo/AsyncEntryDemo.java View File

@@ -58,7 +58,9 @@ public class AsyncEntryDemo {
ContextUtil.runOnContext(entry.getAsyncContext(), () -> { ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
try { try {
TimeUnit.SECONDS.sleep(2); TimeUnit.SECONDS.sleep(2);
// Normal entry nested in asynchronous entry.
anotherSyncInAsync(); anotherSyncInAsync();

System.out.println("Async result: 666"); System.out.println("Async result: 666");
} catch (InterruptedException e) { } catch (InterruptedException e) {
// Ignore. // Ignore.
@@ -115,12 +117,10 @@ public class AsyncEntryDemo {
try { try {
final AsyncEntry entry = SphU.asyncEntry("test-async-not-nested"); final AsyncEntry entry = SphU.asyncEntry("test-async-not-nested");


CompletableFuture.runAsync(() -> { this.invoke("abc", result -> {
// If no nested entry later, we don't have to wrap in `ContextUtil.runOnContext()`. // If no nested entry later, we don't have to wrap in `ContextUtil.runOnContext()`.
try { try {
TimeUnit.SECONDS.sleep(1); // Here to handle the async result (without other entry).
} catch (InterruptedException ex) {
// Ignore.
} finally { } finally {
// Exit the async entry. // Exit the async entry.
entry.exit(); entry.exit();
@@ -138,7 +138,7 @@ public class AsyncEntryDemo {
final AsyncEntry entry = SphU.asyncEntry("test-async"); final AsyncEntry entry = SphU.asyncEntry("test-async");
this.invoke("abc", resp -> { this.invoke("abc", resp -> {
// The thread is different from original caller thread for async entry. // The thread is different from original caller thread for async entry.
// So we need to wrap in the async context so that nested sync invocation entry // So we need to wrap in the async context so that nested invocation entry
// can be linked to the parent asynchronous entry. // can be linked to the parent asynchronous entry.
ContextUtil.runOnContext(entry.getAsyncContext(), () -> { ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
try { try {
@@ -149,7 +149,7 @@ public class AsyncEntryDemo {


System.out.println(resp); System.out.println(resp);


// Then we do a sync entry under current async context. // Then we do a sync (normal) entry under current async context.
fetchSyncInAsync(); fetchSyncInAsync();
} finally { } finally {
// Exit the async entry. // Exit the async entry.
@@ -165,7 +165,7 @@ public class AsyncEntryDemo {
} }
} }


public static void main(String[] args) { public static void main(String[] args) throws Exception {
initFlowRule(); initFlowRule();


AsyncEntryDemo service = new AsyncEntryDemo(); AsyncEntryDemo service = new AsyncEntryDemo();
@@ -195,6 +195,8 @@ public class AsyncEntryDemo {
} }
ContextUtil.exit(); ContextUtil.exit();
} }

TimeUnit.SECONDS.sleep(20);
} }


private static void initFlowRule() { private static void initFlowRule() {


Loading…
Cancel
Save