import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
}
- public class PluginCommands {
+ private List<String> lines;
+ private String identifier;
- private static final String CLASS_NAME = "foo.plugin.Plugin";
+ private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
+ throws InterruptedException, ExecutionException, IOException {
+ connectNode();
+ lines = fcpServer.collectUntil(is("EndMessage"));
+ identifier = extractIdentifier(lines);
+ assertThat(lines, requestMatcher.get());
+ }
- private List<String> lines;
- private String identifier;
+ public class PluginCommands {
- private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
- throws InterruptedException, ExecutionException, IOException {
- connectNode();
- lines = fcpServer.collectUntil(is("EndMessage"));
- identifier = extractIdentifier(lines);
- assertThat(lines, requestMatcher.get());
- }
+ private static final String CLASS_NAME = "foo.plugin.Plugin";
private void replyWithPluginInfo() throws IOException {
fcpServer.writeLine(
public void failedLoad() throws ExecutionException, InterruptedException, IOException {
Future<Optional<PluginInfo>> pluginInfo =
fcpClient.loadPlugin().officialFromFreenet("superPlugin").execute();
- connectNode();
- List<String> lines = fcpServer.collectUntil(is("EndMessage"));
- String identifier = extractIdentifier(lines);
- fcpServer.writeLine(
- "ProtocolError",
- "Identifier=" + identifier,
- "EndMessage"
- );
+ connectAndAssert(() -> matchesFcpMessage("LoadPlugin", "EndMessage"));
+ replyWithProtocolError();
assertThat(pluginInfo.get().isPresent(), is(false));
}
}
+ private void replyWithProtocolError() throws IOException {
+ fcpServer.writeLine(
+ "ProtocolError",
+ "Identifier=" + identifier,
+ "EndMessage"
+ );
+ }
+
public class ReloadPlugin {
@Test
assertThat(pluginRemoved.get(), is(true));
}
+ @Test
+ public void removingPluginWithMaxWaitTimeWorks()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Boolean> pluginRemoved = fcpClient.removePlugin().waitFor(1234).plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> allOf(matchPluginRemovedMessage(), hasItem("MaxWaitTime=1234")));
+ replyWithPluginRemoved();
+ assertThat(pluginRemoved.get(), is(true));
+ }
+
+ @Test
+ public void removingPluginWithPurgeWorks()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Boolean> pluginRemoved = fcpClient.removePlugin().purge().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> allOf(matchPluginRemovedMessage(), hasItem("Purge=true")));
+ replyWithPluginRemoved();
+ assertThat(pluginRemoved.get(), is(true));
+ }
+
private void replyWithPluginRemoved() throws IOException {
fcpServer.writeLine(
"PluginRemoved",
}
+ public class GetPluginInfo {
+
+ @Test
+ public void gettingPluginInfoWorks() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<PluginInfo>> pluginInfo = fcpClient.getPluginInfo().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> matchGetPluginInfoMessage());
+ replyWithPluginInfo();
+ verifyPluginInfo(pluginInfo);
+ }
+
+ @Test
+ public void gettingPluginInfoWithDetailsWorks()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<PluginInfo>> pluginInfo =
+ fcpClient.getPluginInfo().detailed().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> allOf(matchGetPluginInfoMessage(), hasItem("Detailed=true")));
+ replyWithPluginInfo();
+ verifyPluginInfo(pluginInfo);
+ }
+
+ @Test
+ public void protocolErrorIsRecognizedAsFailure()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<PluginInfo>> pluginInfo =
+ fcpClient.getPluginInfo().detailed().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> allOf(matchGetPluginInfoMessage(), hasItem("Detailed=true")));
+ replyWithProtocolError();
+ assertThat(pluginInfo.get(), is(Optional.empty()));
+ }
+
+ private Matcher<List<String>> matchGetPluginInfoMessage() {
+ return matchesFcpMessage(
+ "GetPluginInfo",
+ "Identifier=" + identifier,
+ "PluginName=" + CLASS_NAME,
+ "EndMessage"
+ );
+ }
+
+ }
+
+ }
+
+ public class UskSubscriptionCommands {
+
+ private static final String URI = "USK@some,uri/file.txt";
+
+ @Test
+ public void subscriptionWorks() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<UskSubscription>> uskSubscription = fcpClient.subscribeUsk().uri(URI).execute();
+ connectAndAssert(() -> matchesFcpMessage("SubscribeUSK", "URI=" + URI, "EndMessage"));
+ replyWithSubscribed();
+ assertThat(uskSubscription.get().get().getUri(), is(URI));
+ AtomicInteger edition = new AtomicInteger();
+ CountDownLatch updated = new CountDownLatch(2);
+ uskSubscription.get().get().onUpdate(e -> {
+ edition.set(e);
+ updated.countDown();
+ });
+ sendUpdateNotification(23);
+ sendUpdateNotification(24);
+ assertThat("updated in time", updated.await(5, TimeUnit.SECONDS), is(true));
+ assertThat(edition.get(), is(24));
+ }
+
+ private void replyWithSubscribed() throws IOException {
+ fcpServer.writeLine(
+ "SubscribedUSK",
+ "Identifier=" + identifier,
+ "URI=" + URI,
+ "DontPoll=false",
+ "EndMessage"
+ );
+ }
+
+ private void sendUpdateNotification(int edition, String... additionalLines) throws IOException {
+ fcpServer.writeLine(
+ "SubscribedUSKUpdate",
+ "Identifier=" + identifier,
+ "URI=" + URI,
+ "Edition=" + edition
+ );
+ fcpServer.writeLine(additionalLines);
+ fcpServer.writeLine("EndMessage");
+ }
+
}
}