Submit first try to IoExecutor directly.

This commit is contained in:
akwizgran
2022-05-25 12:45:23 +01:00
parent 7e249ecf70
commit 0c22c25995
2 changed files with 18 additions and 30 deletions

View File

@@ -59,8 +59,7 @@ class MailboxApiCallerImpl implements MailboxApiCaller {
private void start() { private void start() {
synchronized (lock) { synchronized (lock) {
if (cancelled) throw new AssertionError(); if (cancelled) throw new AssertionError();
scheduledTask = taskScheduler.schedule(this::callApi, ioExecutor.execute(this::callApi);
ioExecutor, 0, MILLISECONDS);
} }
} }

View File

@@ -31,22 +31,18 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
new MailboxApiCallerImpl(taskScheduler, ioExecutor); new MailboxApiCallerImpl(taskScheduler, ioExecutor);
@Test @Test
public void testSubmitsTaskWithZeroDelay() { public void testSubmitsTaskImmediately() {
// Calling retryWithBackoff() should schedule a try with zero delay // Calling retryWithBackoff() should schedule the first try immediately
AtomicReference<Runnable> runnable = new AtomicReference<>(null); AtomicReference<Runnable> runnable = new AtomicReference<>(null);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(taskScheduler).schedule(with(any(Runnable.class)), oneOf(ioExecutor).execute(with(any(Runnable.class)));
with(ioExecutor), with(0L), with(MILLISECONDS)); will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
will(new DoAllAction(
new CaptureArgumentAction<>(runnable, Runnable.class, 0),
returnValue(scheduledTask)
));
}}); }});
caller.retryWithBackoff(supplier); caller.retryWithBackoff(supplier);
// When the scheduled task runs, the supplier should be called. The // When the task runs, the supplier should be called. The supplier
// supplier returns false, so no retries should be scheduled // returns false, so no retries should be scheduled
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(supplier).get(); oneOf(supplier).get();
will(returnValue(false)); will(returnValue(false));
@@ -57,21 +53,17 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
@Test @Test
public void testDoesNotRetryTaskIfCancelled() { public void testDoesNotRetryTaskIfCancelled() {
// Calling retryWithBackoff() should schedule a try with zero delay // Calling retryWithBackoff() should schedule the first try immediately
AtomicReference<Runnable> runnable = new AtomicReference<>(null); AtomicReference<Runnable> runnable = new AtomicReference<>(null);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(taskScheduler).schedule(with(any(Runnable.class)), oneOf(ioExecutor).execute(with(any(Runnable.class)));
with(ioExecutor), with(0L), with(MILLISECONDS)); will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
will(new DoAllAction(
new CaptureArgumentAction<>(runnable, Runnable.class, 0),
returnValue(scheduledTask)
));
}}); }});
Cancellable returned = caller.retryWithBackoff(supplier); Cancellable returned = caller.retryWithBackoff(supplier);
// When the scheduled task runs, the supplier should be called. The // When the task runs, the supplier should be called. The supplier
// supplier returns true, so a retry should be scheduled // returns true, so a retry should be scheduled
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(supplier).get(); oneOf(supplier).get();
will(returnValue(true)); will(returnValue(true));
@@ -105,6 +97,7 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
@Test @Test
public void testDoublesRetryIntervalUntilMaximumIsReached() { public void testDoublesRetryIntervalUntilMaximumIsReached() {
// The expected retry intervals increase from the min to the max
List<Long> expectedIntervals = new ArrayList<>(); List<Long> expectedIntervals = new ArrayList<>();
for (long interval = MIN_RETRY_INTERVAL_MS; for (long interval = MIN_RETRY_INTERVAL_MS;
interval <= MAX_RETRY_INTERVAL_MS; interval *= 2) { interval <= MAX_RETRY_INTERVAL_MS; interval *= 2) {
@@ -114,21 +107,17 @@ public class MailboxApiCallerImplTest extends BrambleMockTestCase {
expectedIntervals.add(MAX_RETRY_INTERVAL_MS); expectedIntervals.add(MAX_RETRY_INTERVAL_MS);
expectedIntervals.add(MAX_RETRY_INTERVAL_MS); expectedIntervals.add(MAX_RETRY_INTERVAL_MS);
// Calling retryWithBackoff() should schedule a try with zero delay // Calling retryWithBackoff() should schedule the first try immediately
AtomicReference<Runnable> runnable = new AtomicReference<>(null); AtomicReference<Runnable> runnable = new AtomicReference<>(null);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(taskScheduler).schedule(with(any(Runnable.class)), oneOf(ioExecutor).execute(with(any(Runnable.class)));
with(ioExecutor), with(0L), with(MILLISECONDS)); will(new CaptureArgumentAction<>(runnable, Runnable.class, 0));
will(new DoAllAction(
new CaptureArgumentAction<>(runnable, Runnable.class, 0),
returnValue(scheduledTask)
));
}}); }});
caller.retryWithBackoff(supplier); caller.retryWithBackoff(supplier);
// Each time the scheduled task runs, the supplier returns true, so a // Each time the task runs, the supplier returns true, so a retry
// retry should be scheduled with a longer interval // should be scheduled with a longer interval
for (long interval : expectedIntervals) { for (long interval : expectedIntervals) {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
oneOf(supplier).get(); oneOf(supplier).get();