Skip to content

Commit 88a7978

Browse files
Fix coverage
1 parent d2163c8 commit 88a7978

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

telemetry/src/main/java/datadog/telemetry/endpoint/EndpointPeriodicAction.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,37 @@ public EndpointPeriodicAction(EndpointCollector collector) {
2121
@Override
2222
public void doIteration(final TelemetryService service) {
2323
for (final Iterator<Endpoint> it = collector.drain(); it.hasNext(); ) {
24-
if (!service.addEndpoint(it.next())) {
24+
final Endpoint endpoint = it.next();
25+
if (!service.addEndpoint(endpoint)) {
26+
collector.supplier(new HeadAndTailIterator(endpoint, it)); // try again latter
2527
break;
2628
}
2729
}
2830
}
31+
32+
private static final class HeadAndTailIterator implements Iterator<Endpoint> {
33+
private final Endpoint head;
34+
private final Iterator<Endpoint> tail;
35+
private boolean first;
36+
37+
private HeadAndTailIterator(final Endpoint head, final Iterator<Endpoint> tail) {
38+
this.head = head;
39+
this.tail = tail;
40+
first = true;
41+
}
42+
43+
@Override
44+
public boolean hasNext() {
45+
return first || tail.hasNext();
46+
}
47+
48+
@Override
49+
public Endpoint next() {
50+
if (first) {
51+
first = false;
52+
return head;
53+
}
54+
return tail.next();
55+
}
56+
}
2957
}

telemetry/src/test/groovy/datadog/telemetry/endpoint/EndpointPeriodicActionTest.groovy

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,48 @@ import spock.lang.Specification
77

88
class EndpointPeriodicActionTest extends Specification {
99

10-
void 'test that common metrics are joined before being sent to telemetry #iterationIndex'() {
10+
void 'test that endpoints are captured by the periodic action'() {
1111
given:
1212
final endpoint = new Endpoint().first(true).type('REST').method("GET").operation('http.request').path("/test")
1313
final service = Mock(TelemetryService)
14-
final endpointCollector = Mock(EndpointCollector)
14+
final endpointCollector = new EndpointCollector()
15+
endpointCollector.supplier([endpoint].iterator())
1516
final action = new EndpointPeriodicAction(endpointCollector)
1617

1718
when:
1819
action.doIteration(service)
1920

2021
then:
21-
1 * endpointCollector.drain() >> [endpoint].iterator()
2222
1 * service.addEndpoint(endpoint)
2323
0 * _
2424
}
25+
26+
void 'test that endpoints are not lost if the service is at capacity'() {
27+
given:
28+
final endpoints = [
29+
new Endpoint().first(true).type('REST').method("GET").operation('http.request').path("/test1"),
30+
new Endpoint().type('REST').method("GET").operation('http.request').path("/test2"),
31+
new Endpoint().type('REST').method("GET").operation('http.request').path("/test3"),
32+
]
33+
final service = Mock(TelemetryService)
34+
final endpointCollector = new EndpointCollector()
35+
endpointCollector.supplier(endpoints.iterator())
36+
final action = new EndpointPeriodicAction(endpointCollector)
37+
38+
when:
39+
action.doIteration(service)
40+
41+
then:
42+
1 * service.addEndpoint(endpoints[0]) >> true
43+
1 * service.addEndpoint(endpoints[1]) >> false
44+
0 * _
45+
46+
when:
47+
action.doIteration(service)
48+
49+
then:
50+
1 * service.addEndpoint(endpoints[1]) >> true
51+
1 * service.addEndpoint(endpoints[2]) >> true
52+
0 * _
53+
}
2554
}

0 commit comments

Comments
 (0)