kl0u commented on a change in pull request #9974: [FLINK-14501][FLINK-14502] 
Decouple ClusterDescriptor/ClusterSpecification from CommandLine
URL: https://github.com/apache/flink/pull/9974#discussion_r339470136
 
 

 ##########
 File path: 
flink-clients/src/test/java/org/apache/flink/client/deployment/ClusterClientServiceLoaderTest.java
 ##########
 @@ -0,0 +1,167 @@
+/*
+ * 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.client.deployment;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.DeploymentOptions;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.annotation.Nullable;
+
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for the {@link DefaultClusterClientServiceLoader}.
+ */
+public class ClusterClientServiceLoaderTest {
+
+       private static final String VALID_TARGET = "existing";
+       private static final String AMBIGUOUS_TARGET = "duplicate";
+       private static final String NON_EXISTING_TARGET = "non-existing";
+
+       private static final int VALID_ID = 42;
+
+       private ClusterClientServiceLoader serviceLoaderUnderTest;
+
+       @Before
+       public void init() {
+               serviceLoaderUnderTest = new 
DefaultClusterClientServiceLoader();
+       }
+
+       @Test
+       public void testStandaloneClusterClientFactoryDiscovery() {
+               final Configuration config = new Configuration();
+               config.setString(DeploymentOptions.TARGET, 
StandaloneClientFactory.ID);
+
+               ClusterClientFactory<StandaloneClusterId> factory = 
serviceLoaderUnderTest.getClusterClientFactory(config);
+               assertTrue(factory instanceof StandaloneClientFactory);
+       }
+
+       @Test
+       public void testFactoryDiscovery() {
+               final Configuration config = new Configuration();
+               config.setString(DeploymentOptions.TARGET, VALID_TARGET);
+
+               final ClusterClientFactory<Integer> factory = 
serviceLoaderUnderTest.getClusterClientFactory(config);
+               assertNotNull(factory);
+
+               final Integer id = factory.getClusterId(config);
+               assertThat(id, allOf(is(notNullValue()), equalTo(VALID_ID)));
+       }
+
+       @Test(expected = IllegalStateException.class)
+       public void testMoreThanOneCompatibleFactoriesException() {
+               final Configuration config = new Configuration();
+               config.setString(DeploymentOptions.TARGET, AMBIGUOUS_TARGET);
+
+               serviceLoaderUnderTest.getClusterClientFactory(config);
+               fail();
+       }
+
+       @Test
+       public void testNoFactoriesFound() {
+               final Configuration config = new Configuration();
+               config.setString(DeploymentOptions.TARGET, NON_EXISTING_TARGET);
+
+               final ClusterClientFactory<Integer> factory = 
serviceLoaderUnderTest.getClusterClientFactory(config);
+               assertNull(factory);
+       }
+
+       /**
+        * Test {@link ClusterClientFactory} that is successfully discovered.
+        */
+       public static class ValidClusterClientFactory extends 
DummyClusterClientFactory {
+
+               public static final String ID = VALID_TARGET;
+
+               @Override
+               public boolean isCompatibleWith(Configuration configuration) {
+                       return 
configuration.getString(DeploymentOptions.TARGET).equals(VALID_TARGET);
+               }
+
+               @Nullable
+               @Override
+               public Integer getClusterId(Configuration configuration) {
+                       return VALID_ID;
+               }
+       }
+
+       /**
+        * Test {@link ClusterClientFactory} that has a duplicate.
+        */
+       public static class FirstCollidingClusterClientFactory extends 
DummyClusterClientFactory {
+
+               public static final String ID = AMBIGUOUS_TARGET;
+
+               @Override
+               public boolean isCompatibleWith(Configuration configuration) {
+                       return 
configuration.getString(DeploymentOptions.TARGET).equals(AMBIGUOUS_TARGET);
+               }
+       }
+
+       /**
+        * Test {@link ClusterClientFactory} that has a duplicate.
+        */
+       public static class SecondCollidingClusterClientFactory extends 
DummyClusterClientFactory {
+
+               public static final String ID = AMBIGUOUS_TARGET;
+
+               @Override
+               public boolean isCompatibleWith(Configuration configuration) {
+                       return 
configuration.getString(DeploymentOptions.TARGET).equals(AMBIGUOUS_TARGET);
+               }
+       }
+
+       /**
+        * A base test {@link ClusterClientFactory} that supports no operation 
and is meant to be extended.
+        */
+       public static class DummyClusterClientFactory implements 
ClusterClientFactory<Integer> {
 
 Review comment:
   I see. I will rename.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to