diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/helper/EnableableLifeCycle.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/helper/EnableableLifeCycle.java index eb393ce5d851b..aa6113c1c4235 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/helper/EnableableLifeCycle.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/helper/EnableableLifeCycle.java @@ -88,11 +88,11 @@ public void shutdown() shutdown0(); } - public abstract void init0(); + protected abstract void init0(); - public abstract void start0(); + protected abstract void start0(); - public abstract void stop0(); + protected abstract void stop0(); - public abstract void shutdown0(); + protected abstract void shutdown0(); } diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/net/Server.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/net/Server.java index ff8afd0a085f3..5f908319fad08 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/net/Server.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/net/Server.java @@ -76,13 +76,13 @@ public Server( ChildInitializer childInitializer, ListenSocketAddress listenAddr } @Override - public void init0() + protected void init0() { // do nothing } @Override - public void start0() + protected void start0() { if ( channel != null ) { @@ -121,7 +121,7 @@ public void start0() } @Override - public void stop0() + protected void stop0() { if ( channel == null ) { @@ -148,7 +148,7 @@ public void stop0() } @Override - public void shutdown0() + protected void shutdown0() { // do nothing } diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServeLifeCycleStateChangeTest.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCycleStateChangeTest.java similarity index 88% rename from enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServeLifeCycleStateChangeTest.java rename to enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCycleStateChangeTest.java index 7dea836a177fd..a50124e1936e5 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServeLifeCycleStateChangeTest.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCycleStateChangeTest.java @@ -27,16 +27,16 @@ import java.util.ArrayList; import java.util.List; -import org.neo4j.causalclustering.helper.ServerStateTestHelpers.EnableableState; -import org.neo4j.causalclustering.helper.ServerStateTestHelpers.LifeCycleState; +import org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.EnableableState; +import org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.LifeCycleState; import org.neo4j.logging.AssertableLogProvider; import static org.junit.Assert.assertEquals; -import static org.neo4j.causalclustering.helper.ServerStateTestHelpers.setEnableableState; -import static org.neo4j.causalclustering.helper.ServerStateTestHelpers.setInitialState; +import static org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.setEnableableState; +import static org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.setInitialState; @RunWith( Parameterized.class ) -public class ServeLifeCycleStateChangeTest +public class EnableableLifeCycleStateChangeTest { @Parameterized.Parameter() public LifeCycleState fromState; diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerEnableableStateChangeTest.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCylcleEnableableStateChangeTest.java similarity index 89% rename from enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerEnableableStateChangeTest.java rename to enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCylcleEnableableStateChangeTest.java index 684a3d89c31f6..bcb67f77d52a1 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerEnableableStateChangeTest.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifeCylcleEnableableStateChangeTest.java @@ -27,16 +27,16 @@ import java.util.ArrayList; import java.util.List; -import org.neo4j.causalclustering.helper.ServerStateTestHelpers.EnableableState; -import org.neo4j.causalclustering.helper.ServerStateTestHelpers.LifeCycleState; +import org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.EnableableState; +import org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.LifeCycleState; import org.neo4j.logging.NullLogProvider; import static org.junit.Assert.assertEquals; -import static org.neo4j.causalclustering.helper.ServerStateTestHelpers.setEnableableState; -import static org.neo4j.causalclustering.helper.ServerStateTestHelpers.setInitialState; +import static org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.setEnableableState; +import static org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.setInitialState; @RunWith( Parameterized.class ) -public class ServerEnableableStateChangeTest +public class EnableableLifeCylcleEnableableStateChangeTest { @Parameterized.Parameter() public LifeCycleState fromState; diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerStateTestHelpers.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifecycleStateTestHelpers.java similarity index 78% rename from enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerStateTestHelpers.java rename to enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifecycleStateTestHelpers.java index dbea77553e665..86a59b53915cf 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/ServerStateTestHelpers.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/EnableableLifecycleStateTestHelpers.java @@ -19,30 +19,10 @@ */ package org.neo4j.causalclustering.helper; -import org.neo4j.causalclustering.net.Server; -import org.neo4j.helpers.ListenSocketAddress; import org.neo4j.kernel.lifecycle.Lifecycle; -import org.neo4j.logging.FormattedLogProvider; -import org.neo4j.logging.Level; -import org.neo4j.ports.allocation.PortAuthority; -class ServerStateTestHelpers +class EnableableLifecycleStateTestHelpers { - static void teardown( Server server ) - { - server.stop(); - server.shutdown(); - } - - static Server createServer() - { - return new Server( channel -> - { - }, FormattedLogProvider.withDefaultLogLevel( Level.DEBUG ).toOutputStream( System.out ), - FormattedLogProvider.withDefaultLogLevel( Level.DEBUG ).toOutputStream( System.out ), - new ListenSocketAddress( "localhost", PortAuthority.allocatePort() ), "serverName" ); - } - static void setEnableableState( Enableable enableable, EnableableState enableableState ) throws Throwable { switch ( enableableState ) diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/StateAwareEnableableLifeCycle.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/StateAwareEnableableLifeCycle.java index fa3139402b4f5..81e226e92e8b4 100644 --- a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/StateAwareEnableableLifeCycle.java +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/helper/StateAwareEnableableLifeCycle.java @@ -19,7 +19,7 @@ */ package org.neo4j.causalclustering.helper; -import org.neo4j.causalclustering.helper.ServerStateTestHelpers.LifeCycleState; +import org.neo4j.causalclustering.helper.EnableableLifecycleStateTestHelpers.LifeCycleState; import org.neo4j.logging.Log; public class StateAwareEnableableLifeCycle extends EnableableLifeCycle @@ -32,25 +32,25 @@ public class StateAwareEnableableLifeCycle extends EnableableLifeCycle } @Override - public void start0() + protected void start0() { status = LifeCycleState.Start; } @Override - public void stop0() + protected void stop0() { status = LifeCycleState.Stop; } @Override - public void shutdown0() + protected void shutdown0() { status = LifeCycleState.Shutdown; } @Override - public void init0() + protected void init0() { status = LifeCycleState.Init; } diff --git a/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/net/ServerStateTest.java b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/net/ServerStateTest.java new file mode 100644 index 0000000000000..56f2d4803bf60 --- /dev/null +++ b/enterprise/causal-clustering/src/test/java/org/neo4j/causalclustering/net/ServerStateTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2002-2018 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.neo4j.causalclustering.net; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.neo4j.causalclustering.helper.EnableableLifeCycleStateChangeTest; +import org.neo4j.causalclustering.helper.EnableableLifeCylcleEnableableStateChangeTest; +import org.neo4j.helpers.ListenSocketAddress; +import org.neo4j.logging.FormattedLogProvider; +import org.neo4j.logging.Level; +import org.neo4j.ports.allocation.PortAuthority; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * More generalized state tests of EnableableLifeCycle can be found {@link EnableableLifeCycleStateChangeTest} and + * {@link EnableableLifeCylcleEnableableStateChangeTest} + */ +public class ServerStateTest +{ + private Server server; + private final EventLoopGroup clientGroup = new NioEventLoopGroup(); + + @Before + public void setUp() throws InterruptedException + { + server = createServer(); + server.init(); + assertFalse( canConnect( server.address(), clientGroup ) ); + } + + @After + public void tearDown() + { + server.stop(); + server.shutdown(); + } + + @Test + public void shouldStartServerNormally() throws InterruptedException + { + server.start(); + assertTrue( canConnect( server.address(), clientGroup ) ); + } + + @Test + public void canDisableAndEnableServer() throws InterruptedException + { + server.start(); + assertTrue( canConnect( server.address(), clientGroup ) ); + + server.disable(); + assertFalse( canConnect( server.address(), clientGroup ) ); + + server.enable(); + assertTrue( canConnect( server.address(), clientGroup ) ); + } + + @Test + public void serverCannotBeEnabledIfLifeCycleHasNotStarted() throws InterruptedException + { + server.enable(); + assertFalse( canConnect( server.address(), clientGroup ) ); + + server.start(); + assertTrue( canConnect( server.address(), clientGroup ) ); + } + + @Test + public void serverCannotStartIfDisabled() throws InterruptedException + { + server.disable(); + + server.start(); + assertFalse( canConnect( server.address(), clientGroup ) ); + + server.enable(); + assertTrue( canConnect( server.address(), clientGroup ) ); + } + + private static Server createServer() + { + return new Server( channel -> + { + }, FormattedLogProvider.withDefaultLogLevel( Level.DEBUG ).toOutputStream( System.out ), + FormattedLogProvider.withDefaultLogLevel( Level.DEBUG ).toOutputStream( System.out ), + new ListenSocketAddress( "localhost", PortAuthority.allocatePort() ), "serverName" ); + } + + private static boolean canConnect( ListenSocketAddress socketAddress, EventLoopGroup eventExecutors ) throws InterruptedException + { + return new Bootstrap().group( eventExecutors ).channel( NioSocketChannel.class ).handler( new ChannelInitializer() + { + @Override + protected void initChannel( NioSocketChannel ch ) + { + + } + } ).connect( socketAddress.getHostname(), socketAddress.getPort() ).await().isSuccess(); + } +}