Skip to content

Commit f0f831b

Browse files
committed
fix: npe caused by incomplete initialization of dependencies
1 parent a7fd620 commit f0f831b

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/config/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.arex.agent.bootstrap.util.ConcurrentHashSet;
55
import io.arex.agent.bootstrap.util.StringUtil;
66
import io.arex.inst.runtime.context.RecordLimiter;
7+
import io.arex.inst.runtime.listener.EventProcessor;
78
import io.arex.inst.runtime.model.DynamicClassEntity;
89

910
import java.util.ArrayList;
@@ -197,6 +198,9 @@ public boolean isLocalStorage() {
197198
* @return true: invalid, false: valid
198199
*/
199200
public boolean invalidRecord(String path) {
201+
if (!EventProcessor.dependencyInitComplete()) {
202+
return true;
203+
}
200204
if (isLocalStorage()) {
201205
return false;
202206
}

arex-instrumentation-api/src/main/java/io/arex/inst/runtime/listener/EventProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class EventProcessor {
4141
* the onRequest method must be called before calling the onCreate method
4242
*/
4343
public static void onCreate(EventSource source){
44-
if (!InitializeEnum.COMPLETE.equals(INIT_DEPENDENCY.get())) {
44+
if (!dependencyInitComplete()) {
4545
return;
4646
}
4747
initContext(source);
@@ -131,4 +131,8 @@ private static void initLog(ClassLoader contextClassLoader) {
131131
List<Logger> extensionLoggerList = ServiceLoader.load(Logger.class, contextClassLoader);
132132
LogManager.build(extensionLoggerList);
133133
}
134+
135+
public static boolean dependencyInitComplete() {
136+
return InitializeEnum.COMPLETE.equals(INIT_DEPENDENCY.get());
137+
}
134138
}

arex-instrumentation-api/src/test/java/io/arex/inst/runtime/config/ConfigTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
import io.arex.agent.bootstrap.constants.ConfigConstants;
44
import io.arex.agent.bootstrap.util.ConcurrentHashSet;
55
import io.arex.inst.runtime.context.RecordLimiter;
6+
import io.arex.inst.runtime.listener.EventProcessor;
67
import io.arex.inst.runtime.model.ArexConstants;
78
import io.arex.inst.runtime.model.DynamicClassEntity;
89
import java.util.Arrays;
910
import java.util.HashSet;
1011
import java.util.List;
12+
13+
import org.junit.jupiter.api.AfterAll;
1114
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.BeforeAll;
1216
import org.junit.jupiter.api.Test;
1317
import org.junit.jupiter.params.ParameterizedTest;
1418
import org.junit.jupiter.params.provider.Arguments;
1519
import org.junit.jupiter.params.provider.MethodSource;
20+
import org.mockito.MockedStatic;
21+
import org.mockito.Mockito;
1622

1723
import java.util.function.Predicate;
1824
import java.util.stream.Stream;
@@ -21,6 +27,15 @@
2127
import static org.junit.jupiter.params.provider.Arguments.arguments;
2228

2329
class ConfigTest {
30+
@BeforeAll
31+
static void setUp() {
32+
Mockito.mockStatic(EventProcessor.class);
33+
}
34+
35+
@AfterAll
36+
static void tearDown() {
37+
Mockito.clearAllCaches();
38+
}
2439

2540
@ParameterizedTest
2641
@MethodSource("invalidCase")
@@ -32,27 +47,38 @@ void invalidRecord(Runnable mocker, Predicate<Boolean> predicate) {
3247
static Stream<Arguments> invalidCase() {
3348
ConfigBuilder config = ConfigBuilder.create("mock");
3449
Runnable mocker1 = () -> {
50+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
3551
config.addProperty(ConfigConstants.STORAGE_SERVICE_MODE, "local").build();;
3652
};
3753
Runnable mocker2 = () -> {
54+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
3855
config.addProperty(ConfigConstants.STORAGE_SERVICE_MODE, "").build();;
3956
config.recordRate(0).build();
4057
};
4158
Runnable mocker3 = () -> {
59+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
4260
config.recordRate(1).build();
4361
};
4462
Runnable mocker4 = () -> {
63+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
4564
config.addProperty(ConfigConstants.DURING_WORK, "true").build();;
4665
};
4766
Runnable mocker5 = () -> {
67+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
4868
config.addProperty(ConfigConstants.AGENT_ENABLED, "true").build();
4969
RecordLimiter.init(mock -> true);
5070
};
5171
Runnable disableRecord = () -> {
72+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(true);
5273
config.addProperty(ConfigConstants.DISABLE_RECORD, "true").build();;
5374
RecordLimiter.init(mock -> true);
5475
};
5576

77+
Runnable dependencyNotInitComplete = () -> {
78+
Mockito.when(EventProcessor.dependencyInitComplete()).thenReturn(false);
79+
config.build();;
80+
};
81+
5682
Predicate<Boolean> assertFalse = result -> !result;
5783
Predicate<Boolean> assertTrue = result -> result;
5884
return Stream.of(
@@ -61,7 +87,8 @@ static Stream<Arguments> invalidCase() {
6187
arguments(mocker3, assertTrue),
6288
arguments(mocker4, assertTrue),
6389
arguments(mocker5, assertFalse),
64-
arguments(disableRecord, assertTrue)
90+
arguments(disableRecord, assertTrue),
91+
arguments(dependencyNotInitComplete, assertTrue)
6592
);
6693
}
6794

0 commit comments

Comments
 (0)