From cc672378935fb2ca8c3702ef57e71f381e96ef07 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 12 Aug 2020 14:45:39 +0100 Subject: [PATCH] Don't redundantly use wakeful IO executor. --- .../bramble/plugin/PollerImpl.java | 11 +++++--- .../bramble/plugin/PollerImplTest.java | 27 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java index dbdd6e1d7..e687aa6aa 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/PollerImpl.java @@ -27,6 +27,7 @@ import org.briarproject.bramble.api.properties.TransportProperties; import org.briarproject.bramble.api.properties.TransportPropertyManager; import org.briarproject.bramble.api.system.Clock; import org.briarproject.bramble.api.system.TaskScheduler; +import org.briarproject.bramble.api.system.Wakeful; import org.briarproject.bramble.api.system.WakefulIoExecutor; import java.security.SecureRandom; @@ -57,7 +58,7 @@ class PollerImpl implements Poller, EventListener { private static final Logger LOG = getLogger(PollerImpl.class.getName()); - private final Executor wakefulIoExecutor; + private final Executor ioExecutor, wakefulIoExecutor; private final TaskScheduler scheduler; private final ConnectionManager connectionManager; private final ConnectionRegistry connectionRegistry; @@ -70,7 +71,8 @@ class PollerImpl implements Poller, EventListener { private final Map tasks; @Inject - PollerImpl(@WakefulIoExecutor Executor wakefulIoExecutor, + PollerImpl(@IoExecutor Executor ioExecutor, + @WakefulIoExecutor Executor wakefulIoExecutor, TaskScheduler scheduler, ConnectionManager connectionManager, ConnectionRegistry connectionRegistry, @@ -78,6 +80,7 @@ class PollerImpl implements Poller, EventListener { TransportPropertyManager transportPropertyManager, SecureRandom random, Clock clock) { + this.ioExecutor = ioExecutor; this.wakefulIoExecutor = wakefulIoExecutor; this.scheduler = scheduler; this.connectionManager = connectionManager; @@ -190,8 +193,8 @@ class PollerImpl implements Poller, EventListener { // it will abort safely when it finds it's been replaced if (scheduled != null) scheduled.future.cancel(false); PollTask task = new PollTask(p, due, randomiseNext); - Future future = scheduler.schedule(task, wakefulIoExecutor, - delay, MILLISECONDS); + Future future = scheduler.schedule(task, ioExecutor, delay, + MILLISECONDS); tasks.put(t, new ScheduledPollTask(task, future)); } } finally { diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java index 5d5db5497..2a93fd39e 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/PollerImplTest.java @@ -58,6 +58,7 @@ public class PollerImplTest extends BrambleMockTestCase { private final Future future = context.mock(Future.class); private final SecureRandom random; + private final Executor ioExecutor = new ImmediateExecutor(); private final Executor wakefulIoExecutor = new ImmediateExecutor(); private final TransportId transportId = getTransportId(); private final ContactId contactId = getContactId(); @@ -74,9 +75,9 @@ public class PollerImplTest extends BrambleMockTestCase { @Before public void setUp() { - poller = new PollerImpl(wakefulIoExecutor, scheduler, connectionManager, - connectionRegistry, pluginManager, transportPropertyManager, - random, clock); + poller = new PollerImpl(ioExecutor, wakefulIoExecutor, scheduler, + connectionManager, connectionRegistry, pluginManager, + transportPropertyManager, random, clock); } @Test @@ -234,7 +235,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) pollingInterval), + with(ioExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); }}); @@ -263,7 +264,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) pollingInterval), + with(ioExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); // Second event @@ -306,7 +307,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) pollingInterval), + with(ioExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); // Second event @@ -323,7 +324,7 @@ public class PollerImplTest extends BrambleMockTestCase { will(returnValue(now + 1)); oneOf(future).cancel(false); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) pollingInterval - 2), + with(ioExecutor), with((long) pollingInterval - 2), with(MILLISECONDS)); }}); @@ -350,7 +351,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); + with(ioExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); will(new RunAction()); // Running the polling task schedules the next polling task @@ -361,7 +362,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) (pollingInterval * 0.5)), + with(ioExecutor), with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); will(returnValue(future)); // Get the transport properties and connected contacts @@ -394,7 +395,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); + with(ioExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); will(new RunAction()); // Running the polling task schedules the next polling task @@ -405,7 +406,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) (pollingInterval * 0.5)), + with(ioExecutor), with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); will(returnValue(future)); // Get the transport properties and connected contacts @@ -436,7 +437,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with(0L), with(MILLISECONDS)); + with(ioExecutor), with(0L), with(MILLISECONDS)); will(returnValue(future)); // The plugin is deactivated before the task runs - cancel the task oneOf(future).cancel(false); @@ -460,7 +461,7 @@ public class PollerImplTest extends BrambleMockTestCase { oneOf(clock).currentTimeMillis(); will(returnValue(now)); oneOf(scheduler).schedule(with(any(Runnable.class)), - with(wakefulIoExecutor), with((long) pollingInterval), + with(ioExecutor), with((long) pollingInterval), with(MILLISECONDS)); will(returnValue(future)); }});