Changed drive monitors to use callbacks rather than blocking.

This commit is contained in:
akwizgran
2011-10-05 16:11:18 +01:00
parent 3a321b0f0e
commit f6df333796
12 changed files with 253 additions and 148 deletions

View File

@@ -10,6 +10,8 @@ import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;
@@ -18,15 +20,13 @@ public class PollingRemovableDriveMonitorTest extends TestCase {
@Test
public void testOneCallbackPerFile() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final List<File> detected = new ArrayList<File>();
final File file1 = new File("foo");
final File file2 = new File("bar");
// Create a finder that returns no files the first time, then two files
final List<File> noDrives = Collections.emptyList();
final List<File> twoDrives = new ArrayList<File>();
twoDrives.add(file1);
twoDrives.add(file2);
// Create a finder that returns no files the first time, then two files
Mockery context = new Mockery();
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
@@ -36,24 +36,21 @@ public class PollingRemovableDriveMonitorTest extends TestCase {
oneOf(finder).findRemovableDrives();
will(returnValue(twoDrives));
}});
// Create a monitor that will wait for two files before stopping
// Create a callback that will wait for two files before stopping
final List<File> detected = new ArrayList<File>();
final CountDownLatch latch = new CountDownLatch(2);
final Callback callback = new Callback() {
public void driveInserted(File f) {
detected.add(f);
latch.countDown();
}
};
// Create the monitor and start it
final RemovableDriveMonitor monitor =
new PollingRemovableDriveMonitor(finder, 10);
monitor.start();
new Thread() {
@Override
public void run() {
try {
detected.add(monitor.waitForInsertion());
detected.add(monitor.waitForInsertion());
latch.countDown();
} catch(IOException e) {
fail();
}
}
}.start();
monitor.start(callback);
// Wait for the monitor to detect the files
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(1, TimeUnit.SECONDS));
monitor.stop();
// Check that both files were detected
assertEquals(2, detected.size());
@@ -63,32 +60,6 @@ public class PollingRemovableDriveMonitorTest extends TestCase {
context.assertIsSatisfied();
}
@Test
public void testExceptionRethrownWhenWaiting() throws Exception {
final List<File> noDrives = Collections.emptyList();
// Create a finder that throws an exception the second time it's polled
Mockery context = new Mockery();
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
context.checking(new Expectations() {{
oneOf(finder).findRemovableDrives();
will(returnValue(noDrives));
oneOf(finder).findRemovableDrives();
will(throwException(new IOException()));
}});
// The monitor should rethrow the exception when it waits
final RemovableDriveMonitor monitor =
new PollingRemovableDriveMonitor(finder, 10);
monitor.start();
try {
monitor.waitForInsertion();
fail();
} catch(IOException expected) {}
// The exception shouldn't be thrown again
monitor.stop();
context.assertIsSatisfied();
}
@Test
public void testExceptionRethrownWhenStopping() throws Exception {
final List<File> noDrives = Collections.emptyList();
@@ -102,11 +73,12 @@ public class PollingRemovableDriveMonitorTest extends TestCase {
oneOf(finder).findRemovableDrives();
will(throwException(new IOException()));
}});
// The monitor should rethrow the exception when it stops
// Create the monitor, start it, and give it some time to run
final RemovableDriveMonitor monitor =
new PollingRemovableDriveMonitor(finder, 10);
monitor.start();
monitor.start(null);
Thread.sleep(50);
// The monitor should rethrow the exception when it stops
try {
monitor.stop();
fail();

View File

@@ -9,8 +9,10 @@ import java.util.List;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.api.ContactId;
import net.sf.briar.api.transport.ConnectionRecogniser;
import net.sf.briar.api.transport.batch.BatchTransportCallback;
import net.sf.briar.api.transport.batch.BatchTransportWriter;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
import org.jmock.Expectations;
import org.jmock.Mockery;
@@ -31,9 +33,14 @@ public class RemovableDrivePluginTest extends TestCase {
@Test
public void testGetId() {
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
assertEquals(RemovableDrivePlugin.TRANSPORT_ID,
plugin.getId().getInt());
@@ -46,15 +53,23 @@ public class RemovableDrivePluginTest extends TestCase {
final List<File> drives = Collections.emptyList();
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId));
@@ -71,18 +86,26 @@ public class RemovableDrivePluginTest extends TestCase {
drives.add(drive2);
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
oneOf(callback).showChoice(with(any(String.class)),
with(any(String[].class)));
will(returnValue(-1)); // The user cancelled the choice
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId));
@@ -101,18 +124,26 @@ public class RemovableDrivePluginTest extends TestCase {
drives.add(drive2);
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
oneOf(callback).showChoice(with(any(String.class)),
with(any(String[].class)));
will(returnValue(0)); // The user chose drive1 but it doesn't exist
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId));
@@ -133,18 +164,26 @@ public class RemovableDrivePluginTest extends TestCase {
assertTrue(drive1.createNewFile());
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
oneOf(callback).showChoice(with(any(String.class)),
with(any(String[].class)));
will(returnValue(0)); // The user chose drive1 but it's not a dir
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
assertNull(plugin.createWriter(contactId));
@@ -165,18 +204,26 @@ public class RemovableDrivePluginTest extends TestCase {
assertTrue(drive1.mkdir());
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
oneOf(callback).showChoice(with(any(String.class)),
with(any(String[].class)));
will(returnValue(0)); // The user chose drive1
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
assertNotNull(plugin.createWriter(contactId));
@@ -200,11 +247,17 @@ public class RemovableDrivePluginTest extends TestCase {
assertTrue(drive1.mkdir());
Mockery context = new Mockery();
final ConnectionRecogniser recogniser =
context.mock(ConnectionRecogniser.class);
final RemovableDriveFinder finder =
context.mock(RemovableDriveFinder.class);
final RemovableDriveMonitor monitor =
context.mock(RemovableDriveMonitor.class);
final BatchTransportCallback callback =
context.mock(BatchTransportCallback.class);
context.checking(new Expectations() {{
oneOf(monitor).start(with(any(Callback.class)));
oneOf(finder).findRemovableDrives();
will(returnValue(drives));
oneOf(callback).showChoice(with(any(String.class)),
@@ -212,7 +265,9 @@ public class RemovableDrivePluginTest extends TestCase {
will(returnValue(0)); // The user chose drive1
oneOf(callback).showMessage(with(any(String.class)));
}});
RemovableDrivePlugin plugin = new RemovableDrivePlugin(finder);
RemovableDrivePlugin plugin = new RemovableDrivePlugin(recogniser,
finder, monitor);
plugin.start(null, null, null, callback);
BatchTransportWriter writer = plugin.createWriter(contactId);

View File

@@ -1,7 +1,6 @@
package net.sf.briar.plugins.file;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -9,6 +8,7 @@ import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
import net.sf.briar.plugins.file.RemovableDriveMonitor.Callback;
import org.junit.After;
import org.junit.Before;
@@ -27,36 +27,31 @@ public class UnixRemovableDriveMonitorTest extends TestCase {
public void testNonexistentDir() throws Exception {
File doesNotExist = new File(testDir, "doesNotExist");
RemovableDriveMonitor monitor = createMonitor(doesNotExist);
monitor.start();
monitor.start(null);
monitor.stop();
}
@Test
public void testOneCallbackPerFile() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
// Create a callback that will wait for two files before stopping
final List<File> detected = new ArrayList<File>();
// Create a monitor that will wait for two files before stopping
final RemovableDriveMonitor monitor = createMonitor(testDir);
monitor.start();
new Thread() {
@Override
public void run() {
try {
detected.add(monitor.waitForInsertion());
detected.add(monitor.waitForInsertion());
latch.countDown();
} catch(IOException e) {
fail();
}
final CountDownLatch latch = new CountDownLatch(2);
final Callback callback = new Callback() {
public void driveInserted(File f) {
detected.add(f);
latch.countDown();
}
}.start();
};
// Create the monitor and start it
RemovableDriveMonitor monitor = createMonitor(testDir);
monitor.start(callback);
// Create two files in the test directory
File file1 = new File(testDir, "1");
File file2 = new File(testDir, "2");
assertTrue(file1.createNewFile());
assertTrue(file2.createNewFile());
// Wait for the monitor to detect the files
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertTrue(latch.await(1, TimeUnit.SECONDS));
monitor.stop();
// Check that both files were detected
assertEquals(2, detected.size());