这是indexloc提供的服务,不要输入任何密码
Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bolts-tasks/src/main/java/bolts/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public void waitForCompletion() throws InterruptedException {
}
}

/**
* Blocks until the task is complete or times out.
* @return {@code true} if the task completed (has a result, an error, or was cancelled.
* {@code false} otherwise.
*/
public boolean waitForCompletion(long duration, TimeUnit timeUnit) throws InterruptedException {
synchronized (lock) {
if (!isCompleted()) {
lock.wait(timeUnit.toMillis(duration));
}
return isCompleted();
}
}

/**
* Creates a completed task with the given value.
*/
Expand Down
26 changes: 26 additions & 0 deletions bolts-tasks/src/test/java/bolts/TaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Copy link
Member

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.

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());
}
Copy link
Member

Choose a reason for hiding this comment

The 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>() {
Expand Down