33import org .fluentd .logger .sender .Event ;
44import org .fluentd .logger .sender .NullSender ;
55import org .fluentd .logger .util .MockFluentd ;
6+ import org .junit .Ignore ;
67import org .junit .Test ;
78import org .msgpack .MessagePack ;
89import org .msgpack .unpacker .Unpacker ;
10+ import org .slf4j .Logger ;
11+ import org .slf4j .LoggerFactory ;
912
1013import java .io .BufferedInputStream ;
1114import java .io .EOFException ;
1215import java .io .IOException ;
1316import java .net .Socket ;
1417import java .util .*;
15- import java .util .concurrent .Executor ;
1618import java .util .concurrent .ExecutorService ;
1719import java .util .concurrent .Executors ;
1820import java .util .concurrent .TimeUnit ;
1921
2022import static org .junit .Assert .assertEquals ;
2123
2224public class TestFluentLogger {
25+ private Logger _logger = LoggerFactory .getLogger (TestFluentLogger .class );
26+
27+ class FixedThreadManager {
28+ private final ExecutorService service ;
29+
30+ public FixedThreadManager (int numThreads ) {
31+ service = Executors .newFixedThreadPool (numThreads );
32+ }
33+
34+ public void submit (Runnable r ) {
35+ service .submit (r );
36+ }
37+
38+ public void join () throws InterruptedException {
39+ service .shutdown ();
40+ while (!service .awaitTermination (1000 , TimeUnit .MILLISECONDS )) {
41+ _logger .debug ("waiting ..." );
42+ }
43+ _logger .trace ("Terminating FixedThreadManager" );
44+ }
45+ }
46+
2347
2448 @ Test
2549 public void testNormal01 () throws Exception {
@@ -42,7 +66,9 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
4266 }
4367 }
4468 });
45- fluentd .start ();
69+
70+ FixedThreadManager threadManager = new FixedThreadManager (1 );
71+ threadManager .submit (fluentd );
4672
4773 // start loggers
4874 FluentLogger logger = FluentLogger .getLogger ("testtag" , host , port );
@@ -66,14 +92,15 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
6692 fluentd .close ();
6793
6894 // wait for unpacking event data on fluentd
69- Thread . sleep ( 1000 );
95+ threadManager . join ( );
7096
7197 // check data
7298 assertEquals (2 , elist .size ());
7399 assertEquals ("testtag.test01" , elist .get (0 ).tag );
74100 assertEquals ("testtag.test01" , elist .get (1 ).tag );
75101 }
76102
103+ @ SuppressWarnings ("unchecked" )
77104 @ Test
78105 public void testNormal02 () throws Exception {
79106 int loggerCount = 3 ;
@@ -106,8 +133,8 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
106133 }
107134 }
108135 });
109- ExecutorService executor = Executors . newFixedThreadPool (1 );
110- executor .submit (fluentd );
136+ FixedThreadManager threadManager = new FixedThreadManager (1 );
137+ threadManager .submit (fluentd );
111138
112139 // start loggers
113140 FluentLogger [] loggers = new FluentLogger [loggerCount ];
@@ -147,8 +174,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
147174 fluentd .close ();
148175
149176 // wait for unpacking event data on fluentd
150- while (executor .awaitTermination (1000 , TimeUnit .MILLISECONDS )) {
151- }
177+ threadManager .join ();
152178
153179 // check data
154180 assertEquals (counts [0 ], elists [0 ].size ());
@@ -174,6 +200,9 @@ public void testReconnection() throws Exception {
174200 int port = MockFluentd .randomPort ();
175201 String host = "localhost" ;
176202 final List <Event > elist1 = new ArrayList <Event >();
203+
204+ FixedThreadManager threadManager = new FixedThreadManager (2 );
205+
177206 MockFluentd fluentd1 = new MockFluentd (port , new MockFluentd .MockProcess () {
178207 public void process (MessagePack msgpack , Socket socket ) throws IOException {
179208 BufferedInputStream in = new BufferedInputStream (socket .getInputStream ());
@@ -192,7 +221,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
192221 }
193222 }
194223 });
195- fluentd1 . start ( );
224+ threadManager . submit ( fluentd1 );
196225
197226 // start loggers
198227 FluentLogger logger = FluentLogger .getLogger ("testtag" , host , port );
@@ -204,6 +233,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
204233 }
205234
206235 TimeUnit .MILLISECONDS .sleep (500 );
236+ _logger .info ("Closing the current fluentd instance" );
207237 fluentd1 .closeClientSockets ();
208238 fluentd1 .close ();
209239
@@ -232,7 +262,7 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
232262 }
233263 }
234264 });
235- fluentd2 . start ( );
265+ threadManager . submit ( fluentd2 );
236266
237267 TimeUnit .MILLISECONDS .sleep (500 );
238268
@@ -245,11 +275,15 @@ public void process(MessagePack msgpack, Socket socket) throws IOException {
245275
246276 // close loggers
247277 FluentLogger .closeAll ();
278+ Thread .sleep (2000 );
248279
249280 fluentd2 .close ();
250281
282+
251283 // wait for unpacking event data on fluentd
252- TimeUnit .MILLISECONDS .sleep (500 );
284+ TimeUnit .MILLISECONDS .sleep (2000 );
285+ threadManager .join ();
286+
253287
254288 // check data
255289 assertEquals (1 , elist1 .size ());
0 commit comments