Skip to content

Commit 0b9492c

Browse files
Reach 100% patch coverage: CapturedContext & LogProbe
Co-authored-by: albabn <[email protected]>
1 parent 844be15 commit 0b9492c

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.datadog.debugger.probe;
2+
3+
import static java.util.Collections.emptyList;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.datadog.debugger.el.DSL;
8+
import com.datadog.debugger.el.ValueScript;
9+
import datadog.trace.bootstrap.debugger.CapturedContext;
10+
import datadog.trace.bootstrap.debugger.EvaluationError;
11+
import datadog.trace.bootstrap.debugger.MethodLocation;
12+
import datadog.trace.bootstrap.debugger.ProbeId;
13+
import datadog.trace.bootstrap.debugger.el.Values;
14+
import java.util.Arrays;
15+
import org.junit.jupiter.api.Test;
16+
17+
class LogProbeCaptureExpressionsUndefinedTest {
18+
@Test
19+
void captureExpressionUndefinedAddsError() {
20+
LogProbe.CaptureExpression ce =
21+
new LogProbe.CaptureExpression(
22+
"ce", new ValueScript(DSL.value(Values.UNDEFINED_OBJECT), "exprUndefined"), null);
23+
24+
LogProbe probe =
25+
LogProbe.builder()
26+
.language("java")
27+
.probeId(ProbeId.newId())
28+
.template(null, emptyList())
29+
.captureExpressions(Arrays.asList(ce))
30+
.build();
31+
32+
CapturedContext context = new CapturedContext();
33+
LogProbe.LogStatus status = new LogProbe.LogStatus(probe);
34+
35+
probe.evaluate(context, status, MethodLocation.DEFAULT);
36+
37+
assertTrue(status.hasLogTemplateErrors());
38+
assertEquals(1, status.getErrors().size());
39+
EvaluationError err = status.getErrors().get(0);
40+
assertEquals("exprUndefined", err.getExpr());
41+
assertEquals("UNDEFINED", err.getMessage());
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package datadog.trace.bootstrap.debugger;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertSame;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import com.datadog.debugger.agent.JsonSnapshotSerializer;
9+
import datadog.trace.bootstrap.debugger.util.TimeoutChecker;
10+
import java.time.Duration;
11+
import java.time.temporal.ChronoUnit;
12+
import java.util.Map;
13+
import org.junit.jupiter.api.BeforeAll;
14+
import org.junit.jupiter.api.Test;
15+
16+
class CapturedContextCaptureExpressionsTest {
17+
@BeforeAll
18+
static void initSerializer() {
19+
// Ensure freeze() has a serializer to produce strValue
20+
DebuggerContext.initValueSerializer(new JsonSnapshotSerializer());
21+
}
22+
23+
@Test
24+
void addGetAndFreezeCaptureExpressions() {
25+
CapturedContext ctx = new CapturedContext();
26+
27+
CapturedContext.CapturedValue exprVal =
28+
CapturedContext.CapturedValue.of("expr1", Object.class.getTypeName(), 42);
29+
30+
// addCaptureExpression should lazily init the map and put the value
31+
ctx.addCaptureExpression(exprVal);
32+
33+
// getCaptureExpressions should return the map (covering getter line)
34+
Map<String, CapturedContext.CapturedValue> exprs = ctx.getCaptureExpressions();
35+
assertNotNull(exprs);
36+
assertTrue(exprs.containsKey("expr1"));
37+
assertSame(exprVal, exprs.get("expr1"));
38+
39+
// freeze should only freeze captureExpressions branch when present
40+
ctx.freeze(new TimeoutChecker(Duration.of(50, ChronoUnit.MILLIS)));
41+
42+
CapturedContext.CapturedValue frozen = exprs.get("expr1");
43+
assertNotNull(frozen.getStrValue());
44+
assertNull(frozen.getValue()); // value released after serialization
45+
}
46+
}

0 commit comments

Comments
 (0)