Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 12, 2024
1 parent 04d0088 commit a1ecc75
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void loadConfiguration() {
* configuration server url was set up.
*/
private void startHttpPolling(String serverUrl, ConfigurationPersistence persistence) {
log.info("Starting configuration polling from configuration server with url: {}", serverUrl);
InspectitScheduler scheduler = InspectitScheduler.getInstance();
HttpConfigurationPoller poller = new HttpConfigurationPoller(serverUrl, persistence);
Duration pollingInterval = PropertiesResolver.getPollingInterval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void classIsRemovedFromCacheAndNotAddedToBatch() {
Set<Class<?>> classes = new HashSet<>();
classes.add(TEST_CLASS);
when(cache.getKeyIterator()).thenReturn(classes.iterator());
when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)).thenReturn(configuration);
when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS))
.thenReturn(configuration);
when(instrumentationState.shouldRetransform(TEST_CLASS, configuration)).thenReturn(false);

BatchInstrumenter instrumenter =
Expand All @@ -55,7 +56,8 @@ void classIsRemovedFromCacheAndAddedToBatch() {
Set<Class<?>> classes = new HashSet<>();
classes.add(TEST_CLASS);
when(cache.getKeyIterator()).thenReturn(classes.iterator());
when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS)).thenReturn(configuration);
when(configurationResolver.getClassInstrumentationConfiguration(TEST_CLASS))
.thenReturn(configuration);
when(instrumentationState.shouldRetransform(TEST_CLASS, configuration)).thenReturn(true);

BatchInstrumenter instrumenter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void stop() {
}

public void configServerSetup(String config_path) throws IOException {

ClassLoader loader = getClass().getClassLoader();
File file = new File(loader.getResource(config_path).getFile());
String body = FileUtils.readFileToString(file, "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ void reset() throws IOException {
}

protected void stopTarget() {

target.stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Request;
import org.junit.jupiter.api.Test;
import org.awaitility.Awaitility;

public class ScopeTest extends SpringTestBase {

Expand Down Expand Up @@ -76,6 +78,34 @@ void multipleScopesInstrumentAllSelectedMethods() throws Exception {
assertLogs(logs, 4);
}

@Test
void configurationUpdatesAreApplied() throws Exception {
// Set up config server to instrument multiple methods
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-multiple-methods.json");

startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget("/greeting");

String logs = target.getLogs();

assertLogs(logs, 2);

// Update configuration to only instrument one method
configurationServerMock.reset();
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-method.json");

awaitConfigurationUpdate();
sendRequestToTarget("/greeting");

logs = target.getLogs();
stopTarget();

// 2 logs before update + 1 log after update
assertLogs(logs, 3);
}

private void sendRequestToTarget(String path) throws Exception {
String url = String.format("http://localhost:%d%s", target.getMappedPort(8080), path);
Call call = client.newCall(new Request.Builder().url(url).get().build());
Expand All @@ -84,7 +114,38 @@ private void sendRequestToTarget(String path) throws Exception {
call.execute();
}

/**
* Checks, if the logs contain "HELLO GEPARD" and "BYE GEPARD" for a specific number of times
*
* @param logs the logs
* @param times the amount of times "HELLO GEPARD" and "BYE GEPARD" should be present in the logs
*/
private void assertLogs(String logs, int times) {
boolean loggedHelloGepardTwice = containsTimes(logs, "HELLO GEPARD", times);
boolean loggedByeGepardTwice = containsTimes(logs, "BYE GEPARD", times);

assertTrue(loggedHelloGepardTwice);
assertTrue(loggedByeGepardTwice);
}

/**
* Checks, if a specific message can be found for a specific amount of times inside the provided logs.
*
* @return true, if the message appears the expected amount of times in the logs
*/
private boolean containsTimes(String logs, String message, int times) {
int count = countTimes(logs, message);
return count == times;
}

/**
* Counts how many times a specific message can be found inside the provided logs
*
* @param logs the logs
* @param message the message to look for
* @return the amount of times the message appears in the logs
*/
private int countTimes(String logs, String message) {
int count = 0;
int index = 0;
while (index != -1) {
Expand All @@ -94,14 +155,30 @@ private boolean containsTimes(String logs, String message, int times) {
index += message.length();
}
}
return count == times;
return count;
}

private void assertLogs(String logs, int i) {
boolean loggedHelloGepardTwice = containsTimes(logs, "HELLO GEPARD", i);
boolean loggedByeGepardTwice = containsTimes(logs, "BYE GEPARD", i);

assertTrue(loggedHelloGepardTwice);
assertTrue(loggedByeGepardTwice);
/**
* Waits until the configuration was polled one more time. This happens via checking the container
* logs. First the method counts the current amount of update messages. If the amount of update
* messages has increased, it is assumed that a new configuration has been pooled.
*/
private void awaitConfigurationUpdate() throws InterruptedException {
String updateMessage =
"Fetched configuration from configuration server and received status code 200";
String logs = target.getLogs();
int configUpdateCount = countTimes(logs, updateMessage);

Awaitility.await()
.atMost(15, TimeUnit.SECONDS)
.until(
() -> {
String newLogs = target.getLogs();
int currentConfigUpdateCount = countTimes(newLogs, updateMessage);
return currentConfigUpdateCount > configUpdateCount;
});

// Wait an additional second as puffer
Thread.sleep(1000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static String expectedString() {
}

private static InspectitConfiguration expectedConfig() {
Scope scope = new Scope(true,"com.example.Application", Collections.emptyList());
Scope scope = new Scope(true, "com.example.Application", Collections.emptyList());
InstrumentationConfiguration instrumentationConfiguration =
new InstrumentationConfiguration(List.of(scope));
return new InspectitConfiguration(instrumentationConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void typeShouldNotBeInstrumented() {
InspectitConfiguration configuration = new InspectitConfiguration();
when(holder.getConfiguration()).thenReturn(configuration);

ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE);
ClassInstrumentationConfiguration config =
resolver.getClassInstrumentationConfiguration(TEST_TYPE);
boolean isActive = config.isActive();

assertFalse(isActive);
Expand All @@ -52,7 +53,8 @@ void typeShouldBeInstrumented() {
InspectitConfiguration configuration = createConfiguration(List.of(scope));
when(holder.getConfiguration()).thenReturn(configuration);

ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE);
ClassInstrumentationConfiguration config =
resolver.getClassInstrumentationConfiguration(TEST_TYPE);
boolean isActive = config.isActive();

assertTrue(isActive);
Expand All @@ -64,7 +66,8 @@ void typeShouldBeDeinstrumented() {
InspectitConfiguration configuration = createConfiguration(List.of(scope));
when(holder.getConfiguration()).thenReturn(configuration);

ClassInstrumentationConfiguration config = resolver.getClassInstrumentationConfiguration(TEST_TYPE);
ClassInstrumentationConfiguration config =
resolver.getClassInstrumentationConfiguration(TEST_TYPE);
boolean isActive = config.isActive();

assertFalse(isActive);
Expand Down

0 comments on commit a1ecc75

Please sign in to comment.