Skip to content

Commit

Permalink
client: fix concurrency issue in test
Browse files Browse the repository at this point in the history
Stop the spider only after the access to the seed URL occurred and
verify that the new URL was not accessed, otherwise the test could fail
because the seed URL could be accessed between the calls to start and
stop.

Signed-off-by: thc202 <thc202@gmail.com>
  • Loading branch information
thc202 committed Dec 18, 2024
1 parent e1d096f commit 5090756
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -119,27 +122,34 @@ void shouldRequestInScopeUrls() {
}

@Test
void shouldIgnoreRequestAfterStopped() {
void shouldIgnoreRequestAfterStopped() throws Exception {
// Given
ClientSpider spider =
new ClientSpider(extClient, "https://www.example.com/", clientOptions, 1);
CountDownLatch cdl = new CountDownLatch(1);
String seedUrl = "https://www.example.com/";
ClientSpider spider = new ClientSpider(extClient, seedUrl, clientOptions, 1);
Options options = mock(Options.class);
Timeouts timeouts = mock(Timeouts.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
when(wd.manage()).thenReturn(options);
when(options.timeouts()).thenReturn(timeouts);
String urlAfterStop = "https://www.example.com/test#1";
doAnswer(
invocation -> {
spider.stop();
map.getOrAddNode(urlAfterStop, false, false);
cdl.countDown();
return null;
})
.when(wd)
.get(seedUrl);

// When
spider.start();
spider.stop();
map.getOrAddNode("https://www.example.com/test#1", false, false);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// Ignore
}
// and stopped on URL access

// Then
verify(wd, never()).get(any());
cdl.await(2, TimeUnit.SECONDS);
assertThat(spider.isStopped(), is(true));
verify(wd, never()).get(urlAfterStop);
}

@Test
Expand Down

0 comments on commit 5090756

Please sign in to comment.