forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionStateTest.cs
executable file
·220 lines (182 loc) · 9.09 KB
/
SessionStateTest.cs
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
using System;
using NUnit.Framework;
using QuickFix;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace UnitTests
{
[TestFixture]
public class SessionStateTest
{
[SetUp]
public void Init()
{
}
[Test]
public void TimedOut()
{
int heartBtIntMillis = 60 * 1000;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastReceivedTime = now;
now = now.AddMilliseconds(heartBtIntMillis);
Assert.False(SessionState.TimedOut(now, heartBtIntMillis, lastReceivedTime));
now = now.AddMilliseconds(heartBtIntMillis);
Assert.False(SessionState.TimedOut(now, heartBtIntMillis, lastReceivedTime));
now = now.AddMilliseconds(heartBtIntMillis);
Assert.True(SessionState.TimedOut(now, heartBtIntMillis, lastReceivedTime));
}
[Test]
public void LogonTimedOut()
{
int logonTimeout = 5 * 1000;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastRecvTime = now;
now = now.AddMilliseconds(4000);
Assert.False(SessionState.LogonTimedOut(now, logonTimeout, lastRecvTime));
now = now.AddMilliseconds(1000);
Assert.True(SessionState.LogonTimedOut(now, logonTimeout, lastRecvTime));
now = now.AddMilliseconds(1);
Assert.True(SessionState.LogonTimedOut(now, logonTimeout, lastRecvTime));
}
[Test]
public void LogoutTimedOut()
{
bool sentLogout = true;
int logoutTimeout = 2 * 1000;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastSentTime = now;
now = now.AddMilliseconds(1000);
Assert.False(SessionState.LogoutTimedOut(now, sentLogout, logoutTimeout, lastSentTime));
now = now.AddMilliseconds(1000);
Assert.True(SessionState.LogoutTimedOut(now, sentLogout, logoutTimeout, lastSentTime));
sentLogout = false;
Assert.False(SessionState.LogoutTimedOut(now, sentLogout, logoutTimeout, lastSentTime));
}
[Test]
public void NeedTestRequest()
{
int heartBtIntMillis = 30 * 1000;
int testRequestCounter = 0;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastReceivedTime = now;
now = now.AddMilliseconds(heartBtIntMillis);
Assert.False(SessionState.NeedTestRequest(now, heartBtIntMillis, lastReceivedTime, testRequestCounter));
now = now.AddMilliseconds(heartBtIntMillis);
Assert.True(SessionState.NeedTestRequest(now, heartBtIntMillis, lastReceivedTime, testRequestCounter));
testRequestCounter += 1;
Assert.False(SessionState.NeedTestRequest(now, heartBtIntMillis, lastReceivedTime, testRequestCounter));
now = now.AddMilliseconds(heartBtIntMillis);
Assert.True(SessionState.NeedTestRequest(now, heartBtIntMillis, lastReceivedTime, testRequestCounter));
}
[Test]
public void NeedHeartbeat()
{
int heartBtIntMillis = 60 * 1000;
int testRequestCounter = 0;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastSentTime = now;
now = now.AddMilliseconds(heartBtIntMillis / 3);
Assert.False(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
now = now.AddMilliseconds(heartBtIntMillis / 3);
Assert.False(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
now = now.AddMilliseconds(heartBtIntMillis / 2);
Assert.True(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
testRequestCounter = 1;
Assert.False(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
now = now.AddMilliseconds(2 * heartBtIntMillis);
Assert.False(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
testRequestCounter = 0;
Assert.True(SessionState.NeedHeartbeat(now, heartBtIntMillis, lastSentTime, testRequestCounter));
}
[Test]
public void WithinHeartbeat()
{
int heartBtIntMillis = 60 * 1000;
System.DateTime now = System.DateTime.UtcNow;
System.DateTime lastSentTime = now;
System.DateTime lastReceivedTime = now;
now = now.AddMilliseconds(heartBtIntMillis - 1);
Assert.True(SessionState.WithinHeartbeat(now, heartBtIntMillis, lastSentTime, lastReceivedTime));
now = now.AddMilliseconds(1);
Assert.False(SessionState.WithinHeartbeat(now, heartBtIntMillis, lastSentTime, lastReceivedTime));
lastSentTime = lastSentTime.AddMilliseconds(1);
lastReceivedTime = lastReceivedTime.AddMilliseconds(1);
Assert.True(SessionState.WithinHeartbeat(now, heartBtIntMillis, lastSentTime, lastReceivedTime));
}
[Test]
public void ThreadSafeSetAndGet() {
//Set up store
if (System.IO.Directory.Exists("store")) {
System.IO.Directory.Delete("store", true);
}
SessionID sessionId = new SessionID("FIX.4.2", "SENDERCOMP", "TARGETCOMP");
Dictionary config = new Dictionary();
config.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
config.SetString(SessionSettings.FILE_STORE_PATH, "store");
SessionSettings settings = new SessionSettings();
settings.Set(sessionId, config);
FileStoreFactory factory = new FileStoreFactory(settings);
FileStore store = (FileStore)factory.Create(sessionId);
NullLog log = new NullLog();
//Set up sessionstate
SessionState state = new SessionState(log, 1) {MessageStore = store};
Hashtable errorsTable = Hashtable.Synchronized(new Hashtable());//used in more than 1 thread at a time
Hashtable setTable = new Hashtable(1000);//only used in 1 thread at a time
Hashtable getTable = new Hashtable(1000);//only used in 1 thread at a time
//Synchronously populate 1000 messages
for (int i = 1; i < 1000; i++) {
string msg = "msg" + i;
state.Set(i, msg);
setTable[i] = msg;
}
//Simulate background sending of messages that populate into the store
AutoResetEvent setEvent = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(delegate(object stateObject) {
AutoResetEvent internalSetEvent = (AutoResetEvent)((object[])stateObject)[0];
SessionState internalState = (SessionState)((object[])stateObject)[1];
for (int i = 1001; i < 2000; i++) {
try {
internalState.Set(i, "msg" + i);
}
catch (System.IO.IOException ex) {
errorsTable[ex.Message] = ex;
}
}
internalSetEvent.Set();
}
, new object[] { setEvent, state });
//Simulate background reading of messages from the store - like is done in a resend request answer
AutoResetEvent getEvent = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(delegate(object stateObject){
AutoResetEvent internalGetEvent = (AutoResetEvent)((object[])stateObject)[0];
SessionState internalState = (SessionState)((object[])stateObject)[1];
for (int i = 1; i < 1000; i++) {
try {
List<string> lst = new List<string>(1);
internalState.Get(i, i, lst);
if (lst.Count == 0) {
getTable[i] = "nothing read";
}
else {
getTable[i] = lst[0];
}
}
catch (System.IO.IOException ex) {
errorsTable[ex.Message] = ex;
}
}
internalGetEvent.Set();
}
, new object[]{getEvent, state});
//wait till done and assert results
Assert.True(setEvent.WaitOne(10000), "Get or Set hung/timed out during concurrent usage");
Assert.True(getEvent.WaitOne(10000), "Get or Set hung/timed out during concurrent usage");
Assert.AreEqual(setTable, getTable, "Garbled data read in concurrent set and get (like between resendrequest and send)");
Assert.AreEqual(errorsTable.Count, 0, "IOException occured in concurrent set and get (like between resendrequest and send)");
//Tear down filestore
state.Dispose();
store.Dispose();
}
}
}