Skip to content

Commit

Permalink
Refactor/remove method hooks (#21)
Browse files Browse the repository at this point in the history
* improve code

* solve sonarcloud issue
  • Loading branch information
EddeCCC authored Oct 10, 2024
1 parent fe4461a commit 23d3f39
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-feature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- "feature/**"
pull_request:

jobs:
build-and-test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public void updateHooksFor(Class<?> clazz, ClassInstrumentationConfiguration con
*/
private Set<MethodDescription.InDefinedShape> getInstrumentedMethods(
Class<?> clazz, ClassInstrumentationConfiguration configuration) {
if (configuration.equals(ClassInstrumentationConfiguration.NO_INSTRUMENTATION))
return Collections.emptySet();

ElementMatcher.Junction<MethodDescription> methodMatcher = configuration.methodMatcher();
TypeDescription type = TypeDescription.ForLoadedType.of(clazz);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Set;
import java.util.Collections;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import rocks.inspectit.gepard.agent.instrumentation.hook.configuration.ClassHookConfiguration;
import rocks.inspectit.gepard.agent.instrumentation.hook.configuration.HookedMethods;
import rocks.inspectit.gepard.agent.internal.instrumentation.model.ClassInstrumentationConfiguration;
import rocks.inspectit.gepard.bootstrap.Instances;
Expand Down Expand Up @@ -77,7 +76,17 @@ void shouldUpdateHooksForClassWhenMethodsAreInstrumented() {

methodHookManager.updateHooksFor(TEST_CLASS, classConfiguration);

verify(hookState).removeObsoleteHooks(eq(TEST_CLASS), any(Set.class));
verify(hookState).updateHooks(eq(TEST_CLASS), any(ClassHookConfiguration.class));
verify(hookState).removeObsoleteHooks(eq(TEST_CLASS), argThat(set -> !set.isEmpty()));
verify(hookState)
.updateHooks(eq(TEST_CLASS), argThat(config -> !config.getMethods().isEmpty()));
}

@Test
void shouldRemoveHooksForClassWhenScopeIsInactive() {
methodHookManager.updateHooksFor(
TEST_CLASS, ClassInstrumentationConfiguration.NO_INSTRUMENTATION);

verify(hookState).removeObsoleteHooks(TEST_CLASS, Collections.emptySet());
verify(hookState).updateHooks(eq(TEST_CLASS), argThat(config -> config.getMethods().isEmpty()));
}
}

0 comments on commit 23d3f39

Please sign in to comment.