forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric8io#2812: SharedIndexInformer EventHandler sees double upda…
…tes at resync interval + Remove scheduleAtFixedDelay execution of Reflector#resyncAndList(), resync should happen in memory without contacting the apiserver just like client go implementation + Added logic for watch.close() when Reflector#stop() is called
- Loading branch information
1 parent
700acec
commit a541e36
Showing
4 changed files
with
124 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
...etes-client/src/test/java/io/fabric8/kubernetes/client/informers/cache/ReflectorTest.java
This file was deleted.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
kubernetes-itests/src/test/java/io/fabric8/kubernetes/DefaultSharedIndexInformerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.fabric8.kubernetes; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.kubernetes.api.model.PodList; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.informers.ResourceEventHandler; | ||
import io.fabric8.kubernetes.client.informers.SharedIndexInformer; | ||
import io.fabric8.kubernetes.client.informers.SharedInformerFactory; | ||
import org.arquillian.cube.kubernetes.api.Session; | ||
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes; | ||
import org.arquillian.cube.requirement.ArquillianConditionalRunner; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
@RunWith(ArquillianConditionalRunner.class) | ||
@RequiresKubernetes | ||
public class DefaultSharedIndexInformerIT { | ||
@ArquillianResource | ||
KubernetesClient client; | ||
|
||
private static final long RESYNC_PERIOD = 1000L; | ||
|
||
@Test | ||
public void testPodSharedIndexInformerGetsEvents() throws InterruptedException { | ||
// Given | ||
CountDownLatch addEvents = new CountDownLatch(1); | ||
CountDownLatch updateEvents = new CountDownLatch(0); | ||
SharedInformerFactory informerFactory = client.informers(); | ||
SharedIndexInformer<Pod> podInformer = informerFactory.sharedIndexInformerFor(Pod.class, RESYNC_PERIOD); | ||
podInformer.addEventHandler(new TestResourceEventHandler<>(addEvents, updateEvents)); | ||
|
||
// When | ||
informerFactory.startAllRegisteredInformers(); | ||
addEvents.await(2 * RESYNC_PERIOD, TimeUnit.MILLISECONDS); | ||
|
||
// Then | ||
assertThat(addEvents.getCount()).isZero(); | ||
informerFactory.stopAllRegisteredInformers(); | ||
} | ||
|
||
@Test | ||
public void testPodSharedIndexInformerGetsSingleUpdates() throws InterruptedException { | ||
// Given | ||
String namespace = "kube-system"; | ||
PodList podList = client.pods().inNamespace(namespace).list(); | ||
int expectedAddEvents = podList.getItems().size(); | ||
CountDownLatch addEvents = new CountDownLatch(expectedAddEvents); | ||
// Let's assume informer resyncs at least two times so we would get update | ||
// events for all pods two times | ||
CountDownLatch updateEvents = new CountDownLatch(2 * expectedAddEvents); | ||
SharedInformerFactory informerFactory = client.informers(); | ||
SharedIndexInformer<Pod> podInformer = informerFactory.inNamespace(namespace).sharedIndexInformerFor(Pod.class, RESYNC_PERIOD); | ||
TestResourceEventHandler<Pod> eventHandler = new TestResourceEventHandler<>(addEvents, updateEvents); | ||
podInformer.addEventHandler(eventHandler); | ||
|
||
// When | ||
informerFactory.startAllRegisteredInformers(); | ||
updateEvents.await(3 * RESYNC_PERIOD, TimeUnit.MILLISECONDS); | ||
|
||
// Then | ||
assertThat(addEvents.getCount()).isZero(); | ||
assertThat(updateEvents.getCount()).isZero(); | ||
assertThat(eventHandler.getUpdateEventsCount()).isLessThan(4 * expectedAddEvents); | ||
informerFactory.stopAllRegisteredInformers(); | ||
} | ||
|
||
private static class TestResourceEventHandler<T extends HasMetadata> implements ResourceEventHandler<T> { | ||
private final CountDownLatch addEventRecievedLatch; | ||
private final CountDownLatch updateEventRecievedLatch; | ||
private final AtomicInteger updateCount; | ||
|
||
public TestResourceEventHandler(CountDownLatch addLatch, CountDownLatch updateLatch) { | ||
this.updateEventRecievedLatch = updateLatch; | ||
this.addEventRecievedLatch = addLatch; | ||
this.updateCount = new AtomicInteger(0); | ||
} | ||
|
||
@Override | ||
public void onAdd(T resource) { | ||
addEventRecievedLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onUpdate(T oldResource, T newResource) { | ||
updateEventRecievedLatch.countDown(); | ||
updateCount.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public void onDelete(T obj, boolean deletedFinalStateUnknown) { } | ||
|
||
public int getUpdateEventsCount() { | ||
return updateCount.get(); | ||
} | ||
} | ||
} |