Wrapped the system clock in an interface so it can be replaced in tests.

This commit is contained in:
akwizgran
2012-09-06 17:21:03 +01:00
parent 67eb9d6f93
commit 960ead0247
26 changed files with 115 additions and 53 deletions

View File

@@ -2,8 +2,10 @@ package net.sf.briar.api.plugins.duplex;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import net.sf.briar.clock.Clock;
public interface DuplexPluginFactory { public interface DuplexPluginFactory {
DuplexPlugin createPlugin(Executor pluginExecutor, DuplexPlugin createPlugin(Executor pluginExecutor, Clock clock,
DuplexPluginCallback callback); DuplexPluginCallback callback);
} }

View File

@@ -2,8 +2,10 @@ package net.sf.briar.api.plugins.simplex;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import net.sf.briar.clock.Clock;
public interface SimplexPluginFactory { public interface SimplexPluginFactory {
SimplexPlugin createPlugin(Executor pluginExecutor, SimplexPlugin createPlugin(Executor pluginExecutor, Clock clock,
SimplexPluginCallback callback); SimplexPluginCallback callback);
} }

View File

@@ -0,0 +1,11 @@
package net.sf.briar.clock;
/**
* An interface for time-related system functions that allows them to be
* replaced for testing.
*/
public interface Clock {
/** @see {@link java.lang.System#currentTimeMillis()} */
long currentTimeMillis();
}

View File

@@ -0,0 +1,9 @@
package net.sf.briar.clock;
/** Default clock implementation. */
public class SystemClock implements Clock {
public long currentTimeMillis() {
return System.currentTimeMillis();
}
}

View File

@@ -0,0 +1,11 @@
package net.sf.briar.clock;
import com.google.inject.AbstractModule;
public class ClockModule extends AbstractModule {
@Override
protected void configure() {
bind(Clock.class).to(SystemClock.class);
}
}

View File

@@ -61,6 +61,7 @@ import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.TransportUpdate; import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.transport.ConnectionContext; import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionWindow; import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.clock.Clock;
import net.sf.briar.util.ByteUtils; import net.sf.briar.util.ByteUtils;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -103,6 +104,7 @@ DatabaseCleaner.Callback {
private final DatabaseCleaner cleaner; private final DatabaseCleaner cleaner;
private final ShutdownManager shutdown; private final ShutdownManager shutdown;
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
private final Clock clock;
private final Collection<DatabaseListener> listeners = private final Collection<DatabaseListener> listeners =
new CopyOnWriteArrayList<DatabaseListener>(); new CopyOnWriteArrayList<DatabaseListener>();
@@ -117,11 +119,13 @@ DatabaseCleaner.Callback {
@Inject @Inject
DatabaseComponentImpl(Database<T> db, DatabaseCleaner cleaner, DatabaseComponentImpl(Database<T> db, DatabaseCleaner cleaner,
ShutdownManager shutdown, PacketFactory packetFactory) { ShutdownManager shutdown, PacketFactory packetFactory,
Clock clock) {
this.db = db; this.db = db;
this.cleaner = cleaner; this.cleaner = cleaner;
this.shutdown = shutdown; this.shutdown = shutdown;
this.packetFactory = packetFactory; this.packetFactory = packetFactory;
this.clock = clock;
} }
public void open(boolean resume) throws DbException, IOException { public void open(boolean resume) throws DbException, IOException {
@@ -632,7 +636,7 @@ DatabaseCleaner.Callback {
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
timestamp = System.currentTimeMillis() - 1; timestamp = clock.currentTimeMillis() - 1;
holes = db.getVisibleHoles(txn, c, timestamp); holes = db.getVisibleHoles(txn, c, timestamp);
subs = db.getVisibleSubscriptions(txn, c, timestamp); subs = db.getVisibleSubscriptions(txn, c, timestamp);
expiry = db.getExpiryTime(txn); expiry = db.getExpiryTime(txn);
@@ -652,7 +656,7 @@ DatabaseCleaner.Callback {
} }
private boolean updateIsDue(long sent) { private boolean updateIsDue(long sent) {
long now = System.currentTimeMillis(); long now = clock.currentTimeMillis();
return now - sent >= DatabaseConstants.MAX_UPDATE_INTERVAL; return now - sent >= DatabaseConstants.MAX_UPDATE_INTERVAL;
} }
@@ -686,7 +690,7 @@ DatabaseCleaner.Callback {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
transports = db.getLocalTransports(txn); transports = db.getLocalTransports(txn);
timestamp = System.currentTimeMillis(); timestamp = clock.currentTimeMillis();
db.setTransportsSent(txn, c, timestamp); db.setTransportsSent(txn, c, timestamp);
db.commitTransaction(txn); db.commitTransaction(txn);
} catch(DbException e) { } catch(DbException e) {
@@ -1328,7 +1332,7 @@ DatabaseCleaner.Callback {
try { try {
if(!p.equals(db.getLocalProperties(txn, t))) { if(!p.equals(db.getLocalProperties(txn, t))) {
db.setLocalProperties(txn, t, p); db.setLocalProperties(txn, t, p);
db.setTransportsModified(txn, System.currentTimeMillis()); db.setTransportsModified(txn, clock.currentTimeMillis());
changed = true; changed = true;
} }
db.commitTransaction(txn); db.commitTransaction(txn);
@@ -1606,7 +1610,7 @@ DatabaseCleaner.Callback {
public boolean shouldCheckFreeSpace() { public boolean shouldCheckFreeSpace() {
synchronized(spaceLock) { synchronized(spaceLock) {
long now = System.currentTimeMillis(); long now = clock.currentTimeMillis();
if(bytesStoredSinceLastCheck > MAX_BYTES_BETWEEN_SPACE_CHECKS if(bytesStoredSinceLastCheck > MAX_BYTES_BETWEEN_SPACE_CHECKS
|| now - timeOfLastCheck > MAX_MS_BETWEEN_SPACE_CHECKS) { || now - timeOfLastCheck > MAX_MS_BETWEEN_SPACE_CHECKS) {
bytesStoredSinceLastCheck = 0L; bytesStoredSinceLastCheck = 0L;

View File

@@ -15,6 +15,7 @@ import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.PacketFactory; import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.transport.ConnectionContextFactory; import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindowFactory; import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.clock.Clock;
import net.sf.briar.util.BoundedExecutor; import net.sf.briar.util.BoundedExecutor;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@@ -51,16 +52,16 @@ public class DatabaseModule extends AbstractModule {
@DatabasePassword Password password, @DatabaseMaxSize long maxSize, @DatabasePassword Password password, @DatabaseMaxSize long maxSize,
ConnectionContextFactory connectionContextFactory, ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory, ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory) { GroupFactory groupFactory, Clock clock) {
return new H2Database(dir, password, maxSize, connectionContextFactory, return new H2Database(dir, password, maxSize, connectionContextFactory,
connectionWindowFactory, groupFactory); connectionWindowFactory, groupFactory, clock);
} }
@Provides @Singleton @Provides @Singleton
DatabaseComponent getDatabaseComponent(Database<Connection> db, DatabaseComponent getDatabaseComponent(Database<Connection> db,
DatabaseCleaner cleaner, ShutdownManager shutdown, DatabaseCleaner cleaner, ShutdownManager shutdown,
PacketFactory packetFactory) { PacketFactory packetFactory, Clock clock) {
return new DatabaseComponentImpl<Connection>(db, cleaner, shutdown, return new DatabaseComponentImpl<Connection>(db, cleaner, shutdown,
packetFactory); packetFactory, clock);
} }
} }

View File

@@ -16,6 +16,7 @@ import net.sf.briar.api.db.DbException;
import net.sf.briar.api.protocol.GroupFactory; import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.transport.ConnectionContextFactory; import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindowFactory; import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.clock.Clock;
import org.apache.commons.io.FileSystemUtils; import org.apache.commons.io.FileSystemUtils;
@@ -40,9 +41,9 @@ class H2Database extends JdbcDatabase {
@DatabaseMaxSize long maxSize, @DatabaseMaxSize long maxSize,
ConnectionContextFactory connectionContextFactory, ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory, ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory) { GroupFactory groupFactory, Clock clock) {
super(connectionContextFactory, connectionWindowFactory, groupFactory, super(connectionContextFactory, connectionWindowFactory, groupFactory,
HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE); clock, HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE);
home = new File(dir, "db"); home = new File(dir, "db");
this.password = password; this.password = password;
url = "jdbc:h2:split:" + home.getPath() url = "jdbc:h2:split:" + home.getPath()

View File

@@ -42,6 +42,7 @@ import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionContextFactory; import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindow; import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.api.transport.ConnectionWindowFactory; import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.clock.Clock;
import net.sf.briar.util.FileUtils; import net.sf.briar.util.FileUtils;
/** /**
@@ -275,6 +276,7 @@ abstract class JdbcDatabase implements Database<Connection> {
private final ConnectionContextFactory connectionContextFactory; private final ConnectionContextFactory connectionContextFactory;
private final ConnectionWindowFactory connectionWindowFactory; private final ConnectionWindowFactory connectionWindowFactory;
private final GroupFactory groupFactory; private final GroupFactory groupFactory;
private final Clock clock;
// Different database libraries use different names for certain types // Different database libraries use different names for certain types
private final String hashType, binaryType, counterType, secretType; private final String hashType, binaryType, counterType, secretType;
@@ -288,11 +290,12 @@ abstract class JdbcDatabase implements Database<Connection> {
JdbcDatabase(ConnectionContextFactory connectionContextFactory, JdbcDatabase(ConnectionContextFactory connectionContextFactory,
ConnectionWindowFactory connectionWindowFactory, ConnectionWindowFactory connectionWindowFactory,
GroupFactory groupFactory, String hashType, String binaryType, GroupFactory groupFactory, Clock clock, String hashType,
String counterType, String secretType) { String binaryType, String counterType, String secretType) {
this.connectionContextFactory = connectionContextFactory; this.connectionContextFactory = connectionContextFactory;
this.connectionWindowFactory = connectionWindowFactory; this.connectionWindowFactory = connectionWindowFactory;
this.groupFactory = groupFactory; this.groupFactory = groupFactory;
this.clock = clock;
this.hashType = hashType; this.hashType = hashType;
this.binaryType = binaryType; this.binaryType = binaryType;
this.counterType = counterType; this.counterType = counterType;
@@ -654,7 +657,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, b.getBytes()); ps.setBytes(1, b.getBytes());
ps.setInt(2, c.getInt()); ps.setInt(2, c.getInt());
ps.setLong(3, System.currentTimeMillis()); ps.setLong(3, clock.currentTimeMillis());
int affected = ps.executeUpdate(); int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException(); if(affected != 1) throw new DbStateException();
ps.close(); ps.close();
@@ -741,7 +744,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.setBytes(1, g.getId().getBytes()); ps.setBytes(1, g.getId().getBytes());
ps.setString(2, g.getName()); ps.setString(2, g.getName());
ps.setBytes(3, g.getPublicKey()); ps.setBytes(3, g.getPublicKey());
long now = System.currentTimeMillis(); long now = clock.currentTimeMillis();
ps.setLong(4, now); ps.setLong(4, now);
int affected = ps.executeUpdate(); int affected = ps.executeUpdate();
if(affected != 1) throw new DbStateException(); if(affected != 1) throw new DbStateException();
@@ -2230,7 +2233,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet rs = null; ResultSet rs = null;
try { try {
// Remove the group ID from the visibility lists // Remove the group ID from the visibility lists
long now = System.currentTimeMillis(); long now = clock.currentTimeMillis();
String sql = "SELECT contactId, nextId FROM visibilities" String sql = "SELECT contactId, nextId FROM visibilities"
+ " WHERE groupId = ?"; + " WHERE groupId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
@@ -2342,7 +2345,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
if(nextId == null) ps.setNull(1, Types.BINARY); // At the tail if(nextId == null) ps.setNull(1, Types.BINARY); // At the tail
else ps.setBytes(1, nextId); // At the head or in the middle else ps.setBytes(1, nextId); // At the head or in the middle
ps.setLong(2, System.currentTimeMillis()); ps.setLong(2, clock.currentTimeMillis());
ps.setInt(3, c.getInt()); ps.setInt(3, c.getInt());
ps.setBytes(4, g.getBytes()); ps.setBytes(4, g.getBytes());
affected = ps.executeUpdate(); affected = ps.executeUpdate();

View File

@@ -12,8 +12,6 @@ import java.security.KeyPair;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import javax.inject.Inject;
import net.sf.briar.api.crypto.CryptoComponent; import net.sf.briar.api.crypto.CryptoComponent;
import net.sf.briar.api.crypto.MessageDigest; import net.sf.briar.api.crypto.MessageDigest;
import net.sf.briar.api.crypto.PseudoRandom; import net.sf.briar.api.crypto.PseudoRandom;
@@ -32,6 +30,8 @@ import net.sf.briar.api.serial.Writer;
import net.sf.briar.api.serial.WriterFactory; import net.sf.briar.api.serial.WriterFactory;
import net.sf.briar.util.ByteUtils; import net.sf.briar.util.ByteUtils;
import com.google.inject.Inject;
class InvitationStarterImpl implements InvitationStarter { class InvitationStarterImpl implements InvitationStarter {
private static final String TIMED_OUT = "INVITATION_TIMED_OUT"; private static final String TIMED_OUT = "INVITATION_TIMED_OUT";

View File

@@ -35,6 +35,7 @@ import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex; import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionDispatcher; import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.ui.UiCallback; import net.sf.briar.api.ui.UiCallback;
import net.sf.briar.clock.Clock;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -54,6 +55,7 @@ class PluginManagerImpl implements PluginManager {
}; };
private final ExecutorService pluginExecutor; private final ExecutorService pluginExecutor;
private final Clock clock;
private final DatabaseComponent db; private final DatabaseComponent db;
private final Poller poller; private final Poller poller;
private final ConnectionDispatcher dispatcher; private final ConnectionDispatcher dispatcher;
@@ -63,9 +65,10 @@ class PluginManagerImpl implements PluginManager {
@Inject @Inject
PluginManagerImpl(@PluginExecutor ExecutorService pluginExecutor, PluginManagerImpl(@PluginExecutor ExecutorService pluginExecutor,
DatabaseComponent db, Poller poller, Clock clock, DatabaseComponent db, Poller poller,
ConnectionDispatcher dispatcher, UiCallback uiCallback) { ConnectionDispatcher dispatcher, UiCallback uiCallback) {
this.pluginExecutor = pluginExecutor; this.pluginExecutor = pluginExecutor;
this.clock = clock;
this.db = db; this.db = db;
this.poller = poller; this.poller = poller;
this.dispatcher = dispatcher; this.dispatcher = dispatcher;
@@ -88,7 +91,7 @@ class PluginManagerImpl implements PluginManager {
(SimplexPluginFactory) c.newInstance(); (SimplexPluginFactory) c.newInstance();
SimplexCallback callback = new SimplexCallback(); SimplexCallback callback = new SimplexCallback();
SimplexPlugin plugin = factory.createPlugin(pluginExecutor, SimplexPlugin plugin = factory.createPlugin(pluginExecutor,
callback); clock, callback);
if(plugin == null) { if(plugin == null) {
if(LOG.isLoggable(Level.INFO)) { if(LOG.isLoggable(Level.INFO)) {
LOG.info(factory.getClass().getSimpleName() LOG.info(factory.getClass().getSimpleName()
@@ -128,7 +131,7 @@ class PluginManagerImpl implements PluginManager {
(DuplexPluginFactory) c.newInstance(); (DuplexPluginFactory) c.newInstance();
DuplexCallback callback = new DuplexCallback(); DuplexCallback callback = new DuplexCallback();
DuplexPlugin plugin = factory.createPlugin(pluginExecutor, DuplexPlugin plugin = factory.createPlugin(pluginExecutor,
callback); clock, callback);
if(plugin == null) { if(plugin == null) {
if(LOG.isLoggable(Level.INFO)) { if(LOG.isLoggable(Level.INFO)) {
LOG.info(factory.getClass().getSimpleName() LOG.info(factory.getClass().getSimpleName()

View File

@@ -32,6 +32,7 @@ import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexTransportConnection; import net.sf.briar.api.plugins.duplex.DuplexTransportConnection;
import net.sf.briar.api.protocol.TransportId; import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.clock.Clock;
import net.sf.briar.util.OsUtils; import net.sf.briar.util.OsUtils;
import net.sf.briar.util.StringUtils; import net.sf.briar.util.StringUtils;
@@ -47,6 +48,7 @@ class BluetoothPlugin implements DuplexPlugin {
Logger.getLogger(BluetoothPlugin.class.getName()); Logger.getLogger(BluetoothPlugin.class.getName());
private final Executor pluginExecutor; private final Executor pluginExecutor;
private final Clock clock;
private final DuplexPluginCallback callback; private final DuplexPluginCallback callback;
private final long pollingInterval; private final long pollingInterval;
private final Object discoveryLock = new Object(); private final Object discoveryLock = new Object();
@@ -58,9 +60,10 @@ class BluetoothPlugin implements DuplexPlugin {
private LocalDevice localDevice = null; // Locking: this private LocalDevice localDevice = null; // Locking: this
private StreamConnectionNotifier socket = null; // Locking: this private StreamConnectionNotifier socket = null; // Locking: this
BluetoothPlugin(@PluginExecutor Executor pluginExecutor, BluetoothPlugin(@PluginExecutor Executor pluginExecutor, Clock clock,
DuplexPluginCallback callback, long pollingInterval) { DuplexPluginCallback callback, long pollingInterval) {
this.pluginExecutor = pluginExecutor; this.pluginExecutor = pluginExecutor;
this.clock = clock;
this.callback = callback; this.callback = callback;
this.pollingInterval = pollingInterval; this.pollingInterval = pollingInterval;
scheduler = Executors.newScheduledThreadPool(0); scheduler = Executors.newScheduledThreadPool(0);
@@ -360,9 +363,9 @@ class BluetoothPlugin implements DuplexPlugin {
} }
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent(); DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
// Try to discover the other party until the invitation times out // Try to discover the other party until the invitation times out
long end = System.currentTimeMillis() + c.getTimeout(); long end = clock.currentTimeMillis() + c.getTimeout();
String url = null; String url = null;
while(url == null && System.currentTimeMillis() < end) { while(url == null && clock.currentTimeMillis() < end) {
InvitationListener listener = new InvitationListener(discoveryAgent, InvitationListener listener = new InvitationListener(discoveryAgent,
c.getUuid()); c.getUuid());
// FIXME: Avoid making alien calls with a lock held // FIXME: Avoid making alien calls with a lock held

View File

@@ -6,13 +6,15 @@ import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPlugin; import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.clock.Clock;
public class BluetoothPluginFactory implements DuplexPluginFactory { public class BluetoothPluginFactory implements DuplexPluginFactory {
private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins private static final long POLLING_INTERVAL = 3L * 60L * 1000L; // 3 mins
public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor, public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback) { Clock clock, DuplexPluginCallback callback) {
return new BluetoothPlugin(pluginExecutor, callback, POLLING_INTERVAL); return new BluetoothPlugin(pluginExecutor, clock, callback,
POLLING_INTERVAL);
} }
} }

View File

@@ -5,10 +5,11 @@ import java.util.concurrent.Executor;
import net.sf.briar.api.plugins.simplex.SimplexPlugin; import net.sf.briar.api.plugins.simplex.SimplexPlugin;
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexPluginFactory; import net.sf.briar.api.plugins.simplex.SimplexPluginFactory;
import net.sf.briar.clock.Clock;
public class GmailPluginFactory implements SimplexPluginFactory { public class GmailPluginFactory implements SimplexPluginFactory {
public SimplexPlugin createPlugin(Executor pluginExecutor, public SimplexPlugin createPlugin(Executor pluginExecutor, Clock clock,
SimplexPluginCallback callback) { SimplexPluginCallback callback) {
return new GmailPlugin(pluginExecutor, callback); return new GmailPlugin(pluginExecutor, callback);

View File

@@ -6,6 +6,7 @@ import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.simplex.SimplexPlugin; import net.sf.briar.api.plugins.simplex.SimplexPlugin;
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexPluginFactory; import net.sf.briar.api.plugins.simplex.SimplexPluginFactory;
import net.sf.briar.clock.Clock;
import net.sf.briar.util.OsUtils; import net.sf.briar.util.OsUtils;
public class RemovableDrivePluginFactory implements SimplexPluginFactory { public class RemovableDrivePluginFactory implements SimplexPluginFactory {
@@ -13,7 +14,7 @@ public class RemovableDrivePluginFactory implements SimplexPluginFactory {
private static final long POLLING_INTERVAL = 10L * 1000L; // 10 seconds private static final long POLLING_INTERVAL = 10L * 1000L; // 10 seconds
public SimplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor, public SimplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
SimplexPluginCallback callback) { Clock clock, SimplexPluginCallback callback) {
RemovableDriveFinder finder; RemovableDriveFinder finder;
RemovableDriveMonitor monitor; RemovableDriveMonitor monitor;
if(OsUtils.isLinux()) { if(OsUtils.isLinux()) {

View File

@@ -6,13 +6,14 @@ import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPlugin; import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.clock.Clock;
public class SimpleSocketPluginFactory implements DuplexPluginFactory { public class SimpleSocketPluginFactory implements DuplexPluginFactory {
private static final long POLLING_INTERVAL = 5L * 60L * 1000L; // 5 mins private static final long POLLING_INTERVAL = 5L * 60L * 1000L; // 5 mins
public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor, public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback) { Clock clock, DuplexPluginCallback callback) {
return new SimpleSocketPlugin(pluginExecutor, callback, return new SimpleSocketPlugin(pluginExecutor, callback,
POLLING_INTERVAL); POLLING_INTERVAL);
} }

View File

@@ -6,13 +6,14 @@ import net.sf.briar.api.plugins.PluginExecutor;
import net.sf.briar.api.plugins.duplex.DuplexPlugin; import net.sf.briar.api.plugins.duplex.DuplexPlugin;
import net.sf.briar.api.plugins.duplex.DuplexPluginCallback; import net.sf.briar.api.plugins.duplex.DuplexPluginCallback;
import net.sf.briar.api.plugins.duplex.DuplexPluginFactory; import net.sf.briar.api.plugins.duplex.DuplexPluginFactory;
import net.sf.briar.clock.Clock;
public class TorPluginFactory implements DuplexPluginFactory { public class TorPluginFactory implements DuplexPluginFactory {
private static final long POLLING_INTERVAL = 15L * 60L * 1000L; // 15 mins private static final long POLLING_INTERVAL = 15L * 60L * 1000L; // 15 mins
public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor, public DuplexPlugin createPlugin(@PluginExecutor Executor pluginExecutor,
DuplexPluginCallback callback) { Clock clock, DuplexPluginCallback callback) {
return new TorPlugin(pluginExecutor, callback, POLLING_INTERVAL); return new TorPlugin(pluginExecutor, callback, POLLING_INTERVAL);
} }
} }

View File

@@ -46,6 +46,7 @@ import net.sf.briar.api.transport.ConnectionReader;
import net.sf.briar.api.transport.ConnectionReaderFactory; import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionWriter; import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.clock.ClockModule;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.db.DatabaseModule; import net.sf.briar.db.DatabaseModule;
import net.sf.briar.lifecycle.LifecycleModule; import net.sf.briar.lifecycle.LifecycleModule;
@@ -83,7 +84,7 @@ public class ProtocolIntegrationTest extends BriarTestCase {
public ProtocolIntegrationTest() throws Exception { public ProtocolIntegrationTest() throws Exception {
super(); super();
Injector i = Guice.createInjector(new CryptoModule(), Injector i = Guice.createInjector(new ClockModule(), new CryptoModule(),
new DatabaseModule(), new LifecycleModule(), new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(), new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(), new SimplexProtocolModule(), new TestDatabaseModule(), new SimplexProtocolModule(),

View File

@@ -9,6 +9,7 @@ import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DbException; import net.sf.briar.api.db.DbException;
import net.sf.briar.api.lifecycle.ShutdownManager; import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.api.protocol.PacketFactory; import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.clock.SystemClock;
import net.sf.briar.db.DatabaseCleaner.Callback; import net.sf.briar.db.DatabaseCleaner.Callback;
import org.jmock.Expectations; import org.jmock.Expectations;
@@ -145,6 +146,6 @@ public class DatabaseComponentImplTest extends DatabaseComponentTest {
Database<T> database, DatabaseCleaner cleaner, Database<T> database, DatabaseCleaner cleaner,
ShutdownManager shutdown, PacketFactory packetFactory) { ShutdownManager shutdown, PacketFactory packetFactory) {
return new DatabaseComponentImpl<T>(database, cleaner, shutdown, return new DatabaseComponentImpl<T>(database, cleaner, shutdown,
packetFactory); packetFactory, new SystemClock());
} }
} }

View File

@@ -42,6 +42,7 @@ import net.sf.briar.api.transport.ConnectionContextFactory;
import net.sf.briar.api.transport.ConnectionWindow; import net.sf.briar.api.transport.ConnectionWindow;
import net.sf.briar.api.transport.ConnectionWindowFactory; import net.sf.briar.api.transport.ConnectionWindowFactory;
import net.sf.briar.api.transport.TransportConstants; import net.sf.briar.api.transport.TransportConstants;
import net.sf.briar.clock.SystemClock;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.lifecycle.LifecycleModule; import net.sf.briar.lifecycle.LifecycleModule;
import net.sf.briar.protocol.ProtocolModule; import net.sf.briar.protocol.ProtocolModule;
@@ -1847,7 +1848,7 @@ public class H2DatabaseTest extends BriarTestCase {
private Database<Connection> open(boolean resume) throws Exception { private Database<Connection> open(boolean resume) throws Exception {
Database<Connection> db = new H2Database(testDir, password, MAX_SIZE, Database<Connection> db = new H2Database(testDir, password, MAX_SIZE,
connectionContextFactory, connectionWindowFactory, connectionContextFactory, connectionWindowFactory,
groupFactory); groupFactory, new SystemClock());
db.open(resume); db.open(resume);
return db; return db;
} }

View File

@@ -13,6 +13,7 @@ import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex; import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.transport.ConnectionDispatcher; import net.sf.briar.api.transport.ConnectionDispatcher;
import net.sf.briar.api.ui.UiCallback; import net.sf.briar.api.ui.UiCallback;
import net.sf.briar.clock.SystemClock;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
@@ -27,7 +28,7 @@ public class PluginManagerImplTest extends BriarTestCase {
final DatabaseComponent db = context.mock(DatabaseComponent.class); final DatabaseComponent db = context.mock(DatabaseComponent.class);
final Poller poller = context.mock(Poller.class); final Poller poller = context.mock(Poller.class);
final ConnectionDispatcher dispatcher = final ConnectionDispatcher dispatcher =
context.mock(ConnectionDispatcher.class); context.mock(ConnectionDispatcher.class);
final UiCallback uiCallback = context.mock(UiCallback.class); final UiCallback uiCallback = context.mock(UiCallback.class);
final AtomicInteger index = new AtomicInteger(0); final AtomicInteger index = new AtomicInteger(0);
context.checking(new Expectations() {{ context.checking(new Expectations() {{
@@ -47,8 +48,8 @@ public class PluginManagerImplTest extends BriarTestCase {
oneOf(poller).stop(); oneOf(poller).stop();
}}); }});
ExecutorService executor = Executors.newCachedThreadPool(); ExecutorService executor = Executors.newCachedThreadPool();
PluginManagerImpl p = new PluginManagerImpl(executor, db, poller, PluginManagerImpl p = new PluginManagerImpl(executor, new SystemClock(),
dispatcher, uiCallback); db, poller, dispatcher, uiCallback);
// We expect either 3 or 4 plugins to be started, depending on whether // We expect either 3 or 4 plugins to be started, depending on whether
// the test machine has a Bluetooth device // the test machine has a Bluetooth device
int started = p.start(); int started = p.start();

View File

@@ -9,6 +9,7 @@ import java.util.concurrent.Executors;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
import net.sf.briar.clock.SystemClock;
import net.sf.briar.plugins.DuplexClientTest; import net.sf.briar.plugins.DuplexClientTest;
// This is not a JUnit test - it has to be run manually while the server test // This is not a JUnit test - it has to be run manually while the server test
@@ -25,7 +26,7 @@ public class BluetoothClientTest extends DuplexClientTest {
// Create the plugin // Create the plugin
callback = new ClientCallback(new TransportConfig(), callback = new ClientCallback(new TransportConfig(),
new TransportProperties(), remote); new TransportProperties(), remote);
plugin = new BluetoothPlugin(executor, callback, 0L); plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@@ -7,6 +7,7 @@ import java.util.concurrent.Executors;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
import net.sf.briar.clock.SystemClock;
import net.sf.briar.plugins.DuplexServerTest; import net.sf.briar.plugins.DuplexServerTest;
// This is not a JUnit test - it has to be run manually while the client test // This is not a JUnit test - it has to be run manually while the client test
@@ -20,7 +21,7 @@ public class BluetoothServerTest extends DuplexServerTest {
// Create the plugin // Create the plugin
callback = new ServerCallback(new TransportConfig(), local, callback = new ServerCallback(new TransportConfig(), local,
Collections.singletonMap(contactId, new TransportProperties())); Collections.singletonMap(contactId, new TransportProperties()));
plugin = new BluetoothPlugin(executor, callback, 0L); plugin = new BluetoothPlugin(executor, new SystemClock(), callback, 0L);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@@ -1,25 +1,21 @@
package net.sf.briar.plugins.email; package net.sf.briar.plugins.email;
import static org.junit.Assert.*; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexTransportReader; import net.sf.briar.api.plugins.simplex.SimplexTransportReader;
import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
import net.sf.briar.clock.SystemClock;
import org.jmock.Mockery;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -106,7 +102,8 @@ Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportPropert
public void testPluginFactoryCreation() public void testPluginFactoryCreation()
{ {
GmailPluginFactory plugin = new GmailPluginFactory(); GmailPluginFactory plugin = new GmailPluginFactory();
plugin.createPlugin(Executors.newSingleThreadExecutor(), callback); plugin.createPlugin(Executors.newSingleThreadExecutor(),
new SystemClock(), callback);
} }
@Test @Test

View File

@@ -30,6 +30,7 @@ import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionRecogniser; import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.ConnectionRegistry; import net.sf.briar.api.transport.ConnectionRegistry;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.clock.ClockModule;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.db.DatabaseModule; import net.sf.briar.db.DatabaseModule;
import net.sf.briar.lifecycle.LifecycleModule; import net.sf.briar.lifecycle.LifecycleModule;
@@ -77,8 +78,9 @@ public class SimplexConnectionReadWriteTest extends BriarTestCase {
} }
private Injector createInjector(File dir) { private Injector createInjector(File dir) {
return Guice.createInjector(new CryptoModule(), new DatabaseModule(), return Guice.createInjector(new ClockModule(), new CryptoModule(),
new LifecycleModule(), new ProtocolModule(), new SerialModule(), new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(dir), new SimplexProtocolModule(), new TestDatabaseModule(dir), new SimplexProtocolModule(),
new TransportModule(), new DuplexProtocolModule()); new TransportModule(), new DuplexProtocolModule());
} }

View File

@@ -10,6 +10,7 @@ import net.sf.briar.BriarTestCase;
import net.sf.briar.TestDatabaseModule; import net.sf.briar.TestDatabaseModule;
import net.sf.briar.api.transport.ConnectionWriter; import net.sf.briar.api.transport.ConnectionWriter;
import net.sf.briar.api.transport.ConnectionWriterFactory; import net.sf.briar.api.transport.ConnectionWriterFactory;
import net.sf.briar.clock.ClockModule;
import net.sf.briar.crypto.CryptoModule; import net.sf.briar.crypto.CryptoModule;
import net.sf.briar.db.DatabaseModule; import net.sf.briar.db.DatabaseModule;
import net.sf.briar.lifecycle.LifecycleModule; import net.sf.briar.lifecycle.LifecycleModule;
@@ -30,7 +31,7 @@ public class ConnectionWriterTest extends BriarTestCase {
public ConnectionWriterTest() throws Exception { public ConnectionWriterTest() throws Exception {
super(); super();
Injector i = Guice.createInjector(new CryptoModule(), Injector i = Guice.createInjector(new ClockModule(), new CryptoModule(),
new DatabaseModule(), new LifecycleModule(), new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(), new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(), new SimplexProtocolModule(), new TestDatabaseModule(), new SimplexProtocolModule(),