# ignite-1006: review
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/8fd909d9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/8fd909d9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/8fd909d9 Branch: refs/heads/ignite-973-2 Commit: 8fd909d98505fe3964bcf756e8bec42888c1164c Parents: a8232be Author: ashutak <ashu...@gridgain.com> Authored: Fri Jun 26 18:01:53 2015 +0300 Committer: ashutak <ashu...@gridgain.com> Committed: Fri Jun 26 18:01:53 2015 +0300 ---------------------------------------------------------------------- .../internal/cluster/ClusterGroupAdapter.java | 12 +- .../internal/ClusterForHostsSelfTest.java | 113 --------------- .../internal/ClusterGroupHostsSelfTest.java | 141 +++++++++++++++++++ .../ignite/internal/ClusterGroupSelfTest.java | 22 --- .../ignite/testsuites/IgniteBasicTestSuite.java | 2 +- 5 files changed, 146 insertions(+), 144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8fd909d9/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java index b770d98..4b61116 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java @@ -296,16 +296,12 @@ public class ClusterGroupAdapter implements ClusterGroupEx, Externalizable { /** {@inheritDoc} */ @Override public Collection<String> hostNames() { - Collection<String> resultHostNames = new HashSet<String> (); - Collection<ClusterNode> allNodes = nodes(); + Set<String> res = new HashSet<>(); - if (!(allNodes.isEmpty())) - { - for (ClusterNode currentNode : allNodes) - Collections.addAll(resultHostNames, currentNode.hostNames().toArray(new String[0])); - } + for (ClusterNode node : nodes()) + res.addAll(node.hostNames()); - return resultHostNames; + return Collections.unmodifiableSet(res); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8fd909d9/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java deleted file mode 100644 index 59c3db9..0000000 --- a/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.ignite.internal; - -import org.apache.ignite.*; -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.testframework.junits.common.*; - -import java.lang.reflect.*; -import java.util.*; - -/** - * Test for {@link ClusterGroup#forHost(String, String...)}. - * - * @see GridProjectionSelfTest - */ -@GridCommonTest(group = "Kernal Self") -public class ClusterForHostsSelfTest extends GridCommonAbstractTest { - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - Collection<String> hostNames = null; - - if ("forHostTest".equals(gridName)) - hostNames = Arrays.asList("h_1", "h_2", "h_3"); - - IgniteConfiguration cfg = super.getConfiguration(gridName); - - if (hostNames != null) { - TcpDiscoverySpi disco = (TcpDiscoverySpi)cfg.getDiscoverySpi(); - - cfg.setDiscoverySpi(new CustomHostsTcpDiscoverySpi(hostNames).setIpFinder(disco.getIpFinder())); - } - - return cfg; - } - - /** - * @throws Exception If failed. - */ - public void testForHosts() throws Exception { - Ignite ignite = startGrid("forHostTest"); - - assertEquals(1, ignite.cluster().forHost("h_1").nodes().size()); - assertEquals(1, ignite.cluster().forHost("h_1", "h_3").nodes().size()); - assertEquals(1, ignite.cluster().forHost("unknown_host", "h_2").nodes().size()); - assertEquals(1, ignite.cluster().forHost("h_1", "h_3", "unknown_host", "h_2").nodes().size()); - - assertEquals(0, ignite.cluster().forHost("unknown_host").nodes().size()); - - boolean gotNpe = false; - - try { - assertEquals(0, ignite.cluster().forHost(null, null, null).nodes().size()); - } - catch (NullPointerException e) { - gotNpe = true; - } - - assertTrue(gotNpe); - } - - /** - * Tcp discovery spi that allow to customise hostNames of created local node. - */ - private static class CustomHostsTcpDiscoverySpi extends TcpDiscoverySpi { - /** Hosts. */ - private final Collection<String> hosts; - - /** - * @param hosts Host names which will be retuned by {@link ClusterNode#hostNames()} of created local node. - */ - CustomHostsTcpDiscoverySpi(Collection<String> hosts) { - this.hosts = hosts; - } - - /** - * @param srvPort Server port. - */ - @Override protected void initLocalNode(int srvPort, boolean addExtAddrAttr) { - super.initLocalNode(srvPort, addExtAddrAttr); - - try { - Field hostNamesField = locNode.getClass().getDeclaredField("hostNames"); - - hostNamesField.setAccessible(true); - - hostNamesField.set(locNode, hosts); - } - catch (IllegalAccessException | NoSuchFieldException e) { - U.error(log, "Looks like implementation of " + locNode.getClass() - + " class was changed. Need to update test.", e); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8fd909d9/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupHostsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupHostsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupHostsSelfTest.java new file mode 100644 index 0000000..297a590 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupHostsSelfTest.java @@ -0,0 +1,141 @@ +/* + * 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.ignite.internal; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.testframework.junits.common.*; + +import java.lang.reflect.*; +import java.util.*; + +/** + * Test for {@link ClusterGroup#forHost(String, String...)}. + * + * @see ClusterGroupSelfTest + */ +@GridCommonTest(group = "Kernal Self") +public class ClusterGroupHostsSelfTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + Collection<String> hostNames = Arrays.asList("h_1", "h_2", "h_3"); + + IgniteConfiguration cfg = super.getConfiguration(gridName); + + TcpDiscoverySpi disco = (TcpDiscoverySpi)cfg.getDiscoverySpi(); + + cfg.setDiscoverySpi(new CustomHostsTcpDiscoverySpi(hostNames).setIpFinder(disco.getIpFinder())); + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testForHosts() throws Exception { + Ignite ignite = grid(); + + assertEquals(1, ignite.cluster().forHost("h_1").nodes().size()); + assertEquals(1, ignite.cluster().forHost("h_1", "h_3").nodes().size()); + assertEquals(1, ignite.cluster().forHost("unknown_host", "h_2").nodes().size()); + assertEquals(1, ignite.cluster().forHost("h_1", "h_3", "unknown_host", "h_2").nodes().size()); + + assertEquals(0, ignite.cluster().forHost("unknown_host").nodes().size()); + + boolean gotNpe = false; + + try { + assertEquals(0, ignite.cluster().forHost(null, null, null).nodes().size()); + } + catch (NullPointerException e) { + gotNpe = true; + } + finally { + assertTrue(gotNpe); + } + } + + /** + * @throws Exception If failed. + */ + public void testHostNames() throws Exception { + Ignite ignite = grid(); + + Collection<String> locNodeHosts = ignite.cluster().localNode().hostNames(); + Collection<String> clusterHosts = ignite.cluster().hostNames(); + + assertTrue(F.eqNotOrdered(locNodeHosts, clusterHosts)); + + boolean gotNpe = false; + + try { + clusterHosts.add("valueShouldNotToBeAdded"); + } + catch (UnsupportedOperationException e) { + gotNpe = true; + } + finally { + assertTrue(gotNpe); + } + } + + /** + * Tcp discovery spi that allow to customise hostNames of created local node. + */ + private static class CustomHostsTcpDiscoverySpi extends TcpDiscoverySpi { + /** Hosts. */ + private final Collection<String> hosts; + + /** + * @param hosts Host names which will be retuned by {@link ClusterNode#hostNames()} of created local node. + */ + CustomHostsTcpDiscoverySpi(Collection<String> hosts) { + this.hosts = hosts; + } + + /** + * @param srvPort Server port. + */ + @Override protected void initLocalNode(int srvPort, boolean addExtAddrAttr) { + super.initLocalNode(srvPort, addExtAddrAttr); + + try { + Field hostNamesField = locNode.getClass().getDeclaredField("hostNames"); + + hostNamesField.setAccessible(true); + + hostNamesField.set(locNode, hosts); + } + catch (IllegalAccessException | NoSuchFieldException e) { + U.error(log, "Looks like implementation of " + locNode.getClass() + + " class was changed. Need to update test.", e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8fd909d9/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupSelfTest.java index 37a6e72..ceb9bef 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/ClusterGroupSelfTest.java @@ -248,26 +248,4 @@ public class ClusterGroupSelfTest extends ClusterGroupAbstractTest { return even ? cnt - 1 : cnt - 2; } - - /** - * @throws Exception If failed. - */ - public void testHostNames() throws Exception { - Collection<String> inputHostNames = ignite.cluster().hostNames(); - Collection<String> localNodeHostNames = ignite.cluster().localNode().hostNames(); - Collection<String> randomNodeHostNames = ignite.cluster().forRandom().node().hostNames(); - Collection<ClusterNode> allNodes = ignite.cluster().nodes(); - Collection<String> checkHostNames = new HashSet<String> (); - - for (ClusterNode currentNode : allNodes) - Collections.addAll(checkHostNames, currentNode.hostNames().toArray(new String[0])); - - assert(checkHostNames.equals(inputHostNames)); - - if (!(localNodeHostNames.isEmpty()) && !(inputHostNames.isEmpty())) - assert((inputHostNames.containsAll(localNodeHostNames)) == true); - - if (!(randomNodeHostNames.isEmpty()) && !(inputHostNames.isEmpty())) - assert((inputHostNames.containsAll(randomNodeHostNames)) == true); - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8fd909d9/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java index 6ff83e2..19c1932 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java @@ -65,7 +65,7 @@ public class IgniteBasicTestSuite extends TestSuite { suite.addTest(IgniteStreamSelfTestSuite.suite()); suite.addTest(new TestSuite(GridSelfTest.class)); - suite.addTest(new TestSuite(ClusterForHostsSelfTest.class)); + suite.addTest(new TestSuite(ClusterGroupHostsSelfTest.class)); suite.addTest(new TestSuite(IgniteMessagingWithClientTest.class)); GridTestUtils.addTestIfNeeded(suite, ClusterGroupSelfTest.class, ignoredTests);