package net.pterodactylus.sone.core;
+import static java.lang.Thread.sleep;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.eq;
import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob;
import net.pterodactylus.sone.freenet.plugin.PluginException;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector;
private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
private final OwnIdentity ownIdentity = mock(OwnIdentity.class);
+ private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true);
+ private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false);
private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT);
private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT);
+ private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) {
+ return webOfTrustUpdater.new WebOfTrustUpdateJob() {
+ @Override
+ public void run() {
+ super.run();
+ try {
+ sleep(100);
+ } catch (InterruptedException ie1) {
+ throw new RuntimeException(ie1);
+ }
+ finish(success);
+ }
+ };
+ }
+
+ @Test
+ public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException {
+ new Thread(successfulWebOfTrustUpdateJob).start();
+ assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true));
+ }
+
+ @Test
+ public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException {
+ new Thread(failingWebOfTrustUpdateJob).start();
+ assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false));
+ }
+
@Test
public void addContextJobAddsTheContext() throws PluginException {
addContextJob.run();