Skip to content

Commit d0170a9

Browse files
committed
Improve OpAMP client test reliability
- Remove debug print statements that caused build warnings - Keep increased timeouts (5s instead of 1s) for takeRequest() and await() calls - Add small delays (100ms) before checking requests to allow async operations to complete - This should resolve sporadic test failures due to timing issues
1 parent 60368a0 commit d0170a9

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

opamp-client/src/test/java/io/opentelemetry/opamp/client/internal/impl/OpampClientImplTest.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,52 +231,48 @@ void onSuccess_withNoChangesToReport_doNotNotifyCallbackOnMessage() {
231231

232232
@Test
233233
void verifyAgentDescriptionSetter() {
234-
System.out.println("DEBUG: Starting verifyAgentDescriptionSetter test");
235234
initializeClient();
236235
AgentDescription agentDescription =
237236
getAgentDescriptionWithOneIdentifyingValue("service.name", "My service");
238237

239238
// Update when changed
240-
System.out.println("DEBUG: Setting agent description and expecting request...");
241239
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
242240
client.setAgentDescription(agentDescription);
241+
// Small delay to allow async request to be sent
242+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
243243
RecordedRequest firstRequest = takeRequest();
244-
System.out.println("DEBUG: First request result: " + (firstRequest != null));
245244
assertThat(firstRequest).isNotNull();
246245

247246
// Ignore when the provided value is the same as the current one
248-
System.out.println("DEBUG: Setting same agent description and expecting no request...");
249247
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
250248
client.setAgentDescription(agentDescription);
249+
// Small delay to allow async request to be sent (or not sent in this case)
250+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
251251
RecordedRequest secondRequest = takeRequest();
252-
System.out.println("DEBUG: Second request result: " + (secondRequest != null));
253252
assertThat(secondRequest).isNull();
254-
System.out.println("DEBUG: verifyAgentDescriptionSetter test completed");
255253
}
256254

257255
@Test
258256
void verifyRemoteConfigStatusSetter() {
259-
System.out.println("DEBUG: Starting verifyRemoteConfigStatusSetter test");
260257
initializeClient();
261258
RemoteConfigStatus remoteConfigStatus =
262259
getRemoteConfigStatus(RemoteConfigStatuses.RemoteConfigStatuses_APPLYING);
263260

264261
// Update when changed
265-
System.out.println("DEBUG: Setting remote config status and expecting request...");
266262
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
267263
client.setRemoteConfigStatus(remoteConfigStatus);
264+
// Small delay to allow async request to be sent
265+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
268266
RecordedRequest firstRequest = takeRequest();
269-
System.out.println("DEBUG: First request result: " + (firstRequest != null));
270267
assertThat(firstRequest).isNotNull();
271268

272269
// Ignore when the provided value is the same as the current one
273-
System.out.println("DEBUG: Setting same remote config status and expecting no request...");
274270
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
275271
client.setRemoteConfigStatus(remoteConfigStatus);
272+
// Small delay to allow async request to be sent (or not sent in this case)
273+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
276274
RecordedRequest secondRequest = takeRequest();
277-
System.out.println("DEBUG: Second request result: " + (secondRequest != null));
278275
assertThat(secondRequest).isNull();
279-
System.out.println("DEBUG: verifyRemoteConfigStatusSetter test completed");
280276
}
281277

282278
@Test
@@ -291,7 +287,6 @@ void onConnectionSuccessful_notifyCallback() {
291287

292288
@Test
293289
void onFailedResponse_keepFieldsForNextRequest() {
294-
System.out.println("DEBUG: Starting onFailedResponse_keepFieldsForNextRequest test");
295290
initializeClient();
296291

297292
// Mock failed request
@@ -300,35 +295,31 @@ void onFailedResponse_keepFieldsForNextRequest() {
300295
// Adding a non-constant field
301296
AgentDescription agentDescription =
302297
getAgentDescriptionWithOneIdentifyingValue("service.namespace", "something");
303-
System.out.println("DEBUG: Setting agent description and expecting first request...");
304298
client.setAgentDescription(agentDescription);
299+
// Small delay to allow async request to be sent
300+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
305301

306302
// Assert first request contains it
307303
RecordedRequest firstRequest = takeRequest();
308-
System.out.println("DEBUG: First request agent_description: " +
309-
(getAgentToServerMessage(firstRequest).agent_description != null ? "present" : "null"));
310304
assertThat(getAgentToServerMessage(firstRequest).agent_description)
311305
.isEqualTo(agentDescription);
312306

313307
// Since it failed, send the agent description field in the next request
314-
System.out.println("DEBUG: Sending request after failure, expecting agent description to be kept...");
315308
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
316309
requestService.sendRequest();
310+
// Small delay to allow async request to be sent
311+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
317312
RecordedRequest secondRequest = takeRequest();
318-
System.out.println("DEBUG: Second request agent_description: " +
319-
(getAgentToServerMessage(secondRequest).agent_description != null ? "present" : "null"));
320313
assertThat(getAgentToServerMessage(secondRequest).agent_description)
321314
.isEqualTo(agentDescription);
322315

323316
// When there's no failure, do not keep it.
324-
System.out.println("DEBUG: Sending request after success, expecting agent description to be null...");
325317
enqueueServerToAgentResponse(new ServerToAgent.Builder().build());
326318
requestService.sendRequest();
319+
// Small delay to allow async request to be sent
320+
try { Thread.sleep(100); } catch (InterruptedException e) { /* ignore */ }
327321
RecordedRequest thirdRequest = takeRequest();
328-
System.out.println("DEBUG: Third request agent_description: " +
329-
(getAgentToServerMessage(thirdRequest).agent_description != null ? "present" : "null"));
330322
assertThat(getAgentToServerMessage(thirdRequest).agent_description).isNull();
331-
System.out.println("DEBUG: onFailedResponse_keepFieldsForNextRequest test completed");
332323
}
333324

334325
@Test
@@ -389,12 +380,8 @@ private static AgentToServer getAgentToServerMessage(RecordedRequest request) {
389380

390381
private RecordedRequest takeRequest() {
391382
try {
392-
System.out.println("DEBUG: Attempting to take request with 5 second timeout...");
393-
RecordedRequest request = server.takeRequest(5, TimeUnit.SECONDS);
394-
System.out.println("DEBUG: Request received: " + (request != null ? request.getRequestLine() : "null"));
395-
return request;
383+
return server.takeRequest(5, TimeUnit.SECONDS);
396384
} catch (InterruptedException e) {
397-
System.out.println("DEBUG: takeRequest interrupted");
398385
throw new RuntimeException(e);
399386
}
400387
}

0 commit comments

Comments
 (0)