yittg commented on a change in pull request #15501:
URL: https://github.com/apache/flink/pull/15501#discussion_r636764963



##########
File path: 
flink-kubernetes/src/test/java/org/apache/flink/kubernetes/kubeclient/resources/KubernetesSharedInformerITCase.java
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.flink.kubernetes.kubeclient.resources;
+
+import org.apache.flink.kubernetes.KubernetesResource;
+import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;
+import org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher;
+import org.apache.flink.kubernetes.kubeclient.KubernetesSharedWatcher.Watch;
+import org.apache.flink.util.TestLogger;
+
+import org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableMap;
+
+import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+/** IT Tests for the {@link KubernetesSharedInformer}. */
+public class KubernetesSharedInformerITCase extends TestLogger {
+
+    @ClassRule public static KubernetesResource kubernetesResource = new 
KubernetesResource();
+
+    @Test
+    public void testWatch() throws Exception {
+        final ImmutableMap<String, String> labels =
+                ImmutableMap.of("app", "shared-informer-test-cluster");
+        final String configMapPrefix = "shared-informer-test-cluster";
+        try (FlinkKubeClient client = kubernetesResource.getFlinkKubeClient();
+                KubernetesConfigMapSharedWatcher configMapSharedWatcher =
+                        client.createConfigMapSharedWatcher(labels)) {
+
+            configMapSharedWatcher.run();
+            createConfigMapsInRange(client, labels, configMapPrefix, 0, 5);
+
+            List<TestingCallbackHandler> callbackHandlers = new ArrayList<>();
+            List<Watch> watchers = new ArrayList<>();
+            for (int i = 0; i < 100; i++) {
+                final String name = configMapPrefix + i % 10;
+                final TestingCallbackHandler handler = new 
TestingCallbackHandler(name);
+                callbackHandlers.add(handler);
+                final Watch watch = configMapSharedWatcher.watch(name, 
handler);
+                watchers.add(watch);
+            }
+
+            createConfigMapsInRange(client, labels, configMapPrefix, 5, 10);
+            for (int i = 0; i < 10; i++) {
+                final String configMapName = configMapPrefix + i;
+                client.checkAndUpdateConfigMap(
+                                configMapName,
+                                configMap -> {
+                                    configMap.getData().put("foo", "bar");
+                                    return Optional.of(configMap);
+                                })
+                        .get();
+            }
+            try {
+                for (TestingCallbackHandler handler : callbackHandlers) {
+                    handler.addFuture.get(2000, TimeUnit.MILLISECONDS);
+                    handler.addOrUpdateFuture.get(2000, TimeUnit.MILLISECONDS);
+                }
+                client.deleteConfigMapsByLabels(labels).get();
+                for (TestingCallbackHandler handler : callbackHandlers) {
+                    handler.deleteFuture.get(2000, TimeUnit.MILLISECONDS);
+                }
+            } finally {
+                client.deleteConfigMapsByLabels(labels).get();
+            }
+            watchers.forEach(Watch::close);
+        }
+    }
+
+    private void createConfigMapsInRange(
+            FlinkKubeClient client,
+            Map<String, String> labels,
+            String namePrefix,
+            int start,
+            int end)
+            throws Exception {
+        for (int i = start; i < end; i++) {
+            client.createConfigMap(
+                            new KubernetesConfigMap(
+                                    new ConfigMapBuilder()
+                                            .withNewMetadata()
+                                            .withName(namePrefix + i)
+                                            .withLabels(labels)
+                                            .endMetadata()
+                                            .build()))
+                    .get();
+        }
+    }
+
+    private static final class TestingCallbackHandler
+            extends NoOpWatchCallbackHandler<KubernetesConfigMap> {
+
+        private final CompletableFuture<Void> addFuture = new 
CompletableFuture<>();
+        private final CompletableFuture<Void> addOrUpdateFuture = new 
CompletableFuture<>();
+        private final CompletableFuture<Void> deleteFuture = new 
CompletableFuture<>();
+
+        private final String name;
+
+        private TestingCallbackHandler(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public void onAdded(List<KubernetesConfigMap> resources) {
+            final KubernetesConfigMap kubernetesConfigMap = resources.get(0);
+            assertThat(kubernetesConfigMap.getName(), is(name));
+            addFuture.complete(null);
+            final String foo = kubernetesConfigMap.getData().get("foo");
+            if (foo != null) {
+                assertThat(foo, is("bar"));
+                addOrUpdateFuture.complete(null);
+            }
+        }
+
+        @Override
+        public void onModified(List<KubernetesConfigMap> resources) {
+            final KubernetesConfigMap kubernetesConfigMap = resources.get(0);
+            assertThat(kubernetesConfigMap.getName(), is(name));
+            final String foo = kubernetesConfigMap.getData().get("foo");
+            assertThat(foo, is("bar"));
+            if (addOrUpdateFuture.isDone()) {
+                assertNotNull(
+                        kubernetesConfigMap
+                                .getInternalResource()
+                                .getMetadata()
+                                .getDeletionTimestamp());
+            } else {

Review comment:
       Yeah, i will add a validation for it.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to