Skip to content

Commit e6a4200

Browse files
committed
Work on iluwatar#74, increased coverage
1 parent 2ff7818 commit e6a4200

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

reactor/src/main/java/com/iluwatar/reactor/app/App.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.iluwatar.reactor.app;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import com.iluwatar.reactor.framework.AbstractNioChannel;
68
import com.iluwatar.reactor.framework.ChannelHandler;
9+
import com.iluwatar.reactor.framework.Dispatcher;
710
import com.iluwatar.reactor.framework.NioDatagramChannel;
811
import com.iluwatar.reactor.framework.NioReactor;
912
import com.iluwatar.reactor.framework.NioServerSocketChannel;
@@ -64,26 +67,28 @@
6467
public class App {
6568

6669
private NioReactor reactor;
70+
private List<AbstractNioChannel> channels = new ArrayList<>();
6771

6872
/**
6973
* App entry.
7074
*
7175
* @throws IOException
7276
*/
7377
public static void main(String[] args) throws IOException {
74-
new App().start();
78+
new App().start(new ThreadPoolDispatcher(2));
7579
}
7680

7781
/**
7882
* Starts the NIO reactor.
83+
* @param threadPoolDispatcher
7984
*
8085
* @throws IOException if any channel fails to bind.
8186
*/
82-
public void start() throws IOException {
87+
public void start(Dispatcher dispatcher) throws IOException {
8388
/*
8489
* The application can customize its event dispatching mechanism.
8590
*/
86-
reactor = new NioReactor(new ThreadPoolDispatcher(2));
91+
reactor = new NioReactor(dispatcher);
8792

8893
/*
8994
* This represents application specific business logic that dispatcher will call on appropriate
@@ -103,20 +108,26 @@ public void start() throws IOException {
103108
* Stops the NIO reactor. This is a blocking call.
104109
*
105110
* @throws InterruptedException if interrupted while stopping the reactor.
111+
* @throws IOException if any I/O error occurs
106112
*/
107-
public void stop() throws InterruptedException {
113+
public void stop() throws InterruptedException, IOException {
108114
reactor.stop();
115+
for (AbstractNioChannel channel : channels) {
116+
channel.getChannel().close();
117+
}
109118
}
110119

111-
private static AbstractNioChannel tcpChannel(int port, ChannelHandler handler) throws IOException {
120+
private AbstractNioChannel tcpChannel(int port, ChannelHandler handler) throws IOException {
112121
NioServerSocketChannel channel = new NioServerSocketChannel(port, handler);
113122
channel.bind();
123+
channels.add(channel);
114124
return channel;
115125
}
116126

117-
private static AbstractNioChannel udpChannel(int port, ChannelHandler handler) throws IOException {
127+
private AbstractNioChannel udpChannel(int port, ChannelHandler handler) throws IOException {
118128
NioDatagramChannel channel = new NioDatagramChannel(port, handler);
119129
channel.bind();
130+
channels.add(channel);
120131
return channel;
121132
}
122133
}

reactor/src/test/java/com/iluwatar/reactor/app/AppTest.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import org.junit.Test;
66

7+
import com.iluwatar.reactor.framework.SameThreadDispatcher;
8+
import com.iluwatar.reactor.framework.ThreadPoolDispatcher;
9+
710
/**
811
*
912
* This class tests the Distributed Logging service by starting a Reactor and then sending it
@@ -14,15 +17,41 @@
1417
public class AppTest {
1518

1619
/**
17-
* Test the application.
20+
* Test the application using pooled thread dispatcher.
21+
*
22+
* @throws IOException if any I/O error occurs.
23+
* @throws InterruptedException if interrupted while stopping the application.
24+
*/
25+
@Test
26+
public void testAppUsingThreadPoolDispatcher() throws IOException, InterruptedException {
27+
App app = new App();
28+
app.start(new ThreadPoolDispatcher(2));
29+
30+
AppClient client = new AppClient();
31+
client.start();
32+
33+
// allow clients to send requests. Artificial delay.
34+
try {
35+
Thread.sleep(2000);
36+
} catch (InterruptedException e) {
37+
e.printStackTrace();
38+
}
39+
40+
client.stop();
41+
42+
app.stop();
43+
}
44+
45+
/**
46+
* Test the application using same thread dispatcher.
1847
*
1948
* @throws IOException if any I/O error occurs.
2049
* @throws InterruptedException if interrupted while stopping the application.
2150
*/
2251
@Test
23-
public void testApp() throws IOException, InterruptedException {
52+
public void testAppUsingSameThreadDispatcher() throws IOException, InterruptedException {
2453
App app = new App();
25-
app.start();
54+
app.start(new SameThreadDispatcher());
2655

2756
AppClient client = new AppClient();
2857
client.start();

0 commit comments

Comments
 (0)