Pass executor to scheduler.

This commit is contained in:
akwizgran
2020-08-06 13:11:12 +01:00
parent d5395d3d01
commit 3aa00ecb3d
14 changed files with 128 additions and 92 deletions

View File

@@ -9,6 +9,7 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.event.EventExecutor;
import org.briarproject.bramble.api.lifecycle.Service; import org.briarproject.bramble.api.lifecycle.Service;
import org.briarproject.bramble.api.network.NetworkManager; import org.briarproject.bramble.api.network.NetworkManager;
import org.briarproject.bramble.api.network.NetworkStatus; import org.briarproject.bramble.api.network.NetworkStatus;
@@ -17,6 +18,7 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault; import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.bramble.api.system.TaskScheduler; import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@@ -51,6 +53,7 @@ class AndroidNetworkManager implements NetworkManager, Service {
private final TaskScheduler scheduler; private final TaskScheduler scheduler;
private final EventBus eventBus; private final EventBus eventBus;
private final Executor eventExecutor;
private final Context appContext; private final Context appContext;
private final AtomicReference<Future<?>> connectivityCheck = private final AtomicReference<Future<?>> connectivityCheck =
new AtomicReference<>(); new AtomicReference<>();
@@ -60,9 +63,10 @@ class AndroidNetworkManager implements NetworkManager, Service {
@Inject @Inject
AndroidNetworkManager(TaskScheduler scheduler, EventBus eventBus, AndroidNetworkManager(TaskScheduler scheduler, EventBus eventBus,
Application app) { @EventExecutor Executor eventExecutor, Application app) {
this.scheduler = scheduler; this.scheduler = scheduler;
this.eventBus = eventBus; this.eventBus = eventBus;
this.eventExecutor = eventExecutor;
this.appContext = app.getApplicationContext(); this.appContext = app.getApplicationContext();
} }
@@ -104,7 +108,8 @@ class AndroidNetworkManager implements NetworkManager, Service {
private void scheduleConnectionStatusUpdate(int delay, TimeUnit unit) { private void scheduleConnectionStatusUpdate(int delay, TimeUnit unit) {
Future<?> newConnectivityCheck = Future<?> newConnectivityCheck =
scheduler.schedule(this::updateConnectionStatus, delay, unit); scheduler.schedule(this::updateConnectionStatus, eventExecutor,
delay, unit);
Future<?> oldConnectivityCheck = Future<?> oldConnectivityCheck =
connectivityCheck.getAndSet(newConnectivityCheck); connectivityCheck.getAndSet(newConnectivityCheck);
if (oldConnectivityCheck != null) oldConnectivityCheck.cancel(false); if (oldConnectivityCheck != null) oldConnectivityCheck.cancel(false);

View File

@@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@@ -78,10 +79,11 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
} }
@Override @Override
public Future<?> schedule(Runnable task, long delay, TimeUnit unit) { public Future<?> schedule(Runnable task, Executor executor, long delay,
TimeUnit unit) {
long now = SystemClock.elapsedRealtime(); long now = SystemClock.elapsedRealtime();
long dueMillis = now + MILLISECONDS.convert(delay, unit); long dueMillis = now + MILLISECONDS.convert(delay, unit);
ScheduledTask s = new ScheduledTask(task, dueMillis); ScheduledTask s = new ScheduledTask(task, executor, dueMillis);
if (dueMillis <= now) { if (dueMillis <= now) {
scheduledExecutorService.execute(s); scheduledExecutorService.execute(s);
} else { } else {
@@ -93,13 +95,13 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
} }
@Override @Override
public Future<?> scheduleWithFixedDelay(Runnable task, long delay, public Future<?> scheduleWithFixedDelay(Runnable task, Executor executor,
long interval, TimeUnit unit) { long delay, long interval, TimeUnit unit) {
Runnable wrapped = () -> { Runnable wrapped = () -> {
task.run(); task.run();
scheduleWithFixedDelay(task, interval, interval, unit); scheduleWithFixedDelay(task, executor, interval, interval, unit);
}; };
return schedule(wrapped, delay, unit); return schedule(wrapped, executor, delay, unit);
} }
@Override @Override
@@ -175,8 +177,9 @@ class AndroidTaskScheduler implements TaskScheduler, Service, AlarmListener {
private final long dueMillis; private final long dueMillis;
public ScheduledTask(Runnable runnable, long dueMillis) { public ScheduledTask(Runnable runnable, Executor executor,
super(runnable, null); long dueMillis) {
super(() -> executor.execute(runnable), null);
this.dueMillis = dueMillis; this.dueMillis = dueMillis;
} }

View File

@@ -6,11 +6,14 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.PowerManager; import android.os.PowerManager;
import org.briarproject.bramble.api.event.EventExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.AndroidWakeLock; import org.briarproject.bramble.api.system.AndroidWakeLock;
import org.briarproject.bramble.api.system.AndroidWakeLockFactory; import org.briarproject.bramble.api.system.AndroidWakeLockFactory;
import org.briarproject.bramble.api.system.TaskScheduler; import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import javax.inject.Inject; import javax.inject.Inject;
@@ -38,12 +41,15 @@ class AndroidWakeLockFactoryImpl implements AndroidWakeLockFactory {
private final SharedWakeLock sharedWakeLock; private final SharedWakeLock sharedWakeLock;
@Inject @Inject
AndroidWakeLockFactoryImpl(TaskScheduler scheduler, Application app) { AndroidWakeLockFactoryImpl(TaskScheduler scheduler,
@EventExecutor Executor eventExecutor,
Application app) {
PowerManager powerManager = (PowerManager) PowerManager powerManager = (PowerManager)
requireNonNull(app.getSystemService(POWER_SERVICE)); requireNonNull(app.getSystemService(POWER_SERVICE));
String tag = getWakeLockTag(app); String tag = getWakeLockTag(app);
sharedWakeLock = new RenewableWakeLock(powerManager, scheduler, sharedWakeLock = new RenewableWakeLock(powerManager, scheduler,
PARTIAL_WAKE_LOCK, tag, LOCK_DURATION_MS, SAFETY_MARGIN_MS); eventExecutor, PARTIAL_WAKE_LOCK, tag, LOCK_DURATION_MS,
SAFETY_MARGIN_MS);
} }
@Override @Override

View File

@@ -6,6 +6,7 @@ import android.os.PowerManager.WakeLock;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.TaskScheduler; import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -28,6 +29,7 @@ class RenewableWakeLock implements SharedWakeLock {
private final PowerManager powerManager; private final PowerManager powerManager;
private final TaskScheduler scheduler; private final TaskScheduler scheduler;
private final Executor eventExecutor;
private final int levelAndFlags; private final int levelAndFlags;
private final String tag; private final String tag;
private final long durationMs, safetyMarginMs; private final long durationMs, safetyMarginMs;
@@ -44,11 +46,16 @@ class RenewableWakeLock implements SharedWakeLock {
@GuardedBy("lock") @GuardedBy("lock")
private long acquired = 0; private long acquired = 0;
RenewableWakeLock(PowerManager powerManager, TaskScheduler scheduler, RenewableWakeLock(PowerManager powerManager,
int levelAndFlags, String tag, long durationMs, TaskScheduler scheduler,
Executor eventExecutor,
int levelAndFlags,
String tag,
long durationMs,
long safetyMarginMs) { long safetyMarginMs) {
this.powerManager = powerManager; this.powerManager = powerManager;
this.scheduler = scheduler; this.scheduler = scheduler;
this.eventExecutor = eventExecutor;
this.levelAndFlags = levelAndFlags; this.levelAndFlags = levelAndFlags;
this.tag = tag; this.tag = tag;
this.durationMs = durationMs; this.durationMs = durationMs;
@@ -69,8 +76,8 @@ class RenewableWakeLock implements SharedWakeLock {
// power management apps // power management apps
wakeLock.setReferenceCounted(false); wakeLock.setReferenceCounted(false);
wakeLock.acquire(durationMs + safetyMarginMs); wakeLock.acquire(durationMs + safetyMarginMs);
future = scheduler.schedule(this::renew, durationMs, future = scheduler.schedule(this::renew, eventExecutor,
MILLISECONDS); durationMs, MILLISECONDS);
acquired = android.os.SystemClock.elapsedRealtime(); acquired = android.os.SystemClock.elapsedRealtime();
} }
} }
@@ -93,7 +100,8 @@ class RenewableWakeLock implements SharedWakeLock {
wakeLock.setReferenceCounted(false); wakeLock.setReferenceCounted(false);
wakeLock.acquire(durationMs + safetyMarginMs); wakeLock.acquire(durationMs + safetyMarginMs);
oldWakeLock.release(); oldWakeLock.release();
future = scheduler.schedule(this::renew, durationMs, MILLISECONDS); future = scheduler.schedule(this::renew, eventExecutor,
durationMs, MILLISECONDS);
acquired = now; acquired = now;
} }
} }

View File

@@ -2,8 +2,8 @@ package org.briarproject.bramble.api.system;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@@ -16,13 +16,16 @@ import java.util.concurrent.TimeUnit;
public interface TaskScheduler { public interface TaskScheduler {
/** /**
* See {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)}. * Submits the given task to the given executor after the given delay.
*/ */
Future<?> schedule(Runnable task, long delay, TimeUnit unit); Future<?> schedule(Runnable task, Executor executor, long delay,
TimeUnit unit);
/** /**
* See {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}. * Submits the given task to the given executor after the given delay,
* and then repeatedly with the given interval between executions
* (measured from the end of one execution to the beginning of the next).
*/ */
Future<?> scheduleWithFixedDelay(Runnable task, long delay, Future<?> scheduleWithFixedDelay(Runnable task, Executor executor,
long interval, TimeUnit unit); long delay, long interval, TimeUnit unit);
} }

View File

@@ -55,7 +55,8 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
synchronized (lock) { synchronized (lock) {
if (streams.isEmpty()) { if (streams.isEmpty()) {
task = scheduler.scheduleWithFixedDelay(this::checkTimeouts, task = scheduler.scheduleWithFixedDelay(this::checkTimeouts,
CHECK_INTERVAL_MS, CHECK_INTERVAL_MS, MILLISECONDS); ioExecutor, CHECK_INTERVAL_MS, CHECK_INTERVAL_MS,
MILLISECONDS);
} }
streams.add(stream); streams.add(stream);
} }
@@ -73,23 +74,21 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
if (toCancel != null) toCancel.cancel(false); if (toCancel != null) toCancel.cancel(false);
} }
// Scheduler @IoExecutor
private void checkTimeouts() { private void checkTimeouts() {
ioExecutor.execute(() -> { List<TimeoutInputStream> snapshot;
List<TimeoutInputStream> snapshot; synchronized (lock) {
synchronized (lock) { snapshot = new ArrayList<>(streams);
snapshot = new ArrayList<>(streams); }
} for (TimeoutInputStream stream : snapshot) {
for (TimeoutInputStream stream : snapshot) { if (stream.hasTimedOut()) {
if (stream.hasTimedOut()) { LOG.info("Input stream has timed out");
LOG.info("Input stream has timed out"); try {
try { stream.close();
stream.close(); } catch (IOException e) {
} catch (IOException e) { logException(LOG, INFO, e);
logException(LOG, INFO, e);
}
} }
} }
}); }
} }
} }

View File

@@ -118,6 +118,7 @@ class PollerImpl implements Poller, EventListener {
} }
} }
// TODO: Make this wakeful
private void connectToContact(ContactId c) { private void connectToContact(ContactId c) {
for (SimplexPlugin s : pluginManager.getSimplexPlugins()) for (SimplexPlugin s : pluginManager.getSimplexPlugins())
if (s.shouldPoll()) connectToContact(c, s); if (s.shouldPoll()) connectToContact(c, s);
@@ -189,8 +190,8 @@ class PollerImpl implements Poller, EventListener {
// it will abort safely when it finds it's been replaced // it will abort safely when it finds it's been replaced
if (scheduled != null) scheduled.future.cancel(false); if (scheduled != null) scheduled.future.cancel(false);
PollTask task = new PollTask(p, due, randomiseNext); PollTask task = new PollTask(p, due, randomiseNext);
Future<?> future = scheduler.schedule(() -> Future<?> future = scheduler.schedule(task, ioExecutor, delay,
ioExecutor.execute(task), delay, MILLISECONDS); MILLISECONDS);
tasks.put(t, new ScheduledPollTask(task, future)); tasks.put(t, new ScheduledPollTask(task, future));
} }
} finally { } finally {
@@ -233,9 +234,9 @@ class PollerImpl implements Poller, EventListener {
private class ScheduledPollTask { private class ScheduledPollTask {
private final PollTask task; private final PollTask task;
private final Future future; private final Future<?> future;
private ScheduledPollTask(PollTask task, Future future) { private ScheduledPollTask(PollTask task, Future<?> future) {
this.task = task; this.task = task;
this.future = future; this.future = future;
} }

View File

@@ -143,8 +143,8 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
} catch (DbException e) { } catch (DbException e) {
throw new ServiceException(e); throw new ServiceException(e);
} }
scheduler.scheduleWithFixedDelay(this::poll, POLLING_INTERVAL_MS, scheduler.scheduleWithFixedDelay(this::poll, worker,
POLLING_INTERVAL_MS, MILLISECONDS); POLLING_INTERVAL_MS, POLLING_INTERVAL_MS, MILLISECONDS);
} }
@EventExecutor @EventExecutor
@@ -204,12 +204,10 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
return plugin.createRendezvousEndpoint(k, cs.alice, h); return plugin.createRendezvousEndpoint(k, cs.alice, h);
} }
// Scheduler // Worker
private void poll() { private void poll() {
worker.execute(() -> { removeExpiredPendingContacts();
removeExpiredPendingContacts(); for (PluginState ps : pluginStates.values()) poll(ps);
for (PluginState ps : pluginStates.values()) poll(ps);
});
} }
// Worker // Worker

View File

@@ -3,6 +3,7 @@ package org.briarproject.bramble.system;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.system.TaskScheduler; import org.briarproject.bramble.api.system.TaskScheduler;
import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -10,8 +11,7 @@ import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
/** /**
* A {@link TaskScheduler} that delegates all calls to a * A {@link TaskScheduler} that uses a {@link ScheduledExecutorService}.
* {@link ScheduledExecutorService}.
*/ */
@ThreadSafe @ThreadSafe
@NotNullByDefault @NotNullByDefault
@@ -24,13 +24,16 @@ class TaskSchedulerImpl implements TaskScheduler {
} }
@Override @Override
public Future<?> schedule(Runnable task, long delay, TimeUnit unit) { public Future<?> schedule(Runnable task, Executor executor, long delay,
return delegate.schedule(task, delay, unit); TimeUnit unit) {
Runnable execute = () -> executor.execute(task);
return delegate.schedule(execute, delay, unit);
} }
@Override @Override
public Future<?> scheduleWithFixedDelay(Runnable task, long delay, public Future<?> scheduleWithFixedDelay(Runnable task, Executor executor,
long interval, TimeUnit unit) { long delay, long interval, TimeUnit unit) {
return delegate.scheduleWithFixedDelay(task, delay, interval, unit); Runnable execute = () -> executor.execute(task);
return delegate.scheduleWithFixedDelay(execute, delay, interval, unit);
} }
} }

View File

@@ -6,6 +6,7 @@ import org.briarproject.bramble.api.contact.PendingContactId;
import org.briarproject.bramble.api.crypto.SecretKey; import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.crypto.TransportCrypto; import org.briarproject.bramble.api.crypto.TransportCrypto;
import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.Transaction; import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -198,17 +199,16 @@ class TransportKeyManagerImpl implements TransportKeyManager {
private void scheduleKeyUpdate(long now) { private void scheduleKeyUpdate(long now) {
long delay = timePeriodLength - now % timePeriodLength; long delay = timePeriodLength - now % timePeriodLength;
scheduler.schedule(this::updateKeys, delay, MILLISECONDS); scheduler.schedule(this::updateKeys, dbExecutor, delay, MILLISECONDS);
} }
@DatabaseExecutor
private void updateKeys() { private void updateKeys() {
dbExecutor.execute(() -> { try {
try { db.transaction(false, this::updateKeys);
db.transaction(false, this::updateKeys); } catch (DbException e) {
} catch (DbException e) { logException(LOG, WARNING, e);
logException(LOG, WARNING, e); }
}
});
} }
@Override @Override

View File

@@ -234,7 +234,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) pollingInterval), with(MILLISECONDS)); with(ioExecutor), with((long) pollingInterval),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
}}); }});
@@ -262,7 +263,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) pollingInterval), with(MILLISECONDS)); with(ioExecutor), with((long) pollingInterval),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
// Second event // Second event
// Get the plugin // Get the plugin
@@ -304,7 +306,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) pollingInterval), with(MILLISECONDS)); with(ioExecutor), with((long) pollingInterval),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
// Second event // Second event
// Get the plugin // Get the plugin
@@ -320,7 +323,8 @@ public class PollerImplTest extends BrambleMockTestCase {
will(returnValue(now + 1)); will(returnValue(now + 1));
oneOf(future).cancel(false); oneOf(future).cancel(false);
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) pollingInterval - 2), with(MILLISECONDS)); with(ioExecutor), with((long) pollingInterval - 2),
with(MILLISECONDS));
}}); }});
poller.eventOccurred(new ConnectionOpenedEvent(contactId, transportId, poller.eventOccurred(new ConnectionOpenedEvent(contactId, transportId,
@@ -345,8 +349,8 @@ public class PollerImplTest extends BrambleMockTestCase {
// Schedule a polling task immediately // Schedule a polling task immediately
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(MILLISECONDS)); with(ioExecutor), with(0L), with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
will(new RunAction()); will(new RunAction());
// Running the polling task schedules the next polling task // Running the polling task schedules the next polling task
@@ -357,7 +361,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); with(ioExecutor), with((long) (pollingInterval * 0.5)),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
// Get the transport properties and connected contacts // Get the transport properties and connected contacts
oneOf(transportPropertyManager).getRemoteProperties(transportId); oneOf(transportPropertyManager).getRemoteProperties(transportId);
@@ -388,8 +393,8 @@ public class PollerImplTest extends BrambleMockTestCase {
// Schedule a polling task immediately // Schedule a polling task immediately
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(MILLISECONDS)); with(ioExecutor), with(0L), with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
will(new RunAction()); will(new RunAction());
// Running the polling task schedules the next polling task // Running the polling task schedules the next polling task
@@ -400,7 +405,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) (pollingInterval * 0.5)), with(MILLISECONDS)); with(ioExecutor), with((long) (pollingInterval * 0.5)),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
// Get the transport properties and connected contacts // Get the transport properties and connected contacts
oneOf(transportPropertyManager).getRemoteProperties(transportId); oneOf(transportPropertyManager).getRemoteProperties(transportId);
@@ -429,8 +435,8 @@ public class PollerImplTest extends BrambleMockTestCase {
// Schedule a polling task immediately // Schedule a polling task immediately
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(MILLISECONDS)); with(ioExecutor), with(0L), with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
// The plugin is deactivated before the task runs - cancel the task // The plugin is deactivated before the task runs - cancel the task
oneOf(future).cancel(false); oneOf(future).cancel(false);
@@ -454,7 +460,8 @@ public class PollerImplTest extends BrambleMockTestCase {
oneOf(clock).currentTimeMillis(); oneOf(clock).currentTimeMillis();
will(returnValue(now)); will(returnValue(now));
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with((long) pollingInterval), with(MILLISECONDS)); with(ioExecutor), with((long) pollingInterval),
with(MILLISECONDS));
will(returnValue(future)); will(returnValue(future));
}}); }});
} }

View File

@@ -123,8 +123,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
e.getPendingContactState() == OFFLINE))); e.getPendingContactState() == OFFLINE)));
// Capture the poll task // Capture the poll task
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)), oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS), with(any(Executor.class)), with(POLLING_INTERVAL_MS),
with(MILLISECONDS)); with(POLLING_INTERVAL_MS), with(MILLISECONDS));
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class, will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
0)); 0));
}}); }});
@@ -159,8 +159,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
e.getPendingContactState() == FAILED))); e.getPendingContactState() == FAILED)));
// Schedule the poll task // Schedule the poll task
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)), oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS), with(any(Executor.class)), with(POLLING_INTERVAL_MS),
with(MILLISECONDS)); with(POLLING_INTERVAL_MS), with(MILLISECONDS));
}}); }});
rendezvousPoller.startService(); rendezvousPoller.startService();
@@ -468,8 +468,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
will(returnValue(emptyList())); will(returnValue(emptyList()));
// Capture the poll task // Capture the poll task
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)), oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS), with(any(Executor.class)), with(POLLING_INTERVAL_MS),
with(MILLISECONDS)); with(POLLING_INTERVAL_MS), with(MILLISECONDS));
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class, will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
0)); 0));
}}); }});
@@ -545,8 +545,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
e.getPendingContactState() == OFFLINE))); e.getPendingContactState() == OFFLINE)));
// Capture the poll task // Capture the poll task
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)), oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
with(POLLING_INTERVAL_MS), with(POLLING_INTERVAL_MS), with(any(Executor.class)), with(POLLING_INTERVAL_MS),
with(MILLISECONDS)); with(POLLING_INTERVAL_MS), with(MILLISECONDS));
will(new CaptureArgumentAction<>(capturePollTask, Runnable.class, will(new CaptureArgumentAction<>(capturePollTask, Runnable.class,
0)); 0));
}}); }});

View File

@@ -117,7 +117,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
new TransportKeySet(keySetId, contactId, null, updated))); new TransportKeySet(keySetId, contactId, null, updated)));
// Schedule a key update at the start of the next time period // Schedule a key update at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(timePeriodLength - 1), with(MILLISECONDS)); with(dbExecutor), with(timePeriodLength - 1),
with(MILLISECONDS));
}}); }});
transportKeyManager.start(txn); transportKeyManager.start(txn);
@@ -420,7 +421,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
} }
// Schedule a key update at the start of the next time period // Schedule a key update at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(timePeriodLength), with(MILLISECONDS)); with(dbExecutor), with(timePeriodLength),
with(MILLISECONDS));
will(new RunAction()); will(new RunAction());
oneOf(dbExecutor).execute(with(any(Runnable.class))); oneOf(dbExecutor).execute(with(any(Runnable.class)));
will(new RunAction()); will(new RunAction());
@@ -445,7 +447,8 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
new TransportKeySet(keySetId, contactId, null, updated))); new TransportKeySet(keySetId, contactId, null, updated)));
// Schedule a key update at the start of the next time period // Schedule a key update at the start of the next time period
oneOf(scheduler).schedule(with(any(Runnable.class)), oneOf(scheduler).schedule(with(any(Runnable.class)),
with(timePeriodLength), with(MILLISECONDS)); with(dbExecutor), with(timePeriodLength),
with(MILLISECONDS));
}}); }});
transportKeyManager.start(txn); transportKeyManager.start(txn);

View File

@@ -146,9 +146,8 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
private void startFeedExecutor() { private void startFeedExecutor() {
if (fetcherStarted.getAndSet(true)) return; if (fetcherStarted.getAndSet(true)) return;
LOG.info("Tor started, scheduling RSS feed fetcher"); LOG.info("Tor started, scheduling RSS feed fetcher");
Runnable fetcher = () -> ioExecutor.execute(this::fetchFeeds); scheduler.scheduleWithFixedDelay(this::fetchFeeds, ioExecutor,
scheduler.scheduleWithFixedDelay(fetcher, FETCH_DELAY_INITIAL, FETCH_DELAY_INITIAL, FETCH_INTERVAL, FETCH_UNIT);
FETCH_INTERVAL, FETCH_UNIT);
} }
@Override @Override
@@ -471,6 +470,7 @@ class FeedManagerImpl implements FeedManager, EventListener, OpenDatabaseHook,
if (date == null) time = now; if (date == null) time = now;
else time = Math.max(0, Math.min(date.getTime(), now)); else time = Math.max(0, Math.min(date.getTime(), now));
String text = getPostText(b.toString()); String text = getPostText(b.toString());
//noinspection TryWithIdenticalCatches
try { try {
// create and store post // create and store post
LocalAuthor localAuthor = feed.getLocalAuthor(); LocalAuthor localAuthor = feed.getLocalAuthor();