From ff7aa42f0de517945b857e1e94a6dfac6d612be4 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 20 Jan 2025 18:23:31 -0600 Subject: [PATCH] Removed QueuedPromise from library --- .../promises/queued/DispatchablePromise.java | 8 +++----- .../promises/queued/ExecutablePromise.java | 8 +++----- .../WhenTheActionIsCancelled.java | 15 ++++++++------- .../handoff/promises/queued/QueuedPromise.java | 11 ++++++++++- .../WhenThePromiseIsCancelledImmediately.java | 4 ++++ 5 files changed, 28 insertions(+), 18 deletions(-) rename src/{main => test}/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java (54%) diff --git a/src/main/java/com/namehillsoftware/handoff/promises/queued/DispatchablePromise.java b/src/main/java/com/namehillsoftware/handoff/promises/queued/DispatchablePromise.java index cfa10e0..ebb6518 100644 --- a/src/main/java/com/namehillsoftware/handoff/promises/queued/DispatchablePromise.java +++ b/src/main/java/com/namehillsoftware/handoff/promises/queued/DispatchablePromise.java @@ -4,19 +4,17 @@ import com.namehillsoftware.handoff.promises.Promise; import com.namehillsoftware.handoff.promises.queued.cancellation.CancellableMessageWriter; -public class DispatchablePromise extends Promise { +public abstract class DispatchablePromise extends Promise implements CancellableMessageWriter { private final CancellationToken cancellationToken = new CancellationToken(); - private final CancellableMessageWriter messageWriter; - public DispatchablePromise(CancellableMessageWriter 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); } diff --git a/src/main/java/com/namehillsoftware/handoff/promises/queued/ExecutablePromise.java b/src/main/java/com/namehillsoftware/handoff/promises/queued/ExecutablePromise.java index 958562b..6f62bda 100644 --- a/src/main/java/com/namehillsoftware/handoff/promises/queued/ExecutablePromise.java +++ b/src/main/java/com/namehillsoftware/handoff/promises/queued/ExecutablePromise.java @@ -1,11 +1,9 @@ package com.namehillsoftware.handoff.promises.queued; -import com.namehillsoftware.handoff.promises.queued.cancellation.CancellableMessageWriter; +public abstract class ExecutablePromise extends DispatchablePromise implements Runnable { -public class ExecutablePromise extends DispatchablePromise implements Runnable { - - public ExecutablePromise(CancellableMessageWriter messageWriter) { - super(messageWriter); + public ExecutablePromise() { + super(); } @Override diff --git a/src/test/java/com/namehillsoftware/handoff/promises/GivenAPromiseThatResolves/AndAlwaysFinishesWithAnAction/WhenTheActionIsCancelled.java b/src/test/java/com/namehillsoftware/handoff/promises/GivenAPromiseThatResolves/AndAlwaysFinishesWithAnAction/WhenTheActionIsCancelled.java index 8cd4077..aad079e 100644 --- a/src/test/java/com/namehillsoftware/handoff/promises/GivenAPromiseThatResolves/AndAlwaysFinishesWithAnAction/WhenTheActionIsCancelled.java +++ b/src/test/java/com/namehillsoftware/handoff/promises/GivenAPromiseThatResolves/AndAlwaysFinishesWithAnAction/WhenTheActionIsCancelled.java @@ -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 { @@ -29,7 +30,7 @@ public static void before() throws InterruptedException { return "test"; }, TestExecutors.TEST_EXECUTOR) .must((cancellationSignal) -> { - isCancelled = true; + isCancelled = cancellationSignal.isCancelled(); isCalled = true; }); @@ -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); } } diff --git a/src/main/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java b/src/test/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java similarity index 54% rename from src/main/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java rename to src/test/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java index 8ed790d..4cfb272 100644 --- a/src/main/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java +++ b/src/test/java/com/namehillsoftware/handoff/promises/queued/QueuedPromise.java @@ -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 extends ExecutablePromise { + private final CancellableMessageWriter task; + public QueuedPromise(CancellableMessageWriter task, Executor executor) { - super(task); + super(); + this.task = task; executor.execute(this); } + + @Override + public Resolution prepareMessage(CancellationSignal cancellationSignal) throws Throwable { + return task.prepareMessage(cancellationSignal); + } } diff --git a/src/test/java/com/namehillsoftware/handoff/promises/queued/cancellation/GivenACancellableQueuedPromise/WhenThePromiseIsCancelledImmediately.java b/src/test/java/com/namehillsoftware/handoff/promises/queued/cancellation/GivenACancellableQueuedPromise/WhenThePromiseIsCancelledImmediately.java index e9107ef..e3b100a 100644 --- a/src/test/java/com/namehillsoftware/handoff/promises/queued/cancellation/GivenACancellableQueuedPromise/WhenThePromiseIsCancelledImmediately.java +++ b/src/test/java/com/namehillsoftware/handoff/promises/queued/cancellation/GivenACancellableQueuedPromise/WhenThePromiseIsCancelledImmediately.java @@ -17,7 +17,10 @@ public class WhenThePromiseIsCancelledImmediately { @BeforeClass public static void before() throws InterruptedException { + final CountDownLatch cancellationLatch = new CountDownLatch(1); final QueuedPromise cancellablePromise = new QueuedPromise<>(cs -> { + cancellationLatch.await(); + if (cs.isCancelled()) throw thrownException; @@ -25,6 +28,7 @@ public static void before() throws InterruptedException { }, TestExecutors.TEST_EXECUTOR); cancellablePromise.cancel(); + cancellationLatch.countDown(); final CountDownLatch rejectionLatch = new CountDownLatch(1); cancellablePromise.then(