Skip to content

Commit

Permalink
Update integration test and demo for async entry
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Sep 25, 2018
1 parent e50fc55 commit 7c179bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,44 @@ public void testAsyncEntryUnderSyncEntry() throws Exception {
ContextUtil.exit();
}

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

testInvocationTreeCorrect();
}

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

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
public void shutdown() {
pool.shutdownNow();
ContextUtil.exit();
ContextTestUtil.cleanUpContext();
}

private void runAsync(Runnable f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ private void anotherAsync() {
ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
try {
TimeUnit.SECONDS.sleep(2);
// Normal entry nested in asynchronous entry.
anotherSyncInAsync();

System.out.println("Async result: 666");
} catch (InterruptedException e) {
// Ignore.
Expand Down Expand Up @@ -115,12 +117,10 @@ private void directlyAsync() {
try {
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()`.
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
// Ignore.
// Here to handle the async result (without other entry).
} finally {
// Exit the async entry.
entry.exit();
Expand All @@ -138,7 +138,7 @@ private void doAsyncThenSync() {
final AsyncEntry entry = SphU.asyncEntry("test-async");
this.invoke("abc", resp -> {
// 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.
ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
try {
Expand All @@ -149,7 +149,7 @@ private void doAsyncThenSync() {

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();
} finally {
// Exit the async entry.
Expand All @@ -165,7 +165,7 @@ private void doAsyncThenSync() {
}
}

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

AsyncEntryDemo service = new AsyncEntryDemo();
Expand Down Expand Up @@ -195,6 +195,8 @@ public static void main(String[] args) {
}
ContextUtil.exit();
}

TimeUnit.SECONDS.sleep(20);
}

private static void initFlowRule() {
Expand Down

0 comments on commit 7c179bb

Please sign in to comment.