Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarW committed Apr 20, 2018
1 parent bf561e7 commit a8636ed
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 44 deletions.
Expand Up @@ -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();
}
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -121,7 +121,7 @@ public void start0()
}

@Override
public void stop0()
protected void stop0()
{
if ( channel == null )
{
Expand All @@ -148,7 +148,7 @@ public void stop0()
}

@Override
public void shutdown0()
protected void shutdown0()
{
// do nothing
}
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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 )
Expand Down
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<NioSocketChannel>()
{
@Override
protected void initChannel( NioSocketChannel ch )
{

}
} ).connect( socketAddress.getHostname(), socketAddress.getPort() ).await().isSuccess();
}
}

0 comments on commit a8636ed

Please sign in to comment.