use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.
the class ResourceTest method testWatch.
@Test
void testWatch() throws InterruptedException {
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withResourceVersion("1").withNamespace("test").and().build();
server.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pod1).once();
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(201, pod1).once();
server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "DELETED")).done().always();
final CountDownLatch latch = new CountDownLatch(1);
Watch watch = client.resource(pod1).watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
latch.countDown();
}
@Override
public void onClose(WatcherException cause) {
}
});
Assert.assertTrue(latch.await(5000, MILLISECONDS));
watch.close();
}
use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.
the class WatchTest method testTryWithResourcesCantConnectShouldCloseAndThenThrowException.
@Test
@DisplayName("TryWithResources, receives error when connecting, should NOT receive events and close before propagating the connect exception")
void testTryWithResourcesCantConnectShouldCloseAndThenThrowException() throws Exception {
// Given
final CountDownLatch closeLatch = new CountDownLatch(1);
server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andReturn(410, outdatedEvent()).once();
final Watcher<Pod> watcher = new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
fail();
}
@Override
public void onClose(WatcherException cause) {
fail("Close event should be invoked by try-with-resources successful completion, not by exception");
}
@Override
public void onClose() {
closeLatch.countDown();
}
};
final Watchable<Watcher<Pod>> watchable = client.pods().withName("pod1").withResourceVersion("1");
// When
final KubernetesClientException result = Assertions.assertThrows(KubernetesClientException.class, () -> {
try (Watch watch = watchable.watch(watcher)) {
assertNull(watch);
fail("Close with resources should call Watcher#onClose when closing watch");
}
});
// Then
assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
assertEquals(410, result.getCode());
}
use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.
the class WatchTest method testOnCloseEvent.
@Test
void testOnCloseEvent() throws InterruptedException {
final CountDownLatch eventLatch = new CountDownLatch(2);
final CountDownLatch closeLatch = new CountDownLatch(1);
server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "MODIFIED")).waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "MODIFIED")).done().once();
Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
eventLatch.countDown();
}
@Override
public void onClose(WatcherException cause) {
fail();
}
@Override
public void onClose() {
closeLatch.countDown();
}
});
assertTrue(eventLatch.await(10, TimeUnit.SECONDS));
watch.close();
assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
}
use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.
the class WatchTest method testTryWithResourcesConnectsThenReceivesEventBookmark.
@Test
@DisplayName("TryWithResources, connects and receives event then receives GONE, should receive first event and then close")
void testTryWithResourcesConnectsThenReceivesEventBookmark() throws InterruptedException {
// Given
server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(EVENT_WAIT_PERIOD_MS).andEmit(new WatchEvent(pod1, "BOOKMARK")).waitFor(EVENT_WAIT_PERIOD_MS).andEmit(outdatedEvent()).done().once();
final CountDownLatch bookmarkLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
final Watcher<Pod> watcher = new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
if (action != BOOKMARK) {
fail();
}
bookmarkLatch.countDown();
}
@Override
public void onClose(WatcherException cause) {
assertTrue(cause.isHttpGone());
closeLatch.countDown();
}
};
// When
try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new ListOptionsBuilder().withAllowWatchBookmarks(true).build(), watcher)) {
// Then
assertNotNull(watch);
assertTrue(bookmarkLatch.await(10, TimeUnit.SECONDS));
assertTrue(closeLatch.await(10, TimeUnit.SECONDS));
}
}
use of io.fabric8.kubernetes.client.Watcher.Action in project kubernetes-client by fabric8io.
the class WatchTest method testHttpErrorReconnect.
/**
* Will attempt a reconnect after 10ms..20ms...40ms....80ms.....160ms......320ms
*/
@Test
void testHttpErrorReconnect() throws InterruptedException {
// Given
client.getConfiguration().setWatchReconnectInterval(10);
final String path = "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true";
// accept watch and disconnect
server.expect().withPath(path).andUpgradeToWebSocket().open().done().once();
// refuse reconnect attempts 6 times
server.expect().withPath(path).andReturn(HttpURLConnection.HTTP_NOT_FOUND, new StatusBuilder().withCode(HttpURLConnection.HTTP_NOT_FOUND).build()).times(6);
// accept next reconnect and send outdated event to stop the watch
server.expect().withPath(path).andUpgradeToWebSocket().open(outdatedEvent()).done().once();
final CountDownLatch closeLatch = new CountDownLatch(1);
final Watcher<Pod> watcher = new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
fail();
}
@Override
public void onClose(WatcherException cause) {
assertTrue(cause.isHttpGone());
closeLatch.countDown();
}
};
// When
try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(watcher)) {
// Then
assertNotNull(watch);
assertTrue(closeLatch.await(3, TimeUnit.MINUTES));
}
}
Aggregations