Work on #74, increased coverage

This commit is contained in:
Narendra Pathai 2015-09-15 13:48:58 +05:30
parent 2ff78184e5
commit e6a4200607
2 changed files with 49 additions and 9 deletions

View File

@ -1,9 +1,12 @@
package com.iluwatar.reactor.app; package com.iluwatar.reactor.app;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.iluwatar.reactor.framework.AbstractNioChannel; import com.iluwatar.reactor.framework.AbstractNioChannel;
import com.iluwatar.reactor.framework.ChannelHandler; import com.iluwatar.reactor.framework.ChannelHandler;
import com.iluwatar.reactor.framework.Dispatcher;
import com.iluwatar.reactor.framework.NioDatagramChannel; import com.iluwatar.reactor.framework.NioDatagramChannel;
import com.iluwatar.reactor.framework.NioReactor; import com.iluwatar.reactor.framework.NioReactor;
import com.iluwatar.reactor.framework.NioServerSocketChannel; import com.iluwatar.reactor.framework.NioServerSocketChannel;
@ -64,6 +67,7 @@ import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
public class App { public class App {
private NioReactor reactor; private NioReactor reactor;
private List<AbstractNioChannel> channels = new ArrayList<>();
/** /**
* App entry. * App entry.
@ -71,19 +75,20 @@ public class App {
* @throws IOException * @throws IOException
*/ */
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
new App().start(); new App().start(new ThreadPoolDispatcher(2));
} }
/** /**
* Starts the NIO reactor. * Starts the NIO reactor.
* @param threadPoolDispatcher
* *
* @throws IOException if any channel fails to bind. * @throws IOException if any channel fails to bind.
*/ */
public void start() throws IOException { public void start(Dispatcher dispatcher) throws IOException {
/* /*
* The application can customize its event dispatching mechanism. * The application can customize its event dispatching mechanism.
*/ */
reactor = new NioReactor(new ThreadPoolDispatcher(2)); reactor = new NioReactor(dispatcher);
/* /*
* This represents application specific business logic that dispatcher will call on appropriate * This represents application specific business logic that dispatcher will call on appropriate
@ -103,20 +108,26 @@ public class App {
* Stops the NIO reactor. This is a blocking call. * Stops the NIO reactor. This is a blocking call.
* *
* @throws InterruptedException if interrupted while stopping the reactor. * @throws InterruptedException if interrupted while stopping the reactor.
* @throws IOException if any I/O error occurs
*/ */
public void stop() throws InterruptedException { public void stop() throws InterruptedException, IOException {
reactor.stop(); reactor.stop();
for (AbstractNioChannel channel : channels) {
channel.getChannel().close();
}
} }
private static AbstractNioChannel tcpChannel(int port, ChannelHandler handler) throws IOException { private AbstractNioChannel tcpChannel(int port, ChannelHandler handler) throws IOException {
NioServerSocketChannel channel = new NioServerSocketChannel(port, handler); NioServerSocketChannel channel = new NioServerSocketChannel(port, handler);
channel.bind(); channel.bind();
channels.add(channel);
return channel; return channel;
} }
private static AbstractNioChannel udpChannel(int port, ChannelHandler handler) throws IOException { private AbstractNioChannel udpChannel(int port, ChannelHandler handler) throws IOException {
NioDatagramChannel channel = new NioDatagramChannel(port, handler); NioDatagramChannel channel = new NioDatagramChannel(port, handler);
channel.bind(); channel.bind();
channels.add(channel);
return channel; return channel;
} }
} }

View File

@ -4,6 +4,9 @@ import java.io.IOException;
import org.junit.Test; import org.junit.Test;
import com.iluwatar.reactor.framework.SameThreadDispatcher;
import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
/** /**
* *
* This class tests the Distributed Logging service by starting a Reactor and then sending it * This class tests the Distributed Logging service by starting a Reactor and then sending it
@ -14,15 +17,41 @@ import org.junit.Test;
public class AppTest { public class AppTest {
/** /**
* Test the application. * Test the application using pooled thread dispatcher.
* *
* @throws IOException if any I/O error occurs. * @throws IOException if any I/O error occurs.
* @throws InterruptedException if interrupted while stopping the application. * @throws InterruptedException if interrupted while stopping the application.
*/ */
@Test @Test
public void testApp() throws IOException, InterruptedException { public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
App app = new App(); App app = new App();
app.start(); app.start(new ThreadPoolDispatcher(2));
AppClient client = new AppClient();
client.start();
// allow clients to send requests. Artificial delay.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
client.stop();
app.stop();
}
/**
* Test the application using same thread dispatcher.
*
* @throws IOException if any I/O error occurs.
* @throws InterruptedException if interrupted while stopping the application.
*/
@Test
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
App app = new App();
app.start(new SameThreadDispatcher());
AppClient client = new AppClient(); AppClient client = new AppClient();
client.start(); client.start();