forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickfix_test.go
306 lines (247 loc) · 7.54 KB
/
quickfix_test.go
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
// Copyright (c) quickfixengine.org All rights reserved.
//
// 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
import (
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/quickfixgo/quickfix/internal"
)
type QuickFIXSuite struct {
suite.Suite
}
type KnowsFieldMap interface {
Has(Tag) bool
GetString(Tag) (string, MessageRejectError)
GetInt(Tag) (int, MessageRejectError)
GetField(Tag, FieldValueReader) MessageRejectError
}
func (s *QuickFIXSuite) MessageType(msgType string, msg *Message) {
s.FieldEquals(tagMsgType, msgType, msg.Header)
}
func (s *QuickFIXSuite) FieldEquals(tag Tag, expectedValue interface{}, fieldMap KnowsFieldMap) {
s.Require().True(fieldMap.Has(tag), "Tag %v not set", tag)
switch expected := expectedValue.(type) {
case string:
val, err := fieldMap.GetString(tag)
s.Nil(err)
s.Equal(expected, val)
case int:
val, err := fieldMap.GetInt(tag)
s.Nil(err)
s.Equal(expected, val)
case bool:
var val FIXBoolean
err := fieldMap.GetField(tag, &val)
s.Nil(err)
s.Equal(expected, val.Bool())
default:
s.FailNow("Field type not handled")
}
}
func (s *QuickFIXSuite) MessageEqualsBytes(expectedBytes []byte, msg *Message) {
actualBytes := msg.build()
s.Equal(string(actualBytes), string(expectedBytes))
}
// MockStore wraps a memory store and mocks Refresh for convenience.
type MockStore struct {
mock.Mock
memoryStore
}
func (s *MockStore) Refresh() error {
return s.Called().Error(0)
}
type MockApp struct {
mock.Mock
decorateToAdmin func(*Message)
lastToAdmin *Message
lastToApp *Message
}
func (e *MockApp) OnCreate(_ SessionID) {
}
func (e *MockApp) OnLogon(_ SessionID) {
e.Called()
}
func (e *MockApp) OnLogout(_ SessionID) {
e.Called()
}
func (e *MockApp) FromAdmin(_ *Message, _ SessionID) (reject MessageRejectError) {
if err, ok := e.Called().Get(0).(MessageRejectError); ok {
return err
}
return nil
}
func (e *MockApp) ToAdmin(msg *Message, _ SessionID) {
e.Called()
if e.decorateToAdmin != nil {
e.decorateToAdmin(msg)
}
e.lastToAdmin = msg
}
func (e *MockApp) ToApp(msg *Message, _ SessionID) (err error) {
e.lastToApp = msg
return e.Called().Error(0)
}
func (e *MockApp) FromApp(_ *Message, _ SessionID) (reject MessageRejectError) {
if err, ok := e.Called().Get(0).(MessageRejectError); ok {
return err
}
return nil
}
type MessageFactory struct {
seqNum int
}
func (m *MessageFactory) SetNextSeqNum(next int) {
m.seqNum = next - 1
}
func (m *MessageFactory) buildMessage(msgType string) *Message {
m.seqNum++
msg := NewMessage()
msg.Header.
SetField(tagBeginString, FIXString(string(BeginStringFIX42))).
SetField(tagSenderCompID, FIXString("TW")).
SetField(tagTargetCompID, FIXString("ISLD")).
SetField(tagSendingTime, FIXUTCTimestamp{Time: time.Now()}).
SetField(tagMsgSeqNum, FIXInt(m.seqNum)).
SetField(tagMsgType, FIXString(msgType))
return msg
}
func (m *MessageFactory) Logout() *Message {
return m.buildMessage(string(msgTypeLogout))
}
func (m *MessageFactory) NewOrderSingle() *Message {
return m.buildMessage("D")
}
func (m *MessageFactory) Heartbeat() *Message {
return m.buildMessage(string(msgTypeHeartbeat))
}
func (m *MessageFactory) Logon() *Message {
return m.buildMessage(string(msgTypeLogon))
}
func (m *MessageFactory) ResendRequest(beginSeqNo int) *Message {
msg := m.buildMessage(string(msgTypeResendRequest))
msg.Body.SetField(tagBeginSeqNo, FIXInt(beginSeqNo))
msg.Body.SetField(tagEndSeqNo, FIXInt(0))
return msg
}
func (m *MessageFactory) SequenceReset(seqNo int) *Message {
msg := m.buildMessage(string(msgTypeSequenceReset))
msg.Body.SetField(tagNewSeqNo, FIXInt(seqNo))
return msg
}
type MockSessionReceiver struct {
sendChannel chan []byte
}
func newMockSessionReceiver() MockSessionReceiver {
return MockSessionReceiver{
sendChannel: make(chan []byte, 10),
}
}
func (p *MockSessionReceiver) LastMessage() (msg []byte, ok bool) {
select {
case msg, ok = <-p.sendChannel:
default:
ok = true
}
return
}
type SessionSuiteRig struct {
QuickFIXSuite
MessageFactory
MockApp MockApp
MockStore MockStore
*session
Receiver MockSessionReceiver
}
func (s *SessionSuiteRig) Init() {
s.MockApp = MockApp{}
s.MockStore = MockStore{}
s.MessageFactory = MessageFactory{}
s.Receiver = newMockSessionReceiver()
s.session = &session{
sessionID: SessionID{BeginString: "FIX.4.2", TargetCompID: "TW", SenderCompID: "ISLD"},
store: &s.MockStore,
application: &s.MockApp,
log: nullLog{},
messageOut: s.Receiver.sendChannel,
sessionEvent: make(chan internal.Event),
}
s.MaxLatency = 120 * time.Second
}
func (s *SessionSuiteRig) State(state sessionState) {
s.IsType(state, s.session.State, "session state should be %v", state)
}
func (s *SessionSuiteRig) MessageSentEquals(msg *Message) {
msgBytes, ok := s.Receiver.LastMessage()
s.True(ok, "Should be connected")
s.NotNil(msgBytes, "Message should have been sent")
s.MessageEqualsBytes(msgBytes, msg)
}
func (s *SessionSuiteRig) LastToAppMessageSent() {
s.MessageSentEquals(s.MockApp.lastToApp)
}
func (s *SessionSuiteRig) LastToAdminMessageSent() {
require.NotNil(s.T(), s.MockApp.lastToAdmin, "No ToAdmin received")
s.MessageSentEquals(s.MockApp.lastToAdmin)
}
func (s *SessionSuiteRig) NotStopped() {
s.False(s.session.Stopped(), "session should not be stopped")
}
func (s *SessionSuiteRig) Stopped() {
s.True(s.session.Stopped(), "session should be stopped")
}
func (s *SessionSuiteRig) Disconnected() {
msg, ok := s.Receiver.LastMessage()
s.Nil(msg, "Expect disconnect, not message")
s.False(ok, "Expect disconnect")
}
func (s *SessionSuiteRig) NoMessageSent() {
msg, _ := s.Receiver.LastMessage()
s.Nil(msg, "no message should be sent but got %s", msg)
}
func (s *SessionSuiteRig) NoMessageQueued() {
s.Empty(s.session.toSend, "no messages should be queueud")
}
func (s *SessionSuiteRig) ExpectStoreReset() {
s.NextSenderMsgSeqNum(1)
s.NextTargetMsgSeqNum(1)
}
func (s *SessionSuiteRig) NextTargetMsgSeqNum(expected int) {
s.Equal(expected, s.session.store.NextTargetMsgSeqNum(), "NextTargetMsgSeqNum should be %v ", expected)
}
func (s *SessionSuiteRig) NextSenderMsgSeqNum(expected int) {
s.Equal(expected, s.session.store.NextSenderMsgSeqNum(), "NextSenderMsgSeqNum should be %v", expected)
}
func (s *SessionSuiteRig) IncrNextSenderMsgSeqNum() {
s.Require().Nil(s.session.store.IncrNextSenderMsgSeqNum())
}
func (s *SessionSuiteRig) IncrNextTargetMsgSeqNum() {
s.Require().Nil(s.session.store.IncrNextTargetMsgSeqNum())
}
func (s *SessionSuiteRig) NoMessagePersisted(seqNum int) {
persistedMessages, err := s.session.store.GetMessages(seqNum, seqNum)
s.Nil(err)
s.Empty(persistedMessages, "The message should not be persisted")
}
func (s *SessionSuiteRig) MessagePersisted(msg *Message) {
var err error
seqNum, err := msg.Header.GetInt(tagMsgSeqNum)
s.Nil(err, "message should have seq num")
persistedMessages, err := s.session.store.GetMessages(seqNum, seqNum)
s.Nil(err)
s.Len(persistedMessages, 1, "a message should be stored at %v", seqNum)
s.MessageEqualsBytes(persistedMessages[0], msg)
}