Skip to content

Commit 17ac9d6

Browse files
committed
Removing executor for HttpRequestServiceTest
1 parent 77ae328 commit 17ac9d6

File tree

2 files changed

+100
-59
lines changed

2 files changed

+100
-59
lines changed

opamp-client/src/main/java/io/opentelemetry/opamp/client/internal/request/service/HttpRequestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void start(Callback callback, Supplier<Request> requestSupplier) {
9494
this.requestSupplier = requestSupplier;
9595
executor.start(this);
9696
} else {
97-
throw new IllegalStateException("RequestDispatcher is already running");
97+
throw new IllegalStateException("HttpRequestService is already running");
9898
}
9999
}
100100

opamp-client/src/test/java/io/opentelemetry/opamp/client/internal/request/service/HttpRequestServiceTest.java

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
import static org.mockito.ArgumentMatchers.any;
1111
import static org.mockito.ArgumentMatchers.anyInt;
1212
import static org.mockito.ArgumentMatchers.eq;
13-
import static org.mockito.Mockito.clearInvocations;
1413
import static org.mockito.Mockito.doThrow;
15-
import static org.mockito.Mockito.inOrder;
1614
import static org.mockito.Mockito.mock;
1715
import static org.mockito.Mockito.never;
1816
import static org.mockito.Mockito.verify;
19-
import static org.mockito.Mockito.verifyNoInteractions;
2017
import static org.mockito.Mockito.when;
2118

2219
import io.opentelemetry.opamp.client.internal.connectivity.http.HttpErrorException;
@@ -28,82 +25,64 @@
2825
import io.opentelemetry.opamp.client.internal.request.delay.PeriodicTaskExecutor;
2926
import io.opentelemetry.opamp.client.internal.response.Response;
3027
import java.io.ByteArrayInputStream;
28+
import java.io.Closeable;
3129
import java.time.Duration;
30+
import java.util.ArrayList;
31+
import java.util.Collections;
32+
import java.util.List;
33+
import java.util.NoSuchElementException;
34+
import java.util.Queue;
3235
import java.util.concurrent.CompletableFuture;
36+
import java.util.concurrent.ConcurrentLinkedQueue;
37+
import java.util.concurrent.CountDownLatch;
3338
import java.util.concurrent.ExecutionException;
3439
import java.util.concurrent.TimeUnit;
3540
import java.util.concurrent.TimeoutException;
41+
import java.util.concurrent.atomic.AtomicInteger;
3642
import java.util.function.Supplier;
3743
import opamp.proto.AgentToServer;
3844
import opamp.proto.RetryInfo;
3945
import opamp.proto.ServerErrorResponse;
4046
import opamp.proto.ServerErrorResponseType;
4147
import opamp.proto.ServerToAgent;
48+
import org.junit.jupiter.api.AfterEach;
4249
import org.junit.jupiter.api.BeforeEach;
4350
import org.junit.jupiter.api.Test;
4451
import org.junit.jupiter.api.extension.ExtendWith;
4552
import org.mockito.ArgumentCaptor;
46-
import org.mockito.InOrder;
4753
import org.mockito.Mock;
4854
import org.mockito.junit.jupiter.MockitoExtension;
4955

5056
@SuppressWarnings("unchecked")
5157
@ExtendWith(MockitoExtension.class)
5258
class HttpRequestServiceTest {
53-
@Mock private HttpSender requestSender;
54-
@Mock private PeriodicDelay periodicRequestDelay;
55-
@Mock private TestPeriodicRetryDelay periodicRetryDelay;
56-
@Mock private PeriodicTaskExecutor executor;
5759
@Mock private RequestService.Callback callback;
5860
@Mock private Supplier<Request> requestSupplier;
61+
private TestHttpSender requestSender;
62+
private final PeriodicDelay periodicRequestDelay =
63+
PeriodicDelay.ofFixedDuration(Duration.ofSeconds(1));
64+
private TestPeriodicRetryDelay periodicRetryDelay;
5965
private int requestSize = -1;
6066
private HttpRequestService httpRequestService;
6167

6268
@BeforeEach
6369
void setUp() {
70+
requestSender = new TestHttpSender();
71+
periodicRetryDelay = new TestPeriodicRetryDelay(Duration.ofSeconds(2));
6472
httpRequestService =
6573
new HttpRequestService(
6674
requestSender,
67-
executor,
75+
PeriodicTaskExecutor.create(periodicRequestDelay),
6876
periodicRequestDelay,
6977
periodicRetryDelay,
7078
RetryAfterParser.getInstance());
71-
}
72-
73-
@Test
74-
void verifyStart() {
7579
httpRequestService.start(callback, requestSupplier);
76-
77-
InOrder inOrder = inOrder(periodicRequestDelay, executor);
78-
inOrder.verify(executor).start(httpRequestService);
79-
80-
// Try starting it again:
81-
try {
82-
httpRequestService.start(callback, requestSupplier);
83-
fail();
84-
} catch (IllegalStateException e) {
85-
assertThat(e).hasMessage("RequestDispatcher is already running");
86-
}
8780
}
8881

89-
@Test
90-
void verifyStop() {
91-
httpRequestService.start(callback, requestSupplier);
92-
httpRequestService.stop();
93-
94-
verify(executor).stop();
95-
96-
// Try stopping it again:
97-
clearInvocations(executor);
98-
httpRequestService.stop();
99-
verifyNoInteractions(executor);
100-
}
101-
102-
@Test
103-
void verifyStop_whenNotStarted() {
82+
@AfterEach
83+
void tearDown() {
84+
requestSender.close();
10485
httpRequestService.stop();
105-
106-
verifyNoInteractions(executor, requestSender, periodicRequestDelay);
10786
}
10887

10988
@Test
@@ -126,9 +105,12 @@ void verifySendingRequest_happyPath() {
126105
prepareRequest();
127106
enqueueResponse(httpResponse);
128107

129-
httpRequestService.run();
108+
httpRequestService.sendRequest();
130109

131-
verify(requestSender).send(any(), eq(requestSize));
110+
requestSender.awaitForRequest(Duration.ofMillis(500));
111+
assertThat(requestSender.requests).hasSize(1);
112+
assertThat(requestSender.requests.get(0).contentLength).isEqualTo(requestSize);
113+
verify(callback).onConnectionSuccess();
132114
verify(callback).onRequestSuccess(Response.create(serverToAgent));
133115
}
134116

@@ -186,7 +168,6 @@ void verifySendingRequest_whenThereIsAGenericHttpError() {
186168
httpRequestService.run();
187169

188170
verifyRequestFailedCallback(500);
189-
verifyNoInteractions(executor);
190171
}
191172

192173
@Test
@@ -200,7 +181,7 @@ void verifySendingRequest_whenThereIsATooManyRequestsError() {
200181
httpRequestService.run();
201182

202183
verifyRequestFailedCallback(429);
203-
verify(executor).setPeriodicDelay(periodicRetryDelay);
184+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
204185
verify(periodicRetryDelay, never()).suggestDelay(any());
205186
}
206187

@@ -216,7 +197,7 @@ void verifySendingRequest_whenThereIsATooManyRequestsError_withSuggestedDelay()
216197
httpRequestService.run();
217198

218199
verifyRequestFailedCallback(429);
219-
verify(executor).setPeriodicDelay(periodicRetryDelay);
200+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
220201
verify(periodicRetryDelay).suggestDelay(Duration.ofSeconds(5));
221202
}
222203

@@ -239,7 +220,7 @@ void verifySendingRequest_whenServerProvidesRetryInfo_usingTheProvidedInfo() {
239220

240221
verify(callback).onRequestSuccess(Response.create(serverToAgent));
241222
verify(periodicRetryDelay).suggestDelay(Duration.ofNanos(nanosecondsToWaitForRetry));
242-
verify(executor).setPeriodicDelay(periodicRetryDelay);
223+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
243224
}
244225

245226
@Test
@@ -258,7 +239,7 @@ void verifySendingRequest_whenServerIsUnavailable() {
258239

259240
verify(callback).onRequestSuccess(Response.create(serverToAgent));
260241
verify(periodicRetryDelay, never()).suggestDelay(any());
261-
verify(executor).setPeriodicDelay(periodicRetryDelay);
242+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
262243
}
263244

264245
@Test
@@ -272,7 +253,7 @@ void verifySendingRequest_whenThereIsAServiceUnavailableError() {
272253
httpRequestService.run();
273254

274255
verifyRequestFailedCallback(503);
275-
verify(executor).setPeriodicDelay(periodicRetryDelay);
256+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
276257
verify(periodicRetryDelay, never()).suggestDelay(any());
277258
}
278259

@@ -288,7 +269,7 @@ void verifySendingRequest_whenThereIsAServiceUnavailableError_withSuggestedDelay
288269
httpRequestService.run();
289270

290271
verifyRequestFailedCallback(503);
291-
verify(executor).setPeriodicDelay(periodicRetryDelay);
272+
// verify(executor).setPeriodicDelay(periodicRetryDelay); todo
292273
verify(periodicRetryDelay).suggestDelay(Duration.ofSeconds(2));
293274
}
294275

@@ -303,7 +284,7 @@ private void verifyRequestFailedCallback(int errorCode) {
303284
void verifySendingRequest_duringRegularMode() {
304285
httpRequestService.sendRequest();
305286

306-
verify(executor).executeNow();
287+
// verify(executor).executeNow(); todo
307288
}
308289

309290
@Test
@@ -312,7 +293,7 @@ void verifySendingRequest_duringRetryMode() {
312293

313294
httpRequestService.sendRequest();
314295

315-
verify(executor, never()).executeNow();
296+
// verify(executor, never()).executeNow(); todo
316297
}
317298

318299
@Test
@@ -325,7 +306,7 @@ void verifySuccessfulSendingRequest_duringRetryMode() {
325306

326307
httpRequestService.run();
327308

328-
verify(executor).setPeriodicDelay(periodicRequestDelay);
309+
// verify(executor).setPeriodicDelay(periodicRequestDelay); todo
329310
}
330311

331312
private void enableRetryMode() {
@@ -339,17 +320,14 @@ private void enableRetryMode() {
339320
}
340321

341322
private void prepareRequest() {
342-
httpRequestService.start(callback, requestSupplier);
343-
clearInvocations(executor);
344323
AgentToServer agentToServer = new AgentToServer.Builder().sequence_num(10).build();
345324
requestSize = agentToServer.encodeByteString().size();
346325
Request request = Request.create(agentToServer);
347326
when(requestSupplier.get()).thenReturn(request);
348327
}
349328

350329
private void enqueueResponse(HttpSender.Response httpResponse) {
351-
when(requestSender.send(any(), anyInt()))
352-
.thenReturn(CompletableFuture.completedFuture(httpResponse));
330+
requestSender.enqueueResponse(httpResponse);
353331
}
354332

355333
private static void attachServerToAgentMessage(
@@ -360,16 +338,79 @@ private static void attachServerToAgentMessage(
360338
}
361339

362340
private static class TestPeriodicRetryDelay implements PeriodicDelay, AcceptsDelaySuggestion {
341+
private final Duration delay;
342+
343+
private TestPeriodicRetryDelay(Duration delay) {
344+
this.delay = delay;
345+
}
363346

364347
@Override
365348
public void suggestDelay(Duration delay) {}
366349

367350
@Override
368351
public Duration getNextDelay() {
369-
return null;
352+
return delay;
370353
}
371354

372355
@Override
373356
public void reset() {}
374357
}
358+
359+
private static class TestHttpSender implements HttpSender, Closeable {
360+
private final List<RequestParams> requests = Collections.synchronizedList(new ArrayList<>());
361+
private final Queue<HttpSender.Response> responses = new ConcurrentLinkedQueue<>();
362+
private final AtomicInteger unexpectedRequests = new AtomicInteger(0);
363+
private volatile CountDownLatch latch;
364+
365+
@Override
366+
public CompletableFuture<HttpSender.Response> send(BodyWriter writer, int contentLength) {
367+
requests.add(new RequestParams(contentLength));
368+
HttpSender.Response response = null;
369+
try {
370+
response = responses.remove();
371+
if (latch != null) {
372+
latch.countDown();
373+
}
374+
} catch (NoSuchElementException e) {
375+
unexpectedRequests.incrementAndGet();
376+
}
377+
return CompletableFuture.completedFuture(response);
378+
}
379+
380+
public void enqueueResponse(HttpSender.Response response) {
381+
responses.add(response);
382+
}
383+
384+
public void awaitForRequest(Duration timeout) {
385+
if (latch != null) {
386+
throw new IllegalStateException();
387+
}
388+
latch = new CountDownLatch(1);
389+
try {
390+
if (!latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
391+
fail("No request received before timeout " + timeout);
392+
}
393+
} catch (InterruptedException e) {
394+
throw new RuntimeException(e);
395+
} finally {
396+
latch = null;
397+
}
398+
}
399+
400+
@Override
401+
public void close() {
402+
int count = unexpectedRequests.get();
403+
if (count > 0) {
404+
fail("Unexpected requests count: " + count);
405+
}
406+
}
407+
408+
private static class RequestParams {
409+
public final int contentLength;
410+
411+
private RequestParams(int contentLength) {
412+
this.contentLength = contentLength;
413+
}
414+
}
415+
}
375416
}

0 commit comments

Comments
 (0)