X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterfaceTest.java;h=44820d4b59ba2e1b9f9901ab92086c3f622e088d;hb=fa06db9c6e864183b2582a3cc3ed93df74d8a8cb;hp=5abf2c177bac09373c5a645863a750d4c98b6d77;hpb=7d95ecaebffebf59a2cc1109e3f98b9a79b21de7;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index 5abf2c1..44820d4 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -1,8 +1,11 @@ package net.pterodactylus.sone.core; import static freenet.keys.InsertableClientSSK.createRandom; +import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; +import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; import static java.lang.System.currentTimeMillis; import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.concurrent.TimeUnit.SECONDS; import static net.pterodactylus.sone.Matchers.delivers; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -13,6 +16,8 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyShort; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -24,6 +29,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.MalformedURLException; import java.util.HashMap; +import java.util.concurrent.CountDownLatch; import net.pterodactylus.sone.core.FreenetInterface.Callback; import net.pterodactylus.sone.core.FreenetInterface.Fetched; @@ -60,6 +66,8 @@ import com.google.common.eventbus.EventBus; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; /** * Unit test for {@link FreenetInterface}. @@ -76,6 +84,8 @@ public class FreenetInterfaceTest { private final USKManager uskManager = mock(USKManager.class); private FreenetInterface freenetInterface; private final Sone sone = mock(Sone.class); + private final ArgumentCaptor callbackCaptor = forClass(USKCallback.class); + private final SoneDownloader soneDownloader = mock(SoneDownloader.class); @Before public void setupFreenetInterface() { @@ -93,6 +103,11 @@ public class FreenetInterfaceTest { when(sone.getRequestUri()).thenReturn(insertSsk.getURI().uskForSSK()); } + @Before + public void setupCallbackCaptorAndUskManager() { + doNothing().when(uskManager).subscribe(any(USK.class), callbackCaptor.capture(), anyBoolean(), any(RequestClient.class)); + } + @Test public void canFetchUri() throws MalformedURLException, FetchException { FreenetURI freenetUri = new FreenetURI("KSK@GPLv3.txt"); @@ -284,6 +299,63 @@ public class FreenetInterfaceTest { } @Test + public void callbackPrioritiesAreInteractive() { + freenetInterface.registerUsk(sone, null); + assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(INTERACTIVE_PRIORITY_CLASS)); + assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS)); + } + + @Test + public void callbackForRegisteredSoneWithHigherEditionTriggersDownload() throws InterruptedException { + freenetInterface.registerUsk(sone, soneDownloader); + final CountDownLatch downloadTriggered = new CountDownLatch(1); + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + downloadTriggered.countDown(); + return null; + } + }).when(soneDownloader).fetchSone(sone); + callbackCaptor.getValue().onFoundEdition(1, null, null, null, false, (short) 0, null, false, false); + assertThat(downloadTriggered.await(1, SECONDS), is(true)); + } + + @Test + public void callbackForRegisteredSoneWithTheSameEditionDoesNotTriggerDownload() throws InterruptedException { + freenetInterface.registerUsk(sone, soneDownloader); + final CountDownLatch downloadTriggered = new CountDownLatch(1); + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + downloadTriggered.countDown(); + return null; + } + }).when(soneDownloader).fetchSone(sone); + callbackCaptor.getValue().onFoundEdition(0, null, null, null, false, (short) 0, null, false, false); + assertThat(downloadTriggered.await(1, SECONDS), is(false)); + } + + @Test + public void callbackForNormalUskUsesDifferentPriorities() { + Callback callback = mock(Callback.class); + FreenetURI uri = createRandom(randomSource, "test-0").getURI().uskForSSK(); + freenetInterface.registerUsk(uri, callback); + assertThat(callbackCaptor.getValue().getPollingPriorityNormal(), is(PREFETCH_PRIORITY_CLASS)); + assertThat(callbackCaptor.getValue().getPollingPriorityProgress(), is(INTERACTIVE_PRIORITY_CLASS)); + } + + @Test + public void callbackForNormalUskForwardsImportantParameters() throws MalformedURLException { + Callback callback = mock(Callback.class); + FreenetURI uri = createRandom(randomSource, "test-0").getURI().uskForSSK(); + freenetInterface.registerUsk(uri, callback); + USK key = mock(USK.class); + when(key.getURI()).thenReturn(uri); + callbackCaptor.getValue().onFoundEdition(3, key, null, null, false, (short) 0, null, true, true); + verify(callback).editionFound(eq(uri), eq(3L), eq(true), eq(true)); + } + + @Test public void fetchedRetainsUriAndFetchResult() { FreenetURI freenetUri = mock(FreenetURI.class); FetchResult fetchResult = mock(FetchResult.class);