-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathThreadPerSessionEventHandlingStrategyTest.java
More file actions
399 lines (339 loc) · 16.2 KB
/
ThreadPerSessionEventHandlingStrategyTest.java
File metadata and controls
399 lines (339 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact ask@quickfixengine.org if any conditions of this licensing
* are not clear to you.
******************************************************************************/
package quickfix.mina;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import quickfix.ConfigError;
import quickfix.DefaultSessionFactory;
import quickfix.FieldNotFound;
import quickfix.FixVersions;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.MemoryStoreFactory;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.Responder;
import quickfix.SLF4JLogFactory;
import quickfix.Session;
import quickfix.SessionFactory;
import quickfix.SessionID;
import quickfix.SessionSettings;
import quickfix.UnitTestApplication;
import quickfix.field.HeartBtInt;
import quickfix.field.MsgSeqNum;
import quickfix.field.SenderCompID;
import quickfix.field.SendingTime;
import quickfix.field.TargetCompID;
import quickfix.field.converter.UtcTimestampConverter;
import quickfix.fix40.Logon;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static quickfix.test.util.ReflectionUtil.getField;
public class ThreadPerSessionEventHandlingStrategyTest {
private final static class ThreadPerSessionEventHandlingStrategyUnderTest extends
ThreadPerSessionEventHandlingStrategy {
public ThreadPerSessionEventHandlingStrategyUnderTest() {
super(null, SessionConnector.DEFAULT_QUEUE_CAPACITY);
}
public Exception getNextMessageException;
public int getMessageCount = 1;
@Override
protected void startDispatcherThread(
ThreadPerSessionEventHandlingStrategy.MessageDispatchingThread dispatcher) {
}
@Override
protected Message getNextMessage(QueueTracker<Message> queueTracker) throws InterruptedException {
if (getMessageCount-- == 0) {
throw new InterruptedException("END COUNT");
}
if (getNextMessageException != null) {
if (getNextMessageException instanceof InterruptedException) {
throw (InterruptedException) getNextMessageException;
}
throw (RuntimeException) getNextMessageException;
}
return super.getNextMessage(queueTracker);
}
}
private ThreadPerSessionEventHandlingStrategy strategy;
@Before
public void init() {
strategy = new ThreadPerSessionEventHandlingStrategy(
null, SessionConnector.DEFAULT_QUEUE_CAPACITY);
}
@After
public void cleanup() {
strategy.stopDispatcherThreads();
strategy = null;
}
/**
* Verifies that messages are dispatched via dispatcher threads and that the threads die when
* the strategy is shut down. See QFJ-410.
*
* @throws Exception
*/
@Test
public void testEventHandling() throws Exception {
final SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
final CountDownLatch latch = new CountDownLatch(1);
final UnitTestApplication application = new UnitTestApplication() {
@Override
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
super.fromAdmin(message, sessionId);
latch.countDown();
}
};
try (Session session = setUpSession(sessionID, application)) {
final Message message = new Logon();
message.getHeader().setString(SenderCompID.FIELD, "ISLD");
message.getHeader().setString(TargetCompID.FIELD, "TW");
message.getHeader().setString(SendingTime.FIELD,
UtcTimestampConverter.convert(new Date(), false));
message.getHeader().setInt(MsgSeqNum.FIELD, 1);
message.setInt(HeartBtInt.FIELD, 30);
strategy.onMessage(session, message);
// Wait for a received message
if (!latch.await(5, TimeUnit.SECONDS)) {
fail("Timeout");
}
assertEquals(1, application.fromAdminMessages.size());
final Thread[] threads = new Thread[1024];
Thread.enumerate(threads);
Thread dispatcherThread = null;
for (final Thread thread : threads) {
if (thread.getName().startsWith("QF/J Session dispatcher")) {
dispatcherThread = thread;
// Dispatcher threads are not daemon threads
assertThat(dispatcherThread.isDaemon(), is(false));
break;
}
}
// We should have found the dispatcher thread
assertThat(dispatcherThread, notNullValue());
// Stop the threads and then check the thread state
strategy.stopDispatcherThreads();
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
if (!dispatcherThread.isAlive()) {
break;
}
}
// Dispatcher thread should be dead
assertThat(dispatcherThread.isAlive(), is(false));
assertNull(strategy.getDispatcher(sessionID));
}
}
/**
* See QFJ-686. Verify that thread is stopped if Session has no responder.
*/
@Test
public void testEventHandlingOnDisconnect() throws Exception {
final SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
final CountDownLatch latch = new CountDownLatch(1);
final UnitTestApplication application = new UnitTestApplication() {
@Override
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
super.fromAdmin(message, sessionId);
latch.countDown();
}
};
try (Session session = setUpSession(sessionID, application)) {
final Message message = new Logon();
message.getHeader().setString(SenderCompID.FIELD, "ISLD");
message.getHeader().setString(TargetCompID.FIELD, "TW");
message.getHeader().setString(SendingTime.FIELD,
UtcTimestampConverter.convert(new Date(), false));
message.getHeader().setInt(MsgSeqNum.FIELD, 1);
message.setInt(HeartBtInt.FIELD, 30);
strategy.onMessage(session, message);
// Wait for a received message
if (!latch.await(5, TimeUnit.SECONDS)) {
fail("Timeout");
}
assertEquals(1, application.fromAdminMessages.size());
Thread[] threads = new Thread[1024];
Thread.enumerate(threads);
Thread dispatcherThread = null;
for (final Thread thread : threads) {
if (thread != null && thread.getName().startsWith("QF/J Session dispatcher")) {
dispatcherThread = thread;
// Dispatcher threads are not daemon threads
assertThat(dispatcherThread.isDaemon(), is(false));
break;
}
}
assertTrue(session.hasResponder());
// QFJ-790: we do not check the state of the responder anymore
// but wait for the END_OF_STREAM message to stop the threads.
strategy.onMessage(session, EventHandlingStrategy.END_OF_STREAM);
// sleep some time to let the thread stop
for (int i = 0; i < 20; i++) {
Thread.sleep(100);
if (!dispatcherThread.isAlive()) {
break;
}
}
assertNull(strategy.getDispatcher(sessionID));
threads = new Thread[1024];
Thread.enumerate(threads);
dispatcherThread = null;
for (final Thread thread : threads) {
if (thread != null && thread.getName().startsWith("QF/J Session dispatcher")) {
dispatcherThread = thread;
// Dispatcher threads are not daemon threads
assertThat(dispatcherThread.isDaemon(), is(false));
break;
}
}
// the session dispatcher should be dead and hence not listed in the threads array
assertNull(dispatcherThread);
assertFalse(session.hasResponder());
}
}
@Test
public void testEventHandlingInterruptInRun() throws Exception {
final SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
try (Session session = setUpSession(sessionID)) {
final Message message = new Logon();
message.setInt(HeartBtInt.FIELD, 30);
final ThreadPerSessionEventHandlingStrategyUnderTest strategy = new ThreadPerSessionEventHandlingStrategyUnderTest();
strategy.onMessage(session, message);
strategy.getNextMessageException = new InterruptedException("TEST");
strategy.getDispatcher(sessionID).run();
}
}
@Test
public void testEventHandlingRuntimeException() throws Exception {
final SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
try (Session session = setUpSession(sessionID)) {
final Message message = new Logon();
message.setInt(HeartBtInt.FIELD, 30);
final ThreadPerSessionEventHandlingStrategyUnderTest strategy = new ThreadPerSessionEventHandlingStrategyUnderTest();
strategy.onMessage(session, message);
strategy.getNextMessageException = new NullPointerException("TEST");
strategy.getDispatcher(sessionID).run();
}
}
// verify the assumption that this always returns null
@Test
public void testVerifyGetConnectorAssumption() throws Exception {
final ThreadPerSessionEventHandlingStrategyUnderTest strategy = new ThreadPerSessionEventHandlingStrategyUnderTest();
assertNull(strategy.getSessionConnector());
}
@Test
public void shouldCreateCorrectTypeOfQueueTracker() throws Exception {
final Session quickfixSession = mock(Session.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS));
assertFalse(getField(
new ThreadPerSessionEventHandlingStrategy(null, 42)
.createDispatcherThread(quickfixSession),
"queueTracker",
QueueTracker.class) instanceof WatermarkTracker);
assertTrue(getField(
new ThreadPerSessionEventHandlingStrategy(null, 42, 43)
.createDispatcherThread(quickfixSession),
"queueTracker",
QueueTracker.class) instanceof WatermarkTracker);
assertFalse(getField(
new ThreadPerSessionEventHandlingStrategy(null, -1, -1)
.createDispatcherThread(quickfixSession),
"queueTracker",
QueueTracker.class) instanceof WatermarkTracker);
}
/**
* Regression test for GH-1128: stopDispatcherThreads() must block until each dispatcher
* thread has fully finished processing, not return as soon as stop has been requested.
*/
@Test
public void testStopDispatcherThreadsIsBlocking() throws Exception {
final SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIX40, "TW", "ISLD");
final CountDownLatch slowProcessingStarted = new CountDownLatch(1);
final AtomicInteger messagesProcessed = new AtomicInteger(0);
final UnitTestApplication application = new UnitTestApplication() {
@Override
public void fromAdmin(Message message, SessionID sessionId) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
super.fromAdmin(message, sessionId);
messagesProcessed.incrementAndGet();
slowProcessingStarted.countDown();
try {
// Simulate slow message processing so the queue is not drained before stop()
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
try (Session session = setUpSession(sessionID, application)) {
final Message message = new Logon();
message.getHeader().setString(SenderCompID.FIELD, "ISLD");
message.getHeader().setString(TargetCompID.FIELD, "TW");
message.getHeader().setString(SendingTime.FIELD,
UtcTimestampConverter.convert(new Date(), false));
message.getHeader().setInt(MsgSeqNum.FIELD, 1);
message.setInt(HeartBtInt.FIELD, 30);
strategy.onMessage(session, message);
// Wait for the dispatcher to start processing (and be inside the slow handler)
assertTrue("Slow processing should have started", slowProcessingStarted.await(5, TimeUnit.SECONDS));
// Find the dispatcher thread before stopping
final Thread[] threads = new Thread[1024];
Thread.enumerate(threads);
Thread dispatcherThread = null;
for (final Thread thread : threads) {
if (thread != null && thread.getName().startsWith("QF/J Session dispatcher")) {
dispatcherThread = thread;
break;
}
}
assertNotNull("Dispatcher thread should exist", dispatcherThread);
// stopDispatcherThreads() must block until the dispatcher has fully terminated
strategy.stopDispatcherThreads();
// Immediately after stop returns the dispatcher thread must be dead (no polling needed)
assertFalse("Dispatcher thread must be dead after stopDispatcherThreads() returns",
dispatcherThread.isAlive());
assertNull(strategy.getDispatcher(sessionID));
}
}
private Session setUpSession(SessionID sessionID) throws ConfigError {
final UnitTestApplication application = new UnitTestApplication();
return setUpSession(sessionID, application);
}
private Session setUpSession(SessionID sessionID, UnitTestApplication application)
throws ConfigError {
final DefaultSessionFactory sessionFactory = new DefaultSessionFactory(application,
new MemoryStoreFactory(), new SLF4JLogFactory(new SessionSettings()));
final SessionSettings settings = new SessionSettings();
settings.setString(SessionFactory.SETTING_CONNECTION_TYPE,
SessionFactory.ACCEPTOR_CONNECTION_TYPE);
settings.setString(Session.SETTING_USE_DATA_DICTIONARY, "N");
settings.setString(Session.SETTING_START_TIME, "00:00:00");
settings.setString(Session.SETTING_END_TIME, "00:00:00");
final Session session = sessionFactory.create(sessionID, settings);
session.setResponder(mock(Responder.class));
return session;
}
}