Skip to content

Commit 725ba25

Browse files
author
C
committed
onClose callback for LogWatch
- unit tests
1 parent 4524840 commit 725ba25

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package io.fabric8.kubernetes.client.dsl.internal;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.spy;
6+
import static org.mockito.Mockito.when;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.net.HttpURLConnection;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.CountDownLatch;
14+
import java.util.concurrent.Executor;
15+
import java.util.concurrent.Executors;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.Mockito;
21+
22+
import io.fabric8.kubernetes.client.http.AsyncBody;
23+
import io.fabric8.kubernetes.client.http.HttpClient;
24+
import io.fabric8.kubernetes.client.http.HttpRequest;
25+
import io.fabric8.kubernetes.client.http.HttpResponse;
26+
import io.fabric8.kubernetes.client.http.TestAsyncBody;
27+
import io.fabric8.kubernetes.client.http.TestHttpResponse;
28+
import io.fabric8.kubernetes.client.impl.BaseClient;
29+
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;
30+
31+
public class LogWatchCallbackTest {
32+
private OperationContext context;
33+
private Executor executor = Executors.newFixedThreadPool(2);
34+
private URL url;
35+
private HttpClient httpClientMock;
36+
37+
@BeforeEach
38+
public void setUp() throws MalformedURLException {
39+
BaseClient mock = mock(BaseClient.class, Mockito.RETURNS_SELF);
40+
Mockito.when(mock.adapt(BaseClient.class).getKubernetesSerialization()).thenReturn(new KubernetesSerialization());
41+
final OperationContext context = new OperationContext().withClient(mock);
42+
when(mock.getExecutor()).thenReturn(this.executor);
43+
this.context = context;
44+
45+
this.url = new URL("http://url_called");
46+
this.httpClientMock = spy(HttpClient.class);
47+
var httpRequestMock = mock(HttpRequest.class);
48+
var builderMock = mock(HttpRequest.Builder.class);
49+
50+
Mockito.when(httpClientMock.newHttpRequestBuilder()).thenReturn(builderMock);
51+
Mockito.when(builderMock.url(url)).thenReturn(builderMock);
52+
Mockito.when(builderMock.build()).thenReturn(httpRequestMock);
53+
54+
}
55+
56+
@Test
57+
public void withOutputStreamCloseEventTest() throws InterruptedException {
58+
59+
var future = new CompletableFuture<HttpResponse<AsyncBody>>();
60+
var reached = new CountDownLatch(1);
61+
62+
Mockito.when(httpClientMock.consumeBytes(Mockito.any(), Mockito.any())).thenReturn(future);
63+
64+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
65+
LogWatchCallback logWatch = new LogWatchCallback(baos, this.context);
66+
logWatch.callAndWait(httpClientMock, url);
67+
68+
logWatch.onClose().thenAccept((Throwable t) -> {
69+
reached.countDown();
70+
});
71+
future.complete(
72+
new TestHttpResponse<AsyncBody>().withCode(HttpURLConnection.HTTP_GONE).withBody(new TestAsyncBody()));
73+
74+
assertThat(reached.await(1, TimeUnit.SECONDS)).isTrue();
75+
logWatch.close();
76+
}
77+
78+
@Test
79+
public void withOutputStreamCloseEventOnFailureTest() throws MalformedURLException, InterruptedException {
80+
81+
var future = new CompletableFuture<HttpResponse<AsyncBody>>();
82+
var reached = new CountDownLatch(1);
83+
84+
Mockito.when(httpClientMock.consumeBytes(Mockito.any(), Mockito.any())).thenReturn(future);
85+
86+
LogWatchCallback logWatch = new LogWatchCallback(new ByteArrayOutputStream(), this.context);
87+
logWatch.callAndWait(httpClientMock, url);
88+
89+
final Throwable tReturned[] = new Throwable[1];
90+
logWatch.onClose().thenAccept((Throwable t) -> {
91+
tReturned[0] = t;
92+
reached.countDown();
93+
});
94+
95+
var th = new Throwable("any exception");
96+
future.completeExceptionally(th);
97+
98+
assertThat(reached.await(1, TimeUnit.SECONDS)).isTrue();
99+
assertThat(tReturned[0]).isEqualTo(th);
100+
101+
logWatch.close();
102+
}
103+
}

0 commit comments

Comments
 (0)