Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
import com.namehillsoftware.handoff.promises.Promise;
import com.namehillsoftware.handoff.promises.queued.cancellation.CancellableMessageWriter;

public class DispatchablePromise<Resolution> extends Promise<Resolution> {
public abstract class DispatchablePromise<Resolution> extends Promise<Resolution> implements CancellableMessageWriter<Resolution> {

private final CancellationToken cancellationToken = new CancellationToken();
private final CancellableMessageWriter<Resolution> messageWriter;

public DispatchablePromise(CancellableMessageWriter<Resolution> messageWriter) {
public DispatchablePromise() {
awaitCancellation(cancellationToken);
this.messageWriter = messageWriter;
}

public final void dispatchMessage() {
try {
resolve(messageWriter.prepareMessage(cancellationToken));
resolve(prepareMessage(cancellationToken));
} catch (Throwable e) {
reject(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.namehillsoftware.handoff.promises.queued;

import com.namehillsoftware.handoff.promises.queued.cancellation.CancellableMessageWriter;
public abstract class ExecutablePromise<Resolution> extends DispatchablePromise<Resolution> implements Runnable {

public class ExecutablePromise<Resolution> extends DispatchablePromise<Resolution> implements Runnable {

public ExecutablePromise(CancellableMessageWriter<Resolution> messageWriter) {
super(messageWriter);
public ExecutablePromise() {
super();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import com.namehillsoftware.handoff.promises.Promise;
import com.namehillsoftware.handoff.promises.queued.QueuedPromise;
import com.namehillsoftware.handoff.promises.queued.cancellation.TestExecutors;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Created by david on 10/17/16.
*/
public class WhenTheActionIsCancelled {

private static String nextReturningPromiseResult;
private static boolean isCalled;
private static boolean isCancelled;
private static volatile String nextReturningPromiseResult;
private static volatile boolean isCalled;
private static volatile boolean isCancelled;

@BeforeClass
public static void before() throws InterruptedException {
Expand All @@ -29,7 +30,7 @@ public static void before() throws InterruptedException {
return "test";
}, TestExecutors.TEST_EXECUTOR)
.must((cancellationSignal) -> {
isCancelled = true;
isCancelled = cancellationSignal.isCancelled();
isCalled = true;
});

Expand All @@ -47,11 +48,11 @@ public void thenTheCancellationIsSet() {

@Test
public void thenTheNextActionReturnsAPromiseOfTheCorrectType() {
Assert.assertEquals("test", nextReturningPromiseResult);
assertEquals("test", nextReturningPromiseResult);
}

@Test
public void thenTheAlwaysConditionIsCalled() {
Assert.assertTrue(isCalled);
assertTrue(isCalled);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.namehillsoftware.handoff.promises.queued;

import com.namehillsoftware.handoff.cancellation.CancellationSignal;
import com.namehillsoftware.handoff.promises.queued.cancellation.CancellableMessageWriter;

import java.util.concurrent.Executor;

public class QueuedPromise<Resolution> extends ExecutablePromise<Resolution> {
private final CancellableMessageWriter<Resolution> task;

public QueuedPromise(CancellableMessageWriter<Resolution> task, Executor executor) {
super(task);
super();
this.task = task;
executor.execute(this);
}

@Override
public Resolution prepareMessage(CancellationSignal cancellationSignal) throws Throwable {
return task.prepareMessage(cancellationSignal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public class WhenThePromiseIsCancelledImmediately {

@BeforeClass
public static void before() throws InterruptedException {
final CountDownLatch cancellationLatch = new CountDownLatch(1);
final QueuedPromise<String> cancellablePromise = new QueuedPromise<>(cs -> {
cancellationLatch.await();

if (cs.isCancelled())
throw thrownException;

return "";
}, TestExecutors.TEST_EXECUTOR);

cancellablePromise.cancel();
cancellationLatch.countDown();

final CountDownLatch rejectionLatch = new CountDownLatch(1);
cancellablePromise.then(
Expand Down
Loading