Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite6a4200

Browse files
committed
Work oniluwatar#74, increased coverage
1 parent2ff7818 commite6a4200

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
packagecom.iluwatar.reactor.app;
22

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

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

6669
privateNioReactorreactor;
70+
privateList<AbstractNioChannel>channels =newArrayList<>();
6771

6872
/**
6973
* App entry.
7074
*
7175
* @throws IOException
7276
*/
7377
publicstaticvoidmain(String[]args)throwsIOException {
74-
newApp().start();
78+
newApp().start(newThreadPoolDispatcher(2));
7579
}
7680

7781
/**
7882
* Starts the NIO reactor.
83+
* @param threadPoolDispatcher
7984
*
8085
* @throws IOException if any channel fails to bind.
8186
*/
82-
publicvoidstart()throwsIOException {
87+
publicvoidstart(Dispatcherdispatcher)throwsIOException {
8388
/*
8489
* The application can customize its event dispatching mechanism.
8590
*/
86-
reactor =newNioReactor(newThreadPoolDispatcher(2));
91+
reactor =newNioReactor(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-
publicvoidstop()throwsInterruptedException {
113+
publicvoidstop()throwsInterruptedException,IOException {
108114
reactor.stop();
115+
for (AbstractNioChannelchannel :channels) {
116+
channel.getChannel().close();
117+
}
109118
}
110119

111-
privatestaticAbstractNioChanneltcpChannel(intport,ChannelHandlerhandler)throwsIOException {
120+
privateAbstractNioChanneltcpChannel(intport,ChannelHandlerhandler)throwsIOException {
112121
NioServerSocketChannelchannel =newNioServerSocketChannel(port,handler);
113122
channel.bind();
123+
channels.add(channel);
114124
returnchannel;
115125
}
116126

117-
privatestaticAbstractNioChanneludpChannel(intport,ChannelHandlerhandler)throwsIOException {
127+
privateAbstractNioChanneludpChannel(intport,ChannelHandlerhandler)throwsIOException {
118128
NioDatagramChannelchannel =newNioDatagramChannel(port,handler);
119129
channel.bind();
130+
channels.add(channel);
120131
returnchannel;
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
importorg.junit.Test;
66

7+
importcom.iluwatar.reactor.framework.SameThreadDispatcher;
8+
importcom.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
publicclassAppTest {
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+
publicvoidtestAppUsingThreadPoolDispatcher()throwsIOException,InterruptedException {
27+
Appapp =newApp();
28+
app.start(newThreadPoolDispatcher(2));
29+
30+
AppClientclient =newAppClient();
31+
client.start();
32+
33+
// allow clients to send requests. Artificial delay.
34+
try {
35+
Thread.sleep(2000);
36+
}catch (InterruptedExceptione) {
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-
publicvoidtestApp()throwsIOException,InterruptedException {
52+
publicvoidtestAppUsingSameThreadDispatcher()throwsIOException,InterruptedException {
2453
Appapp =newApp();
25-
app.start();
54+
app.start(newSameThreadDispatcher());
2655

2756
AppClientclient =newAppClient();
2857
client.start();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp