Skip to content

Commit

Permalink
fix fabric8io#4365: adding a test for the termination exception
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Sep 14, 2022
1 parent 0742e94 commit be5dba7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Expand Up @@ -61,7 +61,15 @@ public Reflector(ListerWatcher<T, L> listerWatcher, SyncableStore<T> store) {

public CompletableFuture<Void> start() {
this.running = true;
return listSyncAndWatch(false);
CompletableFuture<Void> result = listSyncAndWatch(false);
result.whenComplete((v, t) -> {
if (t != null) {
stopFuture.completeExceptionally(t);
} else {
stopFuture.complete(null);
}
});
return result;
}

public void stop() {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.api.model.WatchEvent;
Expand All @@ -36,7 +37,9 @@
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder;
import io.fabric8.kubernetes.client.CustomResourceList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
Expand Down Expand Up @@ -65,6 +68,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand Down Expand Up @@ -897,6 +901,51 @@ public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
assertFalse(podInformer.isRunning());
}

@Test
void testTerminalException() throws InterruptedException, TimeoutException {
// should be an initial 404
SharedIndexInformer<Pod> informer = client.pods().runnableInformer(0);
try {
informer.run();
} catch (Exception e) {
}
try {
informer.stopped().get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof KubernetesClientException);
}

String startResourceVersion = "1000";

// initial list
server.expect().withPath("/api/v1/pods")
.andReturn(200, new PodListBuilder().withNewMetadata().withResourceVersion(startResourceVersion).endMetadata()
.withItems(Collections.emptyList()).build())
.once();

// initial watch - terminates with an exception
server.expect().withPath("/api/v1/pods?resourceVersion=" + startResourceVersion + "&allowWatchBookmarks=true&watch=true")
.andUpgradeToWebSocket()
.open()
.waitFor(WATCH_EVENT_EMIT_TIME)
.andEmit(new WatchEvent(new Service(), "ADDED")) // not a pod
.waitFor(OUTDATED_WATCH_EVENT_EMIT_TIME)
.andEmit(outdatedEvent)
.done().always();

// When
informer = client.pods().inAnyNamespace().runnableInformer(0);
try {
informer.run();
} catch (Exception e) {
}
try {
informer.stopped().get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof WatcherException);
}
}

@Test
void testRunAfterStop() {
// Given
Expand Down

0 comments on commit be5dba7

Please sign in to comment.