-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathSessionTest.java
More file actions
3386 lines (2822 loc) · 151 KB
/
SessionTest.java
File metadata and controls
3386 lines (2822 loc) · 151 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package quickfix;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import quickfix.field.ApplVerID;
import quickfix.field.BeginSeqNo;
import quickfix.field.BeginString;
import quickfix.field.DefaultApplVerID;
import quickfix.field.EncryptMethod;
import quickfix.field.EndSeqNo;
import quickfix.field.GapFillFlag;
import quickfix.field.Headline;
import quickfix.field.HeartBtInt;
import quickfix.field.MsgSeqNum;
import quickfix.field.MsgType;
import quickfix.field.NewSeqNo;
import quickfix.field.NextExpectedMsgSeqNum;
import quickfix.field.OrigSendingTime;
import quickfix.field.PossDupFlag;
import quickfix.field.RefSeqNum;
import quickfix.field.SenderCompID;
import quickfix.field.SendingTime;
import quickfix.field.SessionStatus;
import quickfix.field.TargetCompID;
import quickfix.field.TestReqID;
import quickfix.field.Text;
import quickfix.field.converter.UtcTimeOnlyConverter;
import quickfix.field.converter.UtcTimestampConverter;
import quickfix.field.ResetSeqNumFlag;
import quickfix.fix44.Heartbeat;
import quickfix.fix44.Logon;
import quickfix.fix44.Logout;
import quickfix.fix44.News;
import quickfix.fix44.Reject;
import quickfix.fix44.ResendRequest;
import quickfix.fix44.SequenceReset;
import quickfix.fix44.TestRequest;
import quickfix.test.util.ReflectionUtil;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import org.mockito.Mockito;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static quickfix.SessionFactoryTestSupport.createSession;
/**
* Note: most session tests are in the form of acceptance tests.
*/
public class SessionTest {
@Before
public void setUp() {
SystemTime.setTimeSource(null);
}
@Test
public void testDisposalOfFileResources() throws Exception {
final Application application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final MessageStoreFactory mockMessageStoreFactory = mock(MessageStoreFactory.class);
final CloseableMessageStore mockMessageStore = mock(CloseableMessageStore.class);
when(mockMessageStoreFactory.create(sessionID)).thenReturn(mockMessageStore);
final MessageQueueFactory mockMessageQueueFactory = mock(MessageQueueFactory.class);
final MessageQueue mockMessageQueue = mock(MessageQueue.class);
when(mockMessageQueueFactory.create(sessionID)).thenReturn(mockMessageQueue);
final LogFactory mockLogFactory = mock(LogFactory.class);
final CloseableLog mockLog = mock(CloseableLog.class);
when(mockLogFactory.create(sessionID)).thenReturn(mockLog);
try (Session session = new Session(application,
mockMessageStoreFactory, mockMessageQueueFactory, sessionID, null, null, null, mockLogFactory,
new DefaultMessageFactory(), 30, false, 30, UtcTimestampPrecision.MILLIS, true, false,
false, false, false, false, true, false, 1.5, null, true,
new int[] { 5 }, false, false, false, false, true, false, true, false,
null, true, 0, false, false, true, new ArrayList<>(), Session.DEFAULT_HEARTBEAT_TIMEOUT_MULTIPLIER, false)) {
// Simulate socket disconnect
session.setResponder(null);
}
verify(mockMessageStore).close();
verifyNoMoreInteractions(mockMessageStore);
verify(mockLog, atLeastOnce()).onEvent(anyString());
verify(mockLog).close();
verifyNoMoreInteractions(mockLog);
}
/**
* This is a smoke test for handling noncloseable resources. Obviously,
* these resources should not be closed. If they are, it will generate an
* error (probably a class cast exception).
*
* @throws Exception
*/
@Test
public void testNondisposableFileResources() throws Exception {
final Application application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final MessageStoreFactory mockMessageStoreFactory = mock(MessageStoreFactory.class);
final MessageStore mockMessageStore = mock(MessageStore.class);
when(mockMessageStoreFactory.create(sessionID)).thenReturn(mockMessageStore);
final MessageQueueFactory mockMessageQueueFactory = mock(MessageQueueFactory.class);
final MessageQueue mockMessageQueue = mock(MessageQueue.class);
when(mockMessageQueueFactory.create(sessionID)).thenReturn(mockMessageQueue);
final LogFactory mockLogFactory = mock(LogFactory.class);
final Log mockLog = mock(Log.class);
when(mockLogFactory.create(sessionID)).thenReturn(mockLog);
try (Session session = new Session(application,
mockMessageStoreFactory, mockMessageQueueFactory, sessionID, null, null, null, mockLogFactory,
new DefaultMessageFactory(), 30, false, 30, UtcTimestampPrecision.MILLIS, true, false,
false, false, false, false, true, false, 1.5, null, true,
new int[] { 5 }, false, false, false, false, true, false, true, false,
null, true, 0, false, false, true, new ArrayList<>(), Session.DEFAULT_HEARTBEAT_TIMEOUT_MULTIPLIER, false)) {
// Simulate socket disconnect
session.setResponder(null);
verifyNoMoreInteractions(mockMessageStore);
verify(mockLog, atLeastOnce()).onEvent(anyString());
verifyNoMoreInteractions(mockLog);
}
}
private static class MyUnitTestApplication extends UnitTestApplication {
/**
* Just uses no logging on fromApp since testLargeQueue() sends quite
* some messages.
*/
@Override
public void fromApp(Message message, SessionID sessionId) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
fromAppMessages.add(message);
}
}
private interface CloseableMessageStore extends MessageStore, Closeable {
}
private interface CloseableLog extends Log, Closeable {
}
@Test
public void testSessionWithoutValidateSequenceNumbers() throws Exception {
final UnitTestApplication application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
try (Session session = createSession(sessionID, application, true,
true, false)) {
final UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
final Message logonRequest = new Message(responder.sentMessageData);
session.next(createLogonResponse(sessionID, logonRequest, 1));
assertEquals(
1,
application.lastToAdminMessage().getHeader()
.getInt(MsgSeqNum.FIELD));
assertEquals(2, session.getStore().getNextTargetMsgSeqNum());
assertEquals(2, session.getStore().getNextSenderMsgSeqNum());
session.next(createHeartbeatMessage(1002));
assertNotEquals(ResendRequest.MSGTYPE, application
.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
session.next(createHeartbeatMessage(1003));
assertNotEquals(ResendRequest.MSGTYPE, application
.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
session.next(createHeartbeatMessage(1001));
assertNotEquals(ResendRequest.MSGTYPE, application
.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
}
}
// QFJ-703
@Test
public void testPossDupMessageWithoutOrigSendingTime() throws Exception {
// test default behaviour, i.e. that the message is rejected
// when not setting 122/OrigSendingTime
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
UnitTestApplication application = new UnitTestApplication();
Session session = createSession(sessionID, application, true, true,
true);
UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
Message logonRequest = new Message(responder.sentMessageData);
session.next(createLogonResponse(sessionID, logonRequest, 1));
assertEquals(
1,
application.lastToAdminMessage().getHeader()
.getInt(MsgSeqNum.FIELD));
assertEquals(2, session.getStore().getNextTargetMsgSeqNum());
assertEquals(2, session.getStore().getNextSenderMsgSeqNum());
News newsMessage = createAppMessage(2);
newsMessage.getHeader().setBoolean(PossDupFlag.FIELD, true);
session.next(newsMessage);
assertEquals(Reject.MSGTYPE, application.lastToAdminMessage()
.getHeader().getString(MsgType.FIELD));
assertNull(application.lastFromAppMessage());
// test that the message is NOT rejected when
// setting requiresOrigSendingTime=false
// and not setting 122/OrigSendingTime
application = new UnitTestApplication();
session = createSession(sessionID, application, true, true, true);
responder = new UnitTestResponder();
session.setRequiresOrigSendingTime(false);
session.setResponder(responder);
session.logon();
session.next();
logonRequest = new Message(responder.sentMessageData);
session.next(createLogonResponse(sessionID, logonRequest, 1));
assertEquals(
1,
application.lastToAdminMessage().getHeader()
.getInt(MsgSeqNum.FIELD));
assertEquals(2, session.getStore().getNextTargetMsgSeqNum());
assertEquals(2, session.getStore().getNextSenderMsgSeqNum());
newsMessage = createAppMessage(2);
newsMessage.getHeader().setBoolean(PossDupFlag.FIELD, true);
session.next(newsMessage);
assertEquals(Logon.MSGTYPE, application.lastToAdminMessage()
.getHeader().getString(MsgType.FIELD));
assertNull(application.lastToAppMessage());
assertEquals(News.MSGTYPE, application.lastFromAppMessage()
.getHeader().getString(MsgType.FIELD));
session.close();
}
@Test
public void testInferResetSeqNumAcceptedWithNonInitialSequenceNumber()
throws Exception {
final Application application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
try (Session session = createSession(sessionID, application, true,
true)) {
final UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
final Message logonRequest = new Message(responder.sentMessageData);
session.next(createLogonResponse(sessionID, logonRequest, 2));
assertTrue(
"Should not infer a reset when the sequence number is not one",
responder.disconnectCalled);
}
}
@Test
public void testInferResetSeqNumAccepted() throws Exception {
final Application application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
try (Session session = createSession(sessionID, application, true,
true)) {
final UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
final Message logonRequest = new Message(responder.sentMessageData);
final Message logonResponse = createLogonResponse(sessionID,
logonRequest, 1);
session.next(logonResponse);
assertFalse("Should not disconnect when an accepted reset is inferred",
responder.disconnectCalled);
}
}
@Test
// QFJ-603
public void testUnsupportedVersion() throws Exception {
final UnitTestApplication application = new UnitTestApplication();
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
try (Session session = createSession(sessionID, application, true,
true)) {
final UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
final Message logonRequest = new Message(responder.sentMessageData);
final Message logonResponse = createLogonResponse(sessionID,
logonRequest, 1);
session.next(logonResponse);
final News newsMessage = createAppMessage(2);
// set a BeginString unsupported by the session
newsMessage.getHeader().setString(BeginString.FIELD,
FixVersions.BEGINSTRING_FIX40);
session.next(newsMessage);
final Message lastToAdminMessage = application.lastToAdminMessage();
assertEquals(MsgType.LOGOUT,
lastToAdminMessage.getHeader().getString(MsgType.FIELD));
assertEquals(
"Incorrect BeginString: Message version 'FIX.4.0' does not match the session version 'FIX.4.4'",
lastToAdminMessage.getString(Text.FIELD));
assertTrue(responder.disconnectCalled);
}
}
// QFJ-650
@Test
public void testLogoutOnMissingMsgSeqNum() throws Exception {
final Application application = new UnitTestApplication();
try (Session session = setUpSession(application, false,
new UnitTestResponder())) {
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
logonTo(session);
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
final TestRequest testRequest = (TestRequest) createAdminMessage(2);
session.next(testRequest);
assertEquals(3, state.getNextSenderMsgSeqNum());
assertEquals(3, state.getNextTargetMsgSeqNum());
testRequest.getHeader().removeField(MsgSeqNum.FIELD);
// this should disconnect the session due to the missing MsgSeqNum
session.next(testRequest);
assertFalse("Session should be disconnected", session.isLoggedOn());
// make sure that the target seq num has not been incremented
assertEquals(4, state.getNextSenderMsgSeqNum());
assertEquals(3, state.getNextTargetMsgSeqNum());
session.setResponder(new UnitTestResponder());
logonTo(session, 3);
assertEquals(5, state.getNextSenderMsgSeqNum());
assertEquals(4, state.getNextTargetMsgSeqNum());
assertTrue("Session should be connected", session.isLoggedOn());
}
}
// QFJ-750
@Test
public void testLogoutMsgSeqNumTooHighOrLow() throws Exception {
final Application application = new UnitTestApplication();
try (Session session = setUpSession(application, false,
new UnitTestResponder())) {
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
logonTo(session);
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
final TestRequest testRequest = (TestRequest) createAdminMessage(2);
session.next(testRequest);
assertEquals(3, state.getNextSenderMsgSeqNum());
assertEquals(3, state.getNextTargetMsgSeqNum());
logoutFrom(session, 100);
assertFalse("Session should be disconnected", session.isLoggedOn());
// make sure that the target seq num has not been incremented
assertEquals(4, state.getNextSenderMsgSeqNum());
assertEquals(3, state.getNextTargetMsgSeqNum());
session.setResponder(new UnitTestResponder());
logonTo(session, 3);
assertEquals(5, state.getNextSenderMsgSeqNum());
assertEquals(4, state.getNextTargetMsgSeqNum());
assertTrue("Session should be connected", session.isLoggedOn());
logoutFrom(session, 1);
// make sure that the target seq num has not been incremented
assertEquals(6, state.getNextSenderMsgSeqNum());
assertEquals(4, state.getNextTargetMsgSeqNum());
}
}
@Test
public void testRejectMsgSeqNumTooHighOrLow() throws Exception {
final Application application = new UnitTestApplication();
try (Session session = setUpSession(application, false,
new UnitTestResponder())) {
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
logonTo(session);
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
processMessage(session, createReject(2, 100));
assertEquals(3, state.getNextTargetMsgSeqNum());
// Reject with unexpected seqnum should not increment target seqnum
processMessage(session, createReject(50, 100));
assertEquals(3, state.getNextTargetMsgSeqNum());
// Reject with unexpected seqnum should not increment target seqnum
processMessage(session, createReject(1, 100));
assertEquals(3, state.getNextTargetMsgSeqNum());
}
}
/**
* QFJ-357 Until QF/J 1.5.1 the behaviour was observed that a Logout message
* was always sent as first message. This could be be provoked by altering
* the Session file to contain an old timestamp and deleting the filestore
* files to set the sequence numbers to 1. On the next Logon attempt, the
* Session would get reset and a Logout message would get sent. On versions
* newer than 1.5.1 this test should pass.
*/
@Test
public void testLogonIsFirstMessageOnAcceptor() throws Exception {
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
try (Session session = setUpFileStoreSession(application, false,
new UnitTestResponder(), settings, sessionID)) {
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
logonTo(session);
// we should only answer with a Logon message
assertEquals(1, application.toAdminMessages.size());
assertEquals(MsgType.LOGON, application.toAdminMessages.get(0)
.getHeader().getString(MsgType.FIELD));
// no reset should have been triggered by QF/J after the Logon attempt
assertEquals(0, application.sessionResets);
assertTrue("Session should be connected", session.isLoggedOn());
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
}
}
// QFJ-773
@Test
public void testLogonLogoutOnAcceptor() throws Exception {
final LocalDateTime now = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(now);
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.atOffset(offset).toInstant().toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().minus(100000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
final UnitTestResponder responder = new UnitTestResponder();
try (Session session = setUpFileStoreSession(application, false,
responder, settings, sessionID)) {
session.addStateListener(application);
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
logonTo(session);
// we should only answer with a Logon message
assertEquals(1, application.toAdminMessages.size());
assertEquals(MsgType.LOGON, application.toAdminMessages.get(0)
.getHeader().getString(MsgType.FIELD));
// no reset should have been triggered by QF/J after the Logon attempt
assertEquals(0, application.sessionResets);
assertTrue("Session should be connected", session.isLoggedOn());
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
session.next();
// increment time to force logout and reset
systemTimeSource.increment(3700000);
session.next();
logoutFrom(session, state.getNextTargetMsgSeqNum());
assertEquals(SystemTime.getDate(), state.getCreationTime());
systemTimeSource.increment(10000);
session.next();
systemTimeSource.increment(10000);
session.next();
systemTimeSource.increment(10000);
session.next();
systemTimeSource.increment(10000);
// we should only reset once outside of the session time window
assertEquals(1, application.sessionResets);
assertFalse("Session should be disconnected", session.isLoggedOn());
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
session.setResponder(responder);
// this should get rejected since we are outside of the session time
// window
logonTo(session);
assertFalse("Session should be disconnected", session.isLoggedOn());
// if we now logon to the session, it will be considered new
// and a reset will be done
session.setResponder(responder);
session.next();
assertEquals(2, application.sessionResets);
systemTimeSource.increment(86100000); // jump one day but stay inside
// session time
session.next();
logonTo(session);
assertTrue("Session should be connected", session.isLoggedOn());
assertEquals(SystemTime.getDate(), state.getCreationTime());
// check that the creation time is not updated inside of the session
// time window
int delta = 60000;
systemTimeSource.increment(delta);
assertEquals(SystemTime.getDate().getTime()
- state.getCreationTime().getTime(), delta);
session.next();
assertTrue("Session should be connected", session.isLoggedOn());
}
}
@Test
// QFJ-716
public void testStartOfInitiatorOutsideOfSessionTime() throws Exception {
final LocalDateTime now = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(now);
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.toInstant(offset).toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
// we want to start the initiator before the StartTime
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(1800000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
try (Session session = setUpFileStoreSession(application, true,
new UnitTestResponder(), settings, sessionID)) {
session.addStateListener(application);
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
session.next();
systemTimeSource.increment(10000);
session.next();
systemTimeSource.increment(10000);
session.next();
// we should send no messages since we are outside of session time
assertEquals(0, application.toAdminMessages.size());
// no reset should have been triggered by QF/J (since we were not logged
// on)
assertEquals(0, application.sessionResets);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
// increase time to be within session time
systemTimeSource.increment(1900000);
session.next();
session.next();
// we should have sent a Logon since the StartTime has been reached now
assertEquals(1, application.toAdminMessages.size());
Message logon = application.toAdminMessages.get(0);
assertEquals(MsgType.LOGON, logon.getHeader().getString(MsgType.FIELD));
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
Message createLogonResponse = createLogonResponse(new SessionID(
FixVersions.BEGINSTRING_FIX44, "TARGET", "SENDER"), logon, 1);
session.next(createLogonResponse);
assertTrue(session.isLoggedOn());
assertEquals(1, application.sessionResets);
// increase time to be out of session time
systemTimeSource.increment(1900000);
session.next();
logoutFrom(session, state.getNextTargetMsgSeqNum());
Message logout = application.lastToAdminMessage();
assertEquals(MsgType.LOGOUT, logout.getHeader()
.getString(MsgType.FIELD));
assertFalse(session.isLoggedOn());
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
assertEquals(2, application.sessionResets);
}
}
@Test
// QFJ-716 - we need to make sure that the first message sent is a Logon
public void testStartOfInitiatorInsideOfSessionTime() throws Exception {
final LocalDateTime now = LocalDateTime.now();
ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(now);
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.toInstant(offset).toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
// we want to start the initiator before the StartTime
// make sure we start inside the Session time
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().minus(2000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
try (Session session = setUpFileStoreSession(application, true,
new UnitTestResponder(), settings, sessionID)) {
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
session.next();
systemTimeSource.increment(1000);
session.next();
systemTimeSource.increment(1000);
session.next();
// we should have sent a Logon since we are inside of the SessionTime
assertEquals(1, application.toAdminMessages.size());
assertEquals(MsgType.LOGON, application.toAdminMessages.get(0)
.getHeader().getString(MsgType.FIELD));
// no reset should have been triggered by QF/J
assertEquals(0, application.sessionResets);
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
}
}
@Test
// QFJ-926
public void testSessionNotResetRightAfterLogonOnAcceptor() throws Exception {
// truncate to seconds, otherwise the session time check in Session.next()
// might already reset the session since the session schedule has only precision of seconds
final LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(now);
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.toInstant(offset).toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
// we want to start the session before the StartTime
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(4500L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
try (Session session = setUpFileStoreSession(application, false,
new UnitTestResponder(), settings, sessionID)) {
session.addStateListener(application);
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
session.next();
// we should send no messages since we are outside of session time
assertEquals(0, application.toAdminMessages.size());
// no reset should have been triggered by QF/J (since we were not logged on)
assertEquals(0, application.sessionResets);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
// increase time to be within session time
systemTimeSource.increment(5000);
// there should be a Logon but no subsequent reset
logonTo(session, 1);
// call next() to provoke SessionTime check which should NOT reset seqnums now
session.next();
assertEquals(1, application.toAdminMessages.size());
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
assertTrue(session.isLoggedOn());
assertEquals(1, application.sessionResets);
systemTimeSource.increment(5000);
session.disconnect("test", false);
systemTimeSource.increment(5000);
session.next();
session.setResponder(new UnitTestResponder());
logonTo(session, 2);
session.next();
// check that no reset is done on next Logon
assertEquals(1, application.sessionResets);
}
}
@Test
// QFJ-926
public void testSessionNotResetRightAfterLogonOnInitiator() throws Exception {
// truncate to seconds, otherwise the session time check in Session.next()
// might already reset the session since the session schedule has only precision of seconds
final LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(now);
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.toInstant(offset).toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
// we want to start the session before the StartTime
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(5000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
UnitTestResponder responder = new UnitTestResponder();
try (Session session = setUpFileStoreSession(application, true, responder, settings, sessionID)) {
session.addStateListener(application);
final SessionState state = getSessionState(session);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
session.next();
// we should send no messages since we are outside of session time
assertEquals(0, application.toAdminMessages.size());
// no reset should have been triggered by QF/J (since we were not logged on)
assertEquals(0, application.sessionResets);
assertEquals(1, state.getNextSenderMsgSeqNum());
assertEquals(1, state.getNextTargetMsgSeqNum());
// increase time to be almost within session time to check if session needs to be reset
// (will not reset since it is not yet within session time)
systemTimeSource.increment(4500);
session.next();
// increase time further so that Logon is sent but reset is not done since last check
// of session time was done within one second
systemTimeSource.increment(600);
session.next();
systemTimeSource.increment(1000);
session.next(createLogonResponse(new SessionID(FixVersions.BEGINSTRING_FIX44, "TARGET", "SENDER"), application.lastToAdminMessage(), 1));
assertEquals(1, application.toAdminMessages.size());
assertEquals(2, state.getNextSenderMsgSeqNum());
assertEquals(2, state.getNextTargetMsgSeqNum());
assertTrue(session.isLoggedOn());
assertEquals(1, application.sessionResets);
systemTimeSource.increment(5000);
session.disconnect("test", false);
systemTimeSource.increment(5000);
session.next();
responder = new UnitTestResponder();
session.setResponder(responder);
session.next();
session.next(createLogonResponse(new SessionID(FixVersions.BEGINSTRING_FIX44, "TARGET", "SENDER"), application.lastToAdminMessage(), 2));
// check that no reset is done on next Logon
assertEquals(1, application.sessionResets);
}
}
@Test
// QFJ-929/QFJ-933
public void testLogonIsAnsweredWithLogoutOnException() throws Exception {
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
UnitTestApplication application = new UnitTestApplication();
try (Session session = SessionFactoryTestSupport.createSession(sessionID, application,
false, false, true, true, null)) {
UnitTestResponder responder = new UnitTestResponder();
session.setResponder(responder);
session.logon();
session.next();
Logon logonRequest = new Logon();
setUpHeader(session.getSessionID(), logonRequest, true, 1);
logonRequest.setInt(HeartBtInt.FIELD, 30);
logonRequest.setString(EncryptMethod.FIELD, "");
logonRequest.toString(); // calculate length and checksum
session.next(logonRequest);
// session should not be logged on due to empty EncryptMethod
assertFalse(session.isLoggedOn());
assertEquals(1, application.lastToAdminMessage().getHeader().getInt(MsgSeqNum.FIELD));
assertEquals(MsgType.LOGOUT, application.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
assertEquals(2, session.getStore().getNextTargetMsgSeqNum());
assertEquals(2, session.getStore().getNextSenderMsgSeqNum());
session.setResponder(responder);
session.logon();
session.next();
setUpHeader(session.getSessionID(), logonRequest, true, 2);
logonRequest.removeField(EncryptMethod.FIELD);
logonRequest.toString(); // calculate length and checksum
session.next(logonRequest);
// session should not be logged on due to missing EncryptMethod
assertFalse(session.isLoggedOn());
assertEquals(2, application.lastToAdminMessage().getHeader().getInt(MsgSeqNum.FIELD));
assertEquals(MsgType.LOGOUT, application.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
assertEquals(3, session.getStore().getNextTargetMsgSeqNum());
assertEquals(3, session.getStore().getNextSenderMsgSeqNum());
session.setResponder(responder);
session.logon();
session.next();
setUpHeader(session.getSessionID(), logonRequest, true, 3);
logonRequest.setString(EncryptMethod.FIELD, "A");
logonRequest.toString(); // calculate length and checksum
session.next(logonRequest);
// session should not be logged on due to IncorrectDataFormat
assertFalse(session.isLoggedOn());
assertEquals(3, application.lastToAdminMessage().getHeader().getInt(MsgSeqNum.FIELD));
assertEquals(MsgType.LOGOUT, application.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
assertEquals(4, session.getStore().getNextTargetMsgSeqNum());
assertEquals(4, session.getStore().getNextSenderMsgSeqNum());
session.setResponder(responder);
session.logon();
session.next();
setUpHeader(session.getSessionID(), logonRequest, true, 4);
logonRequest.setString(EncryptMethod.FIELD, "99");
logonRequest.toString(); // calculate length and checksum
session.next(logonRequest);
// session should not be logged on due to IncorrectTagValue
assertFalse(session.isLoggedOn());
assertEquals(4, application.lastToAdminMessage().getHeader().getInt(MsgSeqNum.FIELD));
assertEquals(MsgType.LOGOUT, application.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
assertEquals(5, session.getStore().getNextTargetMsgSeqNum());
assertEquals(5, session.getStore().getNextSenderMsgSeqNum());
session.setResponder(responder);
session.logon();
session.next();
setUpHeader(session.getSessionID(), logonRequest, true, 5);
logonRequest.setString(EncryptMethod.FIELD, "0");
logonRequest.setString(NextExpectedMsgSeqNum.FIELD, "XXX");
logonRequest.toString(); // calculate length and checksum
session.next(logonRequest);
// session should not be logged on due to IncorrectTagValue
assertFalse(session.isLoggedOn());
assertEquals(5, application.lastToAdminMessage().getHeader().getInt(MsgSeqNum.FIELD));
assertEquals(MsgType.LOGOUT, application.lastToAdminMessage().getHeader().getString(MsgType.FIELD));
assertEquals(6, session.getStore().getNextTargetMsgSeqNum());
assertEquals(6, session.getStore().getNextSenderMsgSeqNum());
}
}
/**
* QFJ-357 This test should make sure that outside the Session time _only_ a
* Logout message is sent to the counterparty. Formerly it could be observed
* sometimes that there was a Logon message with a Logout message
* immediately following.
*/
@Test
public void testLogonOutsideSessionTimeIsRejected() throws Exception {
final LocalDateTime now = LocalDateTime.now();
final MockSystemTimeSource systemTimeSource = new MockSystemTimeSource(
now.toInstant(ZoneOffset.UTC).toEpochMilli());
SystemTime.setTimeSource(systemTimeSource);
// set up some basic stuff
final SessionID sessionID = new SessionID(
FixVersions.BEGINSTRING_FIX44, "SENDER", "TARGET");
final SessionSettings settings = SessionSettingsTest.setUpSession(null);
// construct a session schedule which is not active at the moment
// add 30 minutes
settings.setString("StartTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(1800000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("EndTime", UtcTimeOnlyConverter.convert(now.toLocalTime().plus(3600000L, ChronoUnit.MILLIS), UtcTimestampPrecision.SECONDS));
settings.setString("TimeZone", TimeZone.getDefault().getID());
setupFileStoreForQFJ357(sessionID, settings);
// Session gets constructed, triggering a reset
final UnitTestApplication application = new UnitTestApplication();
try (Session session = setUpFileStoreSession(application, false,
new UnitTestResponder(), settings, sessionID)) {