Set span kinds for internal requests

This commit is contained in:
Sebastian Sdorra
2020-11-04 09:37:24 +01:00
parent b91d9e6b60
commit 5694a89589
8 changed files with 77 additions and 14 deletions

View File

@@ -32,6 +32,7 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.net.ahc.AdvancedHttpClient;
import sonia.scm.net.ahc.AdvancedHttpResponse;
import java.io.IOException;
import java.util.Date;
@@ -44,6 +45,7 @@ import java.util.concurrent.Semaphore;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static sonia.scm.admin.ReleaseFeedParser.SPAN_KIND;
@ExtendWith(MockitoExtension.class)
class ReleaseFeedParserTest {
@@ -62,7 +64,7 @@ class ReleaseFeedParserTest {
void shouldFindLatestRelease() throws IOException {
String url = "https://www.scm-manager.org/download/rss.xml";
when(client.get(url).request().contentFromXml(ReleaseFeedDto.class)).thenReturn(createReleaseFeedDto());
when(request(url).contentFromXml(ReleaseFeedDto.class)).thenReturn(createReleaseFeedDto());
Optional<UpdateInfo> update = releaseFeedParser.findLatestRelease(url);
@@ -71,13 +73,17 @@ class ReleaseFeedParserTest {
assertThat(update.get().getLink()).isEqualTo("download-3");
}
private AdvancedHttpResponse request(String url) throws IOException {
return client.get(url).spanKind(SPAN_KIND).request();
}
@Test
void shouldHandleTimeout() throws IOException {
String url = "https://www.scm-manager.org/download/rss.xml";
Semaphore waitWithResultUntilTimeout = new Semaphore(0);
when(client.get(url).request().contentFromXml(ReleaseFeedDto.class)).thenAnswer(invocation -> {
when(request(url).contentFromXml(ReleaseFeedDto.class)).thenAnswer(invocation -> {
waitWithResultUntilTimeout.acquire();
return createReleaseFeedDto();
});
@@ -95,7 +101,7 @@ class ReleaseFeedParserTest {
Semaphore waitWithResultUntilBothTriggered = new Semaphore(0);
when(client.get(url).request().contentFromXml(ReleaseFeedDto.class)).thenAnswer(invocation -> {
when(request(url).contentFromXml(ReleaseFeedDto.class)).thenAnswer(invocation -> {
waitWithResultUntilBothTriggered.acquire();
return createReleaseFeedDto();
});

View File

@@ -32,6 +32,7 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.event.ScmEventBus;
import sonia.scm.net.ahc.AdvancedHttpClient;
import sonia.scm.net.ahc.AdvancedHttpResponse;
import java.io.IOException;
import java.util.Collections;
@@ -41,6 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static sonia.scm.plugin.Tracing.SPAN_KIND;
@ExtendWith(MockitoExtension.class)
class PluginCenterLoaderTest {
@@ -63,16 +65,20 @@ class PluginCenterLoaderTest {
void shouldFetch() throws IOException {
Set<AvailablePlugin> plugins = Collections.emptySet();
PluginCenterDto dto = new PluginCenterDto();
when(client.get(PLUGIN_URL).request().contentFromJson(PluginCenterDto.class)).thenReturn(dto);
when(request().contentFromJson(PluginCenterDto.class)).thenReturn(dto);
when(mapper.map(dto)).thenReturn(plugins);
Set<AvailablePlugin> fetched = loader.load(PLUGIN_URL);
assertThat(fetched).isSameAs(plugins);
}
private AdvancedHttpResponse request() throws IOException {
return client.get(PLUGIN_URL).spanKind(SPAN_KIND).request();
}
@Test
void shouldReturnEmptySetIfPluginCenterNotBeReached() throws IOException {
when(client.get(PLUGIN_URL).request()).thenThrow(new IOException("failed to fetch"));
when(request()).thenThrow(new IOException("failed to fetch"));
Set<AvailablePlugin> fetch = loader.load(PLUGIN_URL);
assertThat(fetch).isEmpty();
@@ -80,7 +86,7 @@ class PluginCenterLoaderTest {
@Test
void shouldFirePluginCenterErrorEvent() throws IOException {
when(client.get(PLUGIN_URL).request()).thenThrow(new IOException("failed to fetch"));
when(request()).thenThrow(new IOException("failed to fetch"));
loader.load(PLUGIN_URL);

View File

@@ -34,6 +34,7 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContextProvider;
import sonia.scm.net.ahc.AdvancedHttpClient;
import sonia.scm.net.ahc.AdvancedHttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -50,6 +51,7 @@ import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static sonia.scm.plugin.Tracing.SPAN_KIND;
@ExtendWith({MockitoExtension.class})
class PluginInstallerTest {
@@ -101,10 +103,14 @@ class PluginInstallerTest {
}
private void mockContent(String content) throws IOException {
when(client.get("https://download.hitchhiker.com").request().contentAsStream())
when(request("https://download.hitchhiker.com").contentAsStream())
.thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
}
private AdvancedHttpResponse request(String url) throws IOException {
return client.get(url).spanKind(SPAN_KIND).request();
}
private AvailablePlugin createGitPlugin() {
return createPlugin(
"scm-git-plugin",
@@ -115,7 +121,7 @@ class PluginInstallerTest {
@Test
void shouldThrowPluginDownloadException() throws IOException {
when(client.get("https://download.hitchhiker.com").request()).thenThrow(new IOException("failed to download"));
when(request("https://download.hitchhiker.com")).thenThrow(new IOException("failed to download"));
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
@@ -136,7 +142,7 @@ class PluginInstallerTest {
void shouldThrowPluginDownloadExceptionAndCleanup() throws IOException {
InputStream stream = mock(InputStream.class);
when(stream.read(any(), anyInt(), anyInt())).thenThrow(new IOException("failed to read"));
when(client.get("https://download.hitchhiker.com").request().contentAsStream()).thenReturn(stream);
when(request("https://download.hitchhiker.com").contentAsStream()).thenReturn(stream);
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();