@@ -86,14 +86,6 @@ void setUp() {
8686 new State .Flags ((long ) AgentToServerFlags .AgentToServerFlags_Unspecified .getValue ()),
8787 effectiveConfig );
8888 requestService = createHttpService ();
89- client = OpampClientImpl .create (requestService , state );
90-
91- // Prepare first request on start
92- ServerToAgent response = new ServerToAgent .Builder ().build ();
93- enqueueServerToAgentResponse (response );
94-
95- callbacks = spy (new TestCallbacks ());
96- client .start (callbacks );
9789 }
9890
9991 @ AfterEach
@@ -105,7 +97,7 @@ void tearDown() {
10597 void verifyFieldsSent () {
10698 // Check first request
10799 ServerToAgent response = new ServerToAgent .Builder ().build ();
108- RecordedRequest firstRequest = takeRequest ( );
100+ RecordedRequest firstRequest = initializeClient ( response );
109101 AgentToServer firstMessage = getAgentToServerMessage (firstRequest );
110102
111103 // Required first request fields
@@ -183,7 +175,7 @@ void verifyFieldsSent() {
183175
184176 @ Test
185177 void verifyStop () {
186- awaitForStartRequest ();
178+ initializeClient ();
187179
188180 enqueueServerToAgentResponse (new ServerToAgent .Builder ().build ());
189181 client .stop ();
@@ -194,6 +186,7 @@ void verifyStop() {
194186
195187 @ Test
196188 void verifyStartOnlyOnce () {
189+ initializeClient ();
197190 try {
198191 client .start (callbacks );
199192 fail ("Should have thrown an exception" );
@@ -204,7 +197,7 @@ void verifyStartOnlyOnce() {
204197
205198 @ Test
206199 void onSuccess_withChangesToReport_notifyCallbackOnMessage () {
207- awaitForStartRequest ();
200+ initializeClient ();
208201 AgentRemoteConfig remoteConfig =
209202 new AgentRemoteConfig .Builder ()
210203 .config (createAgentConfigMap ("someKey" , "someValue" ))
@@ -224,7 +217,7 @@ void onSuccess_withChangesToReport_notifyCallbackOnMessage() {
224217
225218 @ Test
226219 void onSuccess_withNoChangesToReport_doNotNotifyCallbackOnMessage () {
227- awaitForStartRequest ();
220+ initializeClient ();
228221 ServerToAgent serverToAgent = new ServerToAgent .Builder ().build ();
229222 enqueueServerToAgentResponse (serverToAgent );
230223
@@ -239,7 +232,7 @@ void onSuccess_withNoChangesToReport_doNotNotifyCallbackOnMessage() {
239232
240233 @ Test
241234 void verifyAgentDescriptionSetter () {
242- awaitForStartRequest ();
235+ initializeClient ();
243236 AgentDescription agentDescription =
244237 getAgentDescriptionWithOneIdentifyingValue ("service.name" , "My service" );
245238
@@ -256,7 +249,7 @@ void verifyAgentDescriptionSetter() {
256249
257250 @ Test
258251 void verifyRemoteConfigStatusSetter () {
259- awaitForStartRequest ();
252+ initializeClient ();
260253 RemoteConfigStatus remoteConfigStatus =
261254 getRemoteConfigStatus (RemoteConfigStatuses .RemoteConfigStatuses_APPLYING );
262255
@@ -273,15 +266,17 @@ void verifyRemoteConfigStatusSetter() {
273266
274267 @ Test
275268 void onConnectionSuccessful_notifyCallback () {
276- client .onConnectionSuccess ();
269+ initializeClient ();
270+
271+ await ().atMost (Duration .ofSeconds (1 )).until (() -> callbacks .onConnectCalls .get () == 1 );
277272
278273 verify (callbacks ).onConnect (client );
279274 verify (callbacks , never ()).onConnectFailed (any (), any ());
280275 }
281276
282277 @ Test
283278 void onFailedResponse_keepFieldsForNextRequest () {
284- awaitForStartRequest ();
279+ initializeClient ();
285280
286281 // Mock failed request
287282 server .enqueue (new MockResponse .Builder ().code (404 ).build ());
@@ -309,7 +304,7 @@ void onFailedResponse_keepFieldsForNextRequest() {
309304
310305 @ Test
311306 void onFailedResponse_withServerErrorData_notifyCallback () {
312- awaitForStartRequest ();
307+ initializeClient ();
313308
314309 ServerErrorResponse errorResponse = new ServerErrorResponse .Builder ().build ();
315310 enqueueServerToAgentResponse (new ServerToAgent .Builder ().error_response (errorResponse ).build ());
@@ -325,7 +320,7 @@ void onFailedResponse_withServerErrorData_notifyCallback() {
325320
326321 @ Test
327322 void onConnectionFailed_notifyCallback () {
328- awaitForStartRequest ();
323+ initializeClient ();
329324 Throwable throwable = new Throwable ();
330325
331326 client .onConnectionFailed (throwable );
@@ -335,7 +330,7 @@ void onConnectionFailed_notifyCallback() {
335330
336331 @ Test
337332 void whenServerProvidesNewInstanceUid_useIt () {
338- awaitForStartRequest ();
333+ initializeClient ();
339334 byte [] initialUid = state .instanceUid .get ();
340335
341336 byte [] serverProvidedUid = new byte [] {1 , 2 , 3 };
@@ -382,10 +377,6 @@ private static MockResponse getMockResponse(ServerToAgent response) {
382377 return new MockResponse .Builder ().code (200 ).body (bodyBuffer ).build ();
383378 }
384379
385- private void awaitForStartRequest () {
386- assertThat (takeRequest ()).isNotNull ();
387- }
388-
389380 private static RemoteConfigStatus getRemoteConfigStatus (RemoteConfigStatuses status ) {
390381 return new RemoteConfigStatus .Builder ().status (status ).build ();
391382 }
@@ -408,6 +399,21 @@ private static AgentDescription getAgentDescriptionWithOneIdentifyingValue(
408399 return new AgentDescription .Builder ().identifying_attributes (keyValues ).build ();
409400 }
410401
402+ private RecordedRequest initializeClient () {
403+ return initializeClient (new ServerToAgent .Builder ().build ());
404+ }
405+
406+ private RecordedRequest initializeClient (ServerToAgent initialResponse ) {
407+ client = OpampClientImpl .create (requestService , state );
408+
409+ // Prepare first request on start
410+ enqueueServerToAgentResponse (initialResponse );
411+
412+ callbacks = spy (new TestCallbacks ());
413+ client .start (callbacks );
414+ return takeRequest ();
415+ }
416+
411417 private static class TestEffectiveConfig extends State .EffectiveConfig {
412418 private opamp .proto .EffectiveConfig config ;
413419
0 commit comments