From e6a4200607614e25aa2457589cc0f65e390c24f3 Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Tue, 15 Sep 2015 13:48:58 +0530 Subject: [PATCH] Work on #74, increased coverage --- .../java/com/iluwatar/reactor/app/App.java | 23 ++++++++---- .../com/iluwatar/reactor/app/AppTest.java | 35 +++++++++++++++++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/reactor/src/main/java/com/iluwatar/reactor/app/App.java b/reactor/src/main/java/com/iluwatar/reactor/app/App.java index fcc327b34..975435712 100644 --- a/reactor/src/main/java/com/iluwatar/reactor/app/App.java +++ b/reactor/src/main/java/com/iluwatar/reactor/app/App.java @@ -1,9 +1,12 @@ package com.iluwatar.reactor.app; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import com.iluwatar.reactor.framework.AbstractNioChannel; import com.iluwatar.reactor.framework.ChannelHandler; +import com.iluwatar.reactor.framework.Dispatcher; import com.iluwatar.reactor.framework.NioDatagramChannel; import com.iluwatar.reactor.framework.NioReactor; import com.iluwatar.reactor.framework.NioServerSocketChannel; @@ -64,6 +67,7 @@ import com.iluwatar.reactor.framework.ThreadPoolDispatcher; public class App { private NioReactor reactor; + private List channels = new ArrayList<>(); /** * App entry. @@ -71,19 +75,20 @@ public class App { * @throws IOException */ public static void main(String[] args) throws IOException { - new App().start(); + new App().start(new ThreadPoolDispatcher(2)); } /** * Starts the NIO reactor. + * @param threadPoolDispatcher * * @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. */ - reactor = new NioReactor(new ThreadPoolDispatcher(2)); + reactor = new NioReactor(dispatcher); /* * 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. * * @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(); + 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); channel.bind(); + channels.add(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); channel.bind(); + channels.add(channel); return channel; } } diff --git a/reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java b/reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java index bc51e26de..2ac9b448a 100644 --- a/reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java +++ b/reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java @@ -4,6 +4,9 @@ import java.io.IOException; 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 @@ -14,15 +17,41 @@ import org.junit.Test; public class AppTest { /** - * Test the application. + * Test the application using pooled thread dispatcher. * * @throws IOException if any I/O error occurs. * @throws InterruptedException if interrupted while stopping the application. */ @Test - public void testApp() throws IOException, InterruptedException { + public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException { 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(); client.start();