Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Dec 17, 2024
1 parent c9be94c commit 6a71412
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import rocks.inspectit.ocelot.commons.models.command.impl.PingCommand;
import rocks.inspectit.ocelot.commons.models.command.CommandResponse;
import rocks.inspectit.ocelot.config.model.InspectitConfig;
Expand All @@ -24,6 +25,7 @@
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeoutException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -113,6 +115,7 @@ void setup() {
retrySettings.setInitialInterval(Duration.ofMillis(5));
retrySettings.setMultiplier(BigDecimal.ONE);
retrySettings.setRandomizationFactor(BigDecimal.valueOf(0.1));
retrySettings.setTimeLimit(Duration.ofSeconds(1));
environment.getCurrentConfig().getAgentCommands().setRetry(retrySettings);
}

Expand Down Expand Up @@ -151,6 +154,18 @@ void failsWithExceptionIfReachesMaxAttempts() throws IOException {

assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> handler.nextCommand());
}

@Test
void failsIfTimeLimitIsExceeded() throws Exception {
Answer<HttpResponse> delayedAnswer = invocation -> {
Thread.sleep(5000); // 5s delay
return unsuccessfulResponse;
};
when(commandFetcher.fetchCommand(any(), anyBoolean())).thenAnswer(delayedAnswer);

assertThatExceptionOfType(TimeoutException.class).isThrownBy(() -> handler.nextCommand());
verify(commandFetcher, times(2)).fetchCommand(any(), anyBoolean());
}
}

private HttpResponse httpResponseFor(int statusCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ public class Retries {

private final ResponseDefinitionBuilder unsuccessfulResponse = aResponse().withStatus(500);

private final ResponseDefinitionBuilder slowResponse = aResponse().withStatus(500)
.withFixedDelay(5000);

@BeforeEach
public void setup() throws MalformedURLException {
mockServer.start();
Expand All @@ -286,6 +289,7 @@ public void setup() throws MalformedURLException {
retrySettings.setInitialInterval(Duration.ofMillis(5));
retrySettings.setMultiplier(BigDecimal.ONE);
retrySettings.setRandomizationFactor(BigDecimal.valueOf(0.1));
retrySettings.setTimeLimit(Duration.ofSeconds(1));
httpSettings.setRetry(retrySettings);

state = new HttpPropertySourceState("retry-test-state", httpSettings);
Expand Down Expand Up @@ -350,6 +354,18 @@ void noRetriesIfFallingBackToFile() {

mockServer.verify(1, anyRequestedFor(anyUrl()));
}

@Test
void failsIfTimeLimitIsExceeded() {
mockServer.stubFor(getHttpConfigurationMapping.willReturn(slowResponse));

boolean unsuccessfulUpdate = state.update(false);
int requests = mockServer.findRequestsMatching(anyRequestedFor(anyUrl()).build())
.getRequests().size();

assertThat(unsuccessfulUpdate).isFalse();
assertThat(requests).isEqualTo(2);
}
}

@Nested
Expand Down

0 comments on commit 6a71412

Please sign in to comment.