blob: f79928d86b5b7cd3decbea7cdb4b9bb3e54bac3e [file] [log] [blame]
/*
* 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.client;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.ClientConfiguration;
import org.apache.ignite.configuration.ClientConnectorConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.spi.IgniteSpiException;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
/**
* Checks that connection with starting node will be established correctly.
*/
public class ConnectToStartingNodeTest extends GridCommonAbstractTest {
/** Client connector address. */
private static final String CLIENT_CONN_ADDR = "127.0.0.1:" + ClientConnectorConfiguration.DFLT_PORT;
/** Barrier to suspend discovery SPI start. */
private final CyclicBarrier barrier = new CyclicBarrier(2);
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
return super.getConfiguration(igniteInstanceName).setDiscoverySpi(new TcpDiscoverySpi() {
@Override public void spiStart(@Nullable String igniteInstanceName) throws IgniteSpiException {
try {
barrier.await();
}
catch (Exception ignore) {
// No-op.
}
super.spiStart(igniteInstanceName);
try {
barrier.await();
}
catch (Exception ignore) {
// No-op.
}
}
});
}
/**
* Test that client can't connect to server before discovery SPI start.
*/
@Test
public void testClientConnectBeforeDiscoveryStart() throws Exception {
IgniteInternalFuture<Ignite> futStartGrid = GridTestUtils.runAsync((Callable<Ignite>)this::startGrid);
barrier.await();
IgniteInternalFuture<IgniteClient> futStartClient = GridTestUtils.runAsync(
() -> Ignition.startClient(new ClientConfiguration().setAddresses(CLIENT_CONN_ADDR)));
// Server doesn't accept connection before discovery SPI started.
assertFalse(GridTestUtils.waitForCondition(futStartClient::isDone, 500L));
barrier.await();
futStartGrid.get();
// Server accept connection after discovery SPI started.
assertTrue(GridTestUtils.waitForCondition(futStartClient::isDone, 500L));
}
}