mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Add transports to DB during startup. #269
This commit is contained in:
55
briar-tests/src/org/briarproject/TestPluginsModule.java
Normal file
55
briar-tests/src/org/briarproject/TestPluginsModule.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package org.briarproject;
|
||||
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.plugins.PluginConfig;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPlugin;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class TestPluginsModule {
|
||||
|
||||
public static final TransportId TRANSPORT_ID = new TransportId("id");
|
||||
public static final int MAX_LATENCY = 2 * 60 * 1000; // 2 minutes
|
||||
|
||||
private final SimplexPluginFactory simplex = new SimplexPluginFactory() {
|
||||
|
||||
@Override
|
||||
public TransportId getId() {
|
||||
return TRANSPORT_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLatency() {
|
||||
return MAX_LATENCY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SimplexPlugin createPlugin(SimplexPluginCallback callback) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@Provides
|
||||
PluginConfig providePluginConfig() {
|
||||
return new PluginConfig() {
|
||||
|
||||
@Override
|
||||
public Collection<DuplexPluginFactory> getDuplexFactories() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SimplexPluginFactory> getSimplexFactories() {
|
||||
return Collections.singletonList(simplex);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,6 @@ package org.briarproject.plugins;
|
||||
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.plugins.ConnectionManager;
|
||||
import org.briarproject.api.plugins.PluginConfig;
|
||||
@@ -15,9 +13,7 @@ import org.briarproject.api.plugins.simplex.SimplexPluginCallback;
|
||||
import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.api.settings.SettingsManager;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.briarproject.api.ui.UiCallback;
|
||||
import org.briarproject.system.SystemClock;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.jmock.lib.concurrent.Synchroniser;
|
||||
@@ -33,14 +29,12 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
|
||||
@Test
|
||||
public void testStartAndStop() throws Exception {
|
||||
Clock clock = new SystemClock();
|
||||
Mockery context = new Mockery() {{
|
||||
setThreadingPolicy(new Synchroniser());
|
||||
}};
|
||||
final Executor ioExecutor = Executors.newSingleThreadExecutor();
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
final PluginConfig pluginConfig = context.mock(PluginConfig.class);
|
||||
final DatabaseComponent db = context.mock(DatabaseComponent.class);
|
||||
final Poller poller = context.mock(Poller.class);
|
||||
final ConnectionManager connectionManager =
|
||||
context.mock(ConnectionManager.class);
|
||||
@@ -49,30 +43,27 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
final TransportPropertyManager transportPropertyManager =
|
||||
context.mock(TransportPropertyManager.class);
|
||||
final UiCallback uiCallback = context.mock(UiCallback.class);
|
||||
|
||||
// Two simplex plugin factories: both create plugins, one fails to start
|
||||
final SimplexPluginFactory simplexFactory =
|
||||
context.mock(SimplexPluginFactory.class);
|
||||
final SimplexPlugin simplexPlugin = context.mock(SimplexPlugin.class);
|
||||
final TransportId simplexId = new TransportId("simplex");
|
||||
final int simplexLatency = 12345;
|
||||
final Transaction simplexTxn = new Transaction(null);
|
||||
final SimplexPluginFactory simplexFailFactory =
|
||||
context.mock(SimplexPluginFactory.class, "simplexFailFactory");
|
||||
final SimplexPlugin simplexFailPlugin =
|
||||
context.mock(SimplexPlugin.class, "simplexFailPlugin");
|
||||
final TransportId simplexFailId = new TransportId("simplex1");
|
||||
final int simplexFailLatency = 23456;
|
||||
final Transaction simplexFailTxn = new Transaction(null);
|
||||
|
||||
// Two duplex plugin factories: one creates a plugin, the other fails
|
||||
final DuplexPluginFactory duplexFactory =
|
||||
context.mock(DuplexPluginFactory.class);
|
||||
final DuplexPlugin duplexPlugin = context.mock(DuplexPlugin.class);
|
||||
final TransportId duplexId = new TransportId("duplex");
|
||||
final int duplexLatency = 34567;
|
||||
final Transaction duplexTxn = new Transaction(null);
|
||||
final DuplexPluginFactory duplexFailFactory =
|
||||
context.mock(DuplexPluginFactory.class, "duplexFailFactory");
|
||||
final TransportId duplexFailId = new TransportId("duplex1");
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// First simplex plugin
|
||||
oneOf(pluginConfig).getSimplexFactories();
|
||||
@@ -83,12 +74,6 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
oneOf(simplexFactory).createPlugin(with(any(
|
||||
SimplexPluginCallback.class)));
|
||||
will(returnValue(simplexPlugin)); // Created
|
||||
oneOf(simplexPlugin).getMaxLatency();
|
||||
will(returnValue(simplexLatency));
|
||||
oneOf(db).startTransaction();
|
||||
will(returnValue(simplexTxn));
|
||||
oneOf(db).addTransport(simplexTxn, simplexId, simplexLatency);
|
||||
oneOf(db).endTransaction(simplexTxn);
|
||||
oneOf(simplexPlugin).start();
|
||||
will(returnValue(true)); // Started
|
||||
oneOf(simplexPlugin).shouldPoll();
|
||||
@@ -100,13 +85,6 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
oneOf(simplexFailFactory).createPlugin(with(any(
|
||||
SimplexPluginCallback.class)));
|
||||
will(returnValue(simplexFailPlugin)); // Created
|
||||
oneOf(simplexFailPlugin).getMaxLatency();
|
||||
will(returnValue(simplexFailLatency));
|
||||
oneOf(db).startTransaction();
|
||||
will(returnValue(simplexFailTxn));
|
||||
oneOf(db).addTransport(simplexFailTxn, simplexFailId,
|
||||
simplexFailLatency);
|
||||
oneOf(db).endTransaction(simplexFailTxn);
|
||||
oneOf(simplexFailPlugin).start();
|
||||
will(returnValue(false)); // Failed to start
|
||||
// First duplex plugin
|
||||
@@ -117,12 +95,6 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
oneOf(duplexFactory).createPlugin(with(any(
|
||||
DuplexPluginCallback.class)));
|
||||
will(returnValue(duplexPlugin)); // Created
|
||||
oneOf(duplexPlugin).getMaxLatency();
|
||||
will(returnValue(duplexLatency));
|
||||
oneOf(db).startTransaction();
|
||||
will(returnValue(duplexTxn));
|
||||
oneOf(db).addTransport(duplexTxn, duplexId, duplexLatency);
|
||||
oneOf(db).endTransaction(duplexTxn);
|
||||
oneOf(duplexPlugin).start();
|
||||
will(returnValue(true)); // Started
|
||||
oneOf(duplexPlugin).shouldPoll();
|
||||
@@ -139,13 +111,15 @@ public class PluginManagerImplTest extends BriarTestCase {
|
||||
oneOf(simplexPlugin).stop();
|
||||
oneOf(duplexPlugin).stop();
|
||||
}});
|
||||
|
||||
PluginManagerImpl p = new PluginManagerImpl(ioExecutor, eventBus,
|
||||
pluginConfig, clock, db, poller, connectionManager,
|
||||
settingsManager, transportPropertyManager, uiCallback);
|
||||
pluginConfig, poller, connectionManager, settingsManager,
|
||||
transportPropertyManager, uiCallback);
|
||||
|
||||
// Two plugins should be started and stopped
|
||||
assertTrue(p.start());
|
||||
assertTrue(p.stop());
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.sync;
|
||||
import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.ImmediateExecutor;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
@@ -28,7 +27,6 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
private final Executor dbExecutor;
|
||||
private final EventBus eventBus;
|
||||
private final ContactId contactId;
|
||||
private final TransportId transportId;
|
||||
private final MessageId messageId;
|
||||
private final int maxLatency;
|
||||
private final PacketWriter packetWriter;
|
||||
@@ -40,7 +38,6 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
eventBus = context.mock(EventBus.class);
|
||||
packetWriter = context.mock(PacketWriter.class);
|
||||
contactId = new ContactId(234);
|
||||
transportId = new TransportId("id");
|
||||
messageId = new MessageId(TestUtils.getRandomId());
|
||||
maxLatency = Integer.MAX_VALUE;
|
||||
}
|
||||
@@ -48,10 +45,10 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
@Test
|
||||
public void testNothingToSend() throws Exception {
|
||||
final SimplexOutgoingSession session = new SimplexOutgoingSession(db,
|
||||
dbExecutor, eventBus, contactId, transportId, maxLatency,
|
||||
packetWriter);
|
||||
dbExecutor, eventBus, contactId, maxLatency, packetWriter);
|
||||
final Transaction noAckTxn = new Transaction(null);
|
||||
final Transaction noMsgTxn = new Transaction(null);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// Add listener
|
||||
oneOf(eventBus).addListener(session);
|
||||
@@ -73,7 +70,9 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
// Remove listener
|
||||
oneOf(eventBus).removeListener(session);
|
||||
}});
|
||||
|
||||
session.run();
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -82,12 +81,12 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
final Ack ack = new Ack(Collections.singletonList(messageId));
|
||||
final byte[] raw = new byte[1234];
|
||||
final SimplexOutgoingSession session = new SimplexOutgoingSession(db,
|
||||
dbExecutor, eventBus, contactId, transportId, maxLatency,
|
||||
packetWriter);
|
||||
dbExecutor, eventBus, contactId, maxLatency, packetWriter);
|
||||
final Transaction ackTxn = new Transaction(null);
|
||||
final Transaction noAckTxn = new Transaction(null);
|
||||
final Transaction msgTxn = new Transaction(null);
|
||||
final Transaction noMsgTxn = new Transaction(null);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// Add listener
|
||||
oneOf(eventBus).addListener(session);
|
||||
@@ -124,7 +123,9 @@ public class SimplexOutgoingSessionTest extends BriarTestCase {
|
||||
// Remove listener
|
||||
oneOf(eventBus).removeListener(session);
|
||||
}});
|
||||
|
||||
session.run();
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user