Dedicated executors for plugins and the connection recogniser.

This commit is contained in:
akwizgran
2011-12-08 16:11:24 +00:00
parent caf5f34828
commit 6e080bb35d
26 changed files with 223 additions and 176 deletions

View File

@@ -16,8 +16,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import junit.framework.TestCase;
import net.sf.briar.api.crypto.CryptoComponent;
@@ -59,10 +57,8 @@ import net.sf.briar.transport.TransportModule;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class ProtocolIntegrationTest extends TestCase {
@@ -87,14 +83,7 @@ public class ProtocolIntegrationTest extends TestCase {
public ProtocolIntegrationTest() throws Exception {
super();
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(Executor.class).toInstance(
Executors.newCachedThreadPool());
}
};
Injector i = Guice.createInjector(testModule, new CryptoModule(),
Injector i = Guice.createInjector(new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(), new ProtocolBatchModule(),

View File

@@ -13,8 +13,6 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -56,10 +54,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class H2DatabaseTest extends TestCase {
@@ -97,14 +93,7 @@ public class H2DatabaseTest extends TestCase {
public H2DatabaseTest() throws Exception {
super();
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(Executor.class).toInstance(
new ScheduledThreadPoolExecutor(5));
}
};
Injector i = Guice.createInjector(testModule, new CryptoModule(),
Injector i = Guice.createInjector(new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(),
new ProtocolBatchModule(), new TransportModule(),

View File

@@ -5,14 +5,10 @@ import static net.sf.briar.api.transport.TransportConstants.TAG_LENGTH;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import junit.framework.TestCase;
import net.sf.briar.TestDatabaseModule;
@@ -31,8 +27,6 @@ import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
import net.sf.briar.api.protocol.TransportUpdate;
import net.sf.briar.api.transport.BatchTransportReader;
import net.sf.briar.api.transport.BatchTransportWriter;
import net.sf.briar.api.transport.ConnectionContext;
import net.sf.briar.api.transport.ConnectionReaderFactory;
import net.sf.briar.api.transport.ConnectionRecogniser;
@@ -43,9 +37,6 @@ import net.sf.briar.db.DatabaseModule;
import net.sf.briar.lifecycle.LifecycleModule;
import net.sf.briar.plugins.ImmediateExecutor;
import net.sf.briar.protocol.ProtocolModule;
import net.sf.briar.protocol.batch.IncomingBatchConnection;
import net.sf.briar.protocol.batch.OutgoingBatchConnection;
import net.sf.briar.protocol.batch.ProtocolBatchModule;
import net.sf.briar.protocol.stream.ProtocolStreamModule;
import net.sf.briar.serial.SerialModule;
import net.sf.briar.transport.TransportModule;
@@ -54,10 +45,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class BatchConnectionReadWriteTest extends TestCase {
@@ -90,16 +79,8 @@ public class BatchConnectionReadWriteTest extends TestCase {
}
private Injector createInjector(File dir) {
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(Executor.class).toInstance(
new ScheduledThreadPoolExecutor(5));
}
};
return Guice.createInjector(testModule, new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(),
return Guice.createInjector(new CryptoModule(), new DatabaseModule(),
new LifecycleModule(), new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(dir), new ProtocolBatchModule(),
new TransportModule(), new ProtocolStreamModule());
}
@@ -133,12 +114,14 @@ public class BatchConnectionReadWriteTest extends TestCase {
alice.getInstance(ConnectionWriterFactory.class);
ProtocolWriterFactory protoFactory =
alice.getInstance(ProtocolWriterFactory.class);
BatchTransportWriter transport = new TestBatchTransportWriter(out);
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
Long.MAX_VALUE);
OutgoingBatchConnection batchOut = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
// Write whatever needs to be written
batchOut.write();
assertTrue(transport.getSuccess());
// Close Alice's database
db.close();
// Return the contents of the batch connection
@@ -187,14 +170,15 @@ public class BatchConnectionReadWriteTest extends TestCase {
bob.getInstance(ConnectionReaderFactory.class);
ProtocolReaderFactory protoFactory =
bob.getInstance(ProtocolReaderFactory.class);
BatchTransportReader reader = new TestBatchTransportReader(in);
TestBatchTransportReader transport = new TestBatchTransportReader(in);
IncomingBatchConnection batchIn = new IncomingBatchConnection(
new ImmediateExecutor(), new ImmediateExecutor(), db,
connFactory, protoFactory, ctx, reader, tag);
connFactory, protoFactory, ctx, transport, tag);
// No messages should have been added yet
assertFalse(listener.messagesAdded);
// Read whatever needs to be read
batchIn.read();
assertTrue(transport.getSuccess());
// The private message from Alice should have been added
assertTrue(listener.messagesAdded);
// Close Bob's database
@@ -215,46 +199,6 @@ public class BatchConnectionReadWriteTest extends TestCase {
}
}
private static class TestBatchTransportWriter
implements BatchTransportWriter {
private final OutputStream out;
private TestBatchTransportWriter(OutputStream out) {
this.out = out;
}
public long getCapacity() {
return Long.MAX_VALUE;
}
public OutputStream getOutputStream() {
return out;
}
public void dispose(boolean success) {
assertTrue(success);
}
}
private static class TestBatchTransportReader
implements BatchTransportReader {
private final InputStream in;
private TestBatchTransportReader(InputStream in) {
this.in = in;
}
public InputStream getInputStream() {
return in;
}
public void dispose(boolean success) {
assertTrue(success);
}
}
private static class TestCallback implements Callback {
private final CountDownLatch latch = new CountDownLatch(1);

View File

@@ -0,0 +1,29 @@
package net.sf.briar.protocol.batch;
import java.io.InputStream;
import net.sf.briar.api.transport.BatchTransportReader;
class TestBatchTransportReader
implements BatchTransportReader {
private final InputStream in;
private boolean success = false;
TestBatchTransportReader(InputStream in) {
this.in = in;
}
public InputStream getInputStream() {
return in;
}
public void dispose(boolean success) {
this.success = success;
}
boolean getSuccess() {
return success;
}
}

View File

@@ -0,0 +1,37 @@
package net.sf.briar.protocol.batch;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import net.sf.briar.api.transport.BatchTransportWriter;
class TestBatchTransportWriter
implements BatchTransportWriter {
private final ByteArrayOutputStream out;
private final long capacity;
private boolean success = false;
TestBatchTransportWriter(ByteArrayOutputStream out,
long capacity) {
this.out = out;
this.capacity = capacity;
}
public long getCapacity() {
return capacity - out.size();
}
public OutputStream getOutputStream() {
return out;
}
public void dispose(boolean success) {
this.success = success;
}
boolean getSuccess() {
return success;
}
}

View File

@@ -81,8 +81,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(window));
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
assertNull(c.acceptConnection(transportId, new byte[TAG_LENGTH]));
context.assertIsSatisfied();
}
@@ -109,8 +109,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// The tag should not be expected by the wrong transport
TransportId wrong = new TransportId(TestUtils.getRandomId());
@@ -150,8 +150,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(window));
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Ensure the recogniser is initialised
assertFalse(c.isInitialised());
@@ -177,8 +177,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(Collections.emptyList()));
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Remove the contact
c.eventOccurred(new ContactRemovedEvent(contactId));
@@ -214,8 +214,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// The tag should not be expected
assertFalse(c.isInitialised());
@@ -262,8 +262,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Add the transport
c.eventOccurred(new TransportAddedEvent(transportId));
@@ -309,8 +309,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// The tag should not be expected
assertFalse(c.isInitialised());
@@ -358,8 +358,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Update the contact
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
@@ -401,8 +401,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(window));
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Ensure the recogniser is initialised
assertFalse(c.isInitialised());
@@ -431,8 +431,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
will(returnValue(null));
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Update the contact
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,
@@ -497,8 +497,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Ensure the recogniser is initialised
assertFalse(c.isInitialised());
@@ -574,8 +574,8 @@ public class ConnectionRecogniserImplTest extends TestCase {
oneOf(db).setConnectionWindow(contactId, remoteIndex, window);
}});
Executor executor = new ImmediateExecutor();
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(crypto, db,
executor);
ConnectionRecogniserImpl c = new ConnectionRecogniserImpl(executor, db,
crypto);
byte[] tag = calculateTag();
// Update the contact
c.eventOccurred(new RemoteTransportsUpdatedEvent(contactId,

View File

@@ -5,8 +5,6 @@ import static net.sf.briar.api.transport.TransportConstants.MIN_CONNECTION_LENGT
import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import junit.framework.TestCase;
import net.sf.briar.TestDatabaseModule;
@@ -22,10 +20,8 @@ import net.sf.briar.serial.SerialModule;
import org.junit.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
public class ConnectionWriterTest extends TestCase {
@@ -34,14 +30,7 @@ public class ConnectionWriterTest extends TestCase {
public ConnectionWriterTest() throws Exception {
super();
Module testModule = new AbstractModule() {
@Override
public void configure() {
bind(Executor.class).toInstance(
new ScheduledThreadPoolExecutor(5));
}
};
Injector i = Guice.createInjector(testModule, new CryptoModule(),
Injector i = Guice.createInjector(new CryptoModule(),
new DatabaseModule(), new LifecycleModule(),
new ProtocolModule(), new SerialModule(),
new TestDatabaseModule(), new ProtocolBatchModule(),