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