-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Refactor Task execution to support asynchronous callbacks #3274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
5d157d6
b42dc28
12a7f1d
5f8069b
a86c211
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
|
@@ -39,19 +41,31 @@ class CallbackTest { | |
private Integer callingCount = 0; | ||
|
||
@Test | ||
void test() { | ||
Callback callback = () -> callingCount++; | ||
void test() throws InterruptedException { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
var task = new SimpleTask(); | ||
CountDownLatch finalLatch = latch; | ||
Callback callback = | ||
() -> { | ||
callingCount++; | ||
finalLatch.countDown(); | ||
}; | ||
|
||
assertEquals(Integer.valueOf(0), callingCount, "Initial calling count of 0"); | ||
var task = new SimpleTask(); | ||
|
||
task.executeWith(callback); | ||
|
||
latch.await(5, TimeUnit.SECONDS); | ||
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. The test uses |
||
|
||
assertEquals(Integer.valueOf(1), callingCount, "Callback called once"); | ||
|
||
callingCount = 0; | ||
latch = new CountDownLatch(1); | ||
|
||
task.executeWith(callback); | ||
|
||
assertEquals(Integer.valueOf(2), callingCount, "Callback called twice"); | ||
latch.await(5, TimeUnit.SECONDS); | ||
|
||
assertEquals(Integer.valueOf(1), callingCount, "Callback called once again"); | ||
} | ||
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. The test uses |
||
} |
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.
In general, looks good! However, I'd like to keep the synchronous callbacks (previous functionality) as well.
Could we demonstrate both sychronous and asynchronous callback in the example? The guidance would be something like