forked from stripe/stripe-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTelemetryTest.java
More file actions
220 lines (179 loc) · 7.42 KB
/
RequestTelemetryTest.java
File metadata and controls
220 lines (179 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.stripe.net;
import static org.junit.jupiter.api.Assertions.*;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.stripe.Stripe;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link RequestTelemetry}.
*
* <p>These test the class directly, verifying enqueue/poll semantics, telemetry toggle behavior,
* queue bounds, and JSON payload structure — without going through the HTTP layer.
*/
public class RequestTelemetryTest {
private boolean originalTelemetry;
private RequestTelemetry telemetry;
@BeforeEach
public void setUp() {
originalTelemetry = Stripe.enableTelemetry;
Stripe.enableTelemetry = true;
telemetry = new RequestTelemetry();
// Drain any leftover metrics from prior tests (shared static queue)
while (telemetry.pollPayload().isPresent()) {
// discard
}
}
@AfterEach
public void tearDown() {
Stripe.enableTelemetry = originalTelemetry;
}
// ---- basic enqueue / poll ----
@Test
public void testPollReturnsEmptyWhenNothingEnqueued() {
Optional<String> payload = telemetry.pollPayload();
assertFalse(payload.isPresent(), "Expected empty payload when no metrics enqueued");
}
@Test
public void testEnqueueAndPollReturnsPayload() {
StripeResponse response = buildResponse("req_test_1");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(42), null);
Optional<String> payload = telemetry.pollPayload();
assertTrue(payload.isPresent(), "Expected a telemetry payload after enqueue");
JsonObject root = JsonParser.parseString(payload.get()).getAsJsonObject();
JsonObject metrics = root.getAsJsonObject("last_request_metrics");
assertEquals("req_test_1", metrics.get("request_id").getAsString());
assertEquals(42L, metrics.get("request_duration_ms").getAsLong());
assertFalse(metrics.has("usage"), "usage should be absent when null");
}
@Test
public void testPollDrainsQueue() {
StripeResponse response = buildResponse("req_drain");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(10), null);
assertTrue(telemetry.pollPayload().isPresent());
assertFalse(telemetry.pollPayload().isPresent(), "Second poll should return empty");
}
// ---- usage field ----
@Test
public void testUsageIncludedInPayload() {
StripeResponse response = buildResponse("req_usage");
List<String> usage = Arrays.asList("llm", "streaming");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(100), usage);
Optional<String> payload = telemetry.pollPayload();
assertTrue(payload.isPresent());
JsonObject metrics =
JsonParser.parseString(payload.get())
.getAsJsonObject()
.getAsJsonObject("last_request_metrics");
assertEquals("req_usage", metrics.get("request_id").getAsString());
assertTrue(metrics.has("usage"), "usage field should be present");
assertEquals(2, metrics.getAsJsonArray("usage").size());
}
@Test
public void testEmptyUsageListTreatedAsNull() {
StripeResponse response = buildResponse("req_empty_usage");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(50), Collections.emptyList());
Optional<String> payload = telemetry.pollPayload();
assertTrue(payload.isPresent());
JsonObject metrics =
JsonParser.parseString(payload.get())
.getAsJsonObject()
.getAsJsonObject("last_request_metrics");
// The class normalizes empty list to null
assertFalse(metrics.has("usage"), "Empty usage list should be normalized to absent");
}
// ---- telemetry toggle ----
@Test
public void testEnqueueIgnoredWhenTelemetryDisabled() {
Stripe.enableTelemetry = false;
StripeResponse response = buildResponse("req_disabled");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(10), null);
// Re-enable so pollPayload doesn't short-circuit
Stripe.enableTelemetry = true;
assertFalse(
telemetry.pollPayload().isPresent(),
"Nothing should be enqueued when telemetry is disabled");
}
@Test
public void testPollReturnsEmptyWhenTelemetryDisabledAtPollTime() {
// Enqueue while enabled
StripeResponse response = buildResponse("req_toggle");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(10), null);
// Disable before polling
Stripe.enableTelemetry = false;
assertFalse(
telemetry.pollPayload().isPresent(),
"pollPayload should return empty when telemetry is disabled at poll time");
// Re-enable — the metric was consumed (polled off queue) even though it wasn't returned
Stripe.enableTelemetry = true;
assertFalse(
telemetry.pollPayload().isPresent(),
"Metric should have been consumed even when telemetry was disabled");
}
// ---- null request ID ----
@Test
public void testEnqueueIgnoredWhenRequestIdIsNull() {
StripeResponse response = buildResponse(null);
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(10), null);
assertFalse(
telemetry.pollPayload().isPresent(),
"Nothing should be enqueued when requestId is null");
}
// ---- queue capacity ----
@Test
public void testQueueBoundedAtMaxSize() {
// MAX_REQUEST_METRICS_QUEUE_SIZE is 100
for (int i = 0; i < 110; i++) {
StripeResponse response = buildResponse("req_" + i);
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(1), null);
}
int count = 0;
while (telemetry.pollPayload().isPresent()) {
count++;
}
assertEquals(100, count, "Queue should be bounded at 100 entries");
}
// ---- deprecated getHeaderValue ----
@Test
public void testGetHeaderValueReturnsEmptyWhenHeaderAlreadyPresent() {
StripeResponse response = buildResponse("req_dup_header");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(10), null);
HttpHeaders headers =
HttpHeaders.of(
Collections.singletonMap(
RequestTelemetry.HEADER_NAME, Collections.singletonList("existing")));
@SuppressWarnings("deprecation")
Optional<String> result = telemetry.getHeaderValue(headers);
assertFalse(
result.isPresent(),
"getHeaderValue should return empty when header is already present");
}
@Test
public void testGetHeaderValueReturnsTelemetryWhenHeaderAbsent() {
StripeResponse response = buildResponse("req_no_header");
telemetry.maybeEnqueueMetrics(response, Duration.ofMillis(55), null);
HttpHeaders headers = HttpHeaders.of(Collections.emptyMap());
@SuppressWarnings("deprecation")
Optional<String> result = telemetry.getHeaderValue(headers);
assertTrue(result.isPresent(), "getHeaderValue should return telemetry when header is absent");
JsonObject metrics =
JsonParser.parseString(result.get())
.getAsJsonObject()
.getAsJsonObject("last_request_metrics");
assertEquals("req_no_header", metrics.get("request_id").getAsString());
}
// ---- helpers ----
private static StripeResponse buildResponse(String requestId) {
java.util.Map<String, java.util.List<String>> headerMap = new java.util.HashMap<>();
if (requestId != null) {
headerMap.put("Request-Id", Collections.singletonList(requestId));
}
return new StripeResponse(200, HttpHeaders.of(headerMap), "{}");
}
}