|
73 | 73 | import java.util.List; |
74 | 74 | import java.util.Map; |
75 | 75 | import java.util.Optional; |
| 76 | +import java.util.function.DoubleConsumer; |
76 | 77 | import java.util.stream.Collectors; |
77 | 78 | import org.jetbrains.kotlin.com.intellij.util.lang.JavaVersion; |
78 | 79 | import org.joor.Reflect; |
@@ -1296,6 +1297,35 @@ public void nullCondition() throws IOException, URISyntaxException { |
1296 | 1297 | assertEquals("Cannot dereference field: fld", snapshot.getMessage()); |
1297 | 1298 | } |
1298 | 1299 |
|
| 1300 | + @Test |
| 1301 | + public void nullConditionTemplateOnly() throws IOException, URISyntaxException { |
| 1302 | + final String CLASS_NAME = "CapturedSnapshot08"; |
| 1303 | + LogProbe logProbes = |
| 1304 | + createProbeBuilder(PROBE_ID, CLASS_NAME, "doit", "int (java.lang.String)") |
| 1305 | + .when( |
| 1306 | + new ProbeCondition( |
| 1307 | + DSL.when( |
| 1308 | + DSL.eq( |
| 1309 | + DSL.getMember( |
| 1310 | + DSL.getMember(DSL.getMember(DSL.ref("nullTyped"), "fld"), "fld"), |
| 1311 | + "msg"), |
| 1312 | + DSL.value("hello"))), |
| 1313 | + "nullTyped.fld.fld.msg == 'hello'")) |
| 1314 | + .captureSnapshot(false) |
| 1315 | + .template("plain log", Collections.emptyList()) |
| 1316 | + .build(); |
| 1317 | + TestSnapshotListener listener = installProbes(logProbes); |
| 1318 | + Class<?> testClass = compileAndLoadClass(CLASS_NAME); |
| 1319 | + int result = Reflect.onClass(testClass).call("main", "1").get(); |
| 1320 | + assertEquals(3, result); |
| 1321 | + Snapshot snapshot = assertOneSnapshot(listener); |
| 1322 | + assertEquals("Cannot dereference field: fld", snapshot.getMessage()); |
| 1323 | + List<EvaluationError> evaluationErrors = snapshot.getEvaluationErrors(); |
| 1324 | + assertEquals(1, evaluationErrors.size()); |
| 1325 | + assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr()); |
| 1326 | + assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage()); |
| 1327 | + } |
| 1328 | + |
1299 | 1329 | @Test |
1300 | 1330 | public void shortCircuitingCondition() throws IOException, URISyntaxException { |
1301 | 1331 | final String CLASS_NAME = "CapturedSnapshot08"; |
@@ -2676,19 +2706,47 @@ public void ensureCallingSamplingLineProbeCondition() throws IOException, URISyn |
2676 | 2706 | doSamplingTest(this::lineProbeCondition, 1, 1); |
2677 | 2707 | } |
2678 | 2708 |
|
| 2709 | + @Test |
| 2710 | + public void ensureCallingSamplingLogTemplateOnlyConditionError() |
| 2711 | + throws IOException, URISyntaxException { |
| 2712 | + doSamplingTest(this::nullConditionTemplateOnly, ProbeRateLimiter::setGlobalLogRate, 1, 0, 1); |
| 2713 | + } |
| 2714 | + |
2679 | 2715 | private void doSamplingTest(TestMethod testRun, int expectedGlobalCount, int expectedProbeCount) |
2680 | 2716 | throws IOException, URISyntaxException { |
| 2717 | + doSamplingTest( |
| 2718 | + testRun, |
| 2719 | + ProbeRateLimiter::setGlobalSnapshotRate, |
| 2720 | + expectedGlobalCount, |
| 2721 | + expectedProbeCount, |
| 2722 | + 0); |
| 2723 | + } |
| 2724 | + |
| 2725 | + private void doSamplingTest( |
| 2726 | + TestMethod testRun, |
| 2727 | + DoubleConsumer globalRateSetter, |
| 2728 | + int expectedGlobalCount, |
| 2729 | + int expectedProbeCount, |
| 2730 | + int expectedErrorCount) |
| 2731 | + throws IOException, URISyntaxException { |
2681 | 2732 | MockSampler probeSampler = new MockSampler(); |
| 2733 | + MockSampler errorSampler = new MockSampler(); |
2682 | 2734 | MockSampler globalSampler = new MockSampler(); |
2683 | | - ProbeRateLimiter.setSamplerSupplier(rate -> rate < 101 ? probeSampler : globalSampler); |
2684 | | - ProbeRateLimiter.setGlobalSnapshotRate(1000); |
| 2735 | + ProbeRateLimiter.setSamplerSupplier( |
| 2736 | + rate -> { |
| 2737 | + if (rate < 2) return errorSampler; |
| 2738 | + if (rate < 101) return probeSampler; |
| 2739 | + return globalSampler; |
| 2740 | + }); |
| 2741 | + globalRateSetter.accept(1000); |
2685 | 2742 | try { |
2686 | 2743 | testRun.run(); |
2687 | 2744 | } finally { |
2688 | 2745 | ProbeRateLimiter.setSamplerSupplier(null); |
2689 | 2746 | } |
2690 | 2747 | assertEquals(expectedGlobalCount, globalSampler.getCallCount()); |
2691 | 2748 | assertEquals(expectedProbeCount, probeSampler.getCallCount()); |
| 2749 | + assertEquals(expectedErrorCount, errorSampler.getCallCount()); |
2692 | 2750 | } |
2693 | 2751 |
|
2694 | 2752 | @Test |
|
0 commit comments