This repository was archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
Support partial waits #99
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
@@ -310,6 +311,31 @@ public Integer call() throws Exception { | |
| assertEquals(5, task.getResult().intValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBackgroundCallWaitingWithTimeouts() throws Exception { | ||
| final Object sync = new Object(); | ||
|
|
||
| Task<Integer> task = Task.callInBackground(new Callable<Integer>() { | ||
| public Integer call() throws Exception { | ||
| synchronized (sync) { | ||
| sync.wait(); | ||
| Thread.sleep(100); | ||
| } | ||
| return 5; | ||
| } | ||
| }); | ||
| // wait -> timeout | ||
| assertFalse(task.waitForCompletion(100, TimeUnit.MILLISECONDS)); | ||
| synchronized (sync) { | ||
| sync.notify(); | ||
| } | ||
| // wait -> completes | ||
| assertTrue(task.waitForCompletion(1000, TimeUnit.MILLISECONDS)); | ||
| // wait -> already completed | ||
| assertTrue(task.waitForCompletion(100, TimeUnit.MILLISECONDS)); | ||
| assertEquals(5, task.getResult().intValue()); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we'd want to verify the third code path where task.waitForCompletion returns true if it isn't already completed when calling it. |
||
|
|
||
| @Test | ||
| public void testBackgroundCallWaitingOnError() throws Exception { | ||
| Task<Integer> task = Task.callInBackground(new Callable<Integer>() { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do that by adding a short Thead.sleep(50) here (ugly, I know 😞) and add another assertTrue(task.wait...) after the last one to retain the verification that it returns true on an already completed task.