forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
message_router_test.go
192 lines (155 loc) · 6.45 KB
/
message_router_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
package quickfix
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type MessageRouterTestSuite struct {
suite.Suite
*MessageRouter
msg *Message
sessionID SessionID
returnReject MessageRejectError
routedBy string
routedSessionID SessionID
routedMessage *Message
}
func TestMessageRouterTestSuite(t *testing.T) {
suite.Run(t, new(MessageRouterTestSuite))
}
func (suite *MessageRouterTestSuite) givenTheRoute(beginString, msgType string) {
suite.AddRoute(
beginString,
msgType,
func(msg *Message, sessionID SessionID) MessageRejectError {
suite.routedBy = fmt.Sprintf("%v:%v", beginString, msgType)
suite.routedSessionID = sessionID
suite.routedMessage = msg
return suite.returnReject
},
)
}
func (suite *MessageRouterTestSuite) givenTheMessage(msgBytes []byte) {
err := ParseMessage(suite.msg, bytes.NewBuffer(msgBytes))
suite.Nil(err)
var beginString FIXString
suite.Require().Nil(suite.msg.Header.GetField(tagBeginString, &beginString))
var senderCompID FIXString
suite.Require().Nil(suite.msg.Header.GetField(tagSenderCompID, &senderCompID))
var targetCompID FIXString
suite.Require().Nil(suite.msg.Header.GetField(tagTargetCompID, &targetCompID))
suite.sessionID = SessionID{BeginString: string(beginString), SenderCompID: string(targetCompID), TargetCompID: string(senderCompID)}
}
func (suite *MessageRouterTestSuite) givenTargetDefaultApplVerIDForSession(defaultApplVerID string, sessionID SessionID) {
s := &session{
sessionID: sessionID,
targetDefaultApplVerID: defaultApplVerID,
}
suite.Nil(registerSession(s))
}
func (suite *MessageRouterTestSuite) givenAFIX42NewOrderSingle() {
suite.givenTheMessage([]byte("8=FIX.4.29=8735=D49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=235"))
}
func (suite *MessageRouterTestSuite) givenAFIXTLogonMessage() {
suite.givenTheMessage([]byte("8=FIXT.1.19=6335=A34=149=TW52=20160420-21:21:4956=ISLD98=0108=21137=810=105"))
}
func (suite *MessageRouterTestSuite) anticipateReject(rej MessageRejectError) {
suite.returnReject = rej
}
func (suite *MessageRouterTestSuite) verifyMessageNotRouted() {
suite.Equal("", suite.routedBy, "Message should not be routed")
}
func (suite *MessageRouterTestSuite) verifyMessageRoutedBy(beginString, msgType string) {
suite.NotEqual("", suite.routedBy, "Message expected to be routed")
suite.Equal(fmt.Sprintf("%v:%v", beginString, msgType), suite.routedBy)
suite.Equal(suite.sessionID, suite.routedSessionID)
suite.Equal(suite.msg.String(), suite.routedMessage.String())
}
func (suite *MessageRouterTestSuite) resetRouter() {
suite.MessageRouter = NewMessageRouter()
suite.routedBy = ""
suite.routedSessionID = SessionID{}
suite.routedMessage = &Message{}
suite.returnReject = nil
}
func (suite *MessageRouterTestSuite) SetupTest() {
suite.resetRouter()
sessionsLock.Lock()
defer sessionsLock.Unlock()
sessions = make(map[SessionID]*session)
suite.msg = NewMessage()
}
func (suite *MessageRouterTestSuite) TestNoRoute() {
suite.givenTheMessage([]byte("8=FIX.4.39=8735=D49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=235"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageNotRouted()
suite.Equal(NewBusinessMessageRejectError("Unsupported Message Type", 3, nil), rej)
}
func (suite *MessageRouterTestSuite) TestNoRouteWhitelistedMessageTypes() {
var tests = []string{"0", "A", "1", "2", "3", "4", "5", "j"}
for _, test := range tests {
suite.SetupTest()
msg := fmt.Sprintf("8=FIX.4.39=8735=%v49=TW34=356=ISLD52=20160421-14:43:5040=160=20160421-14:43:5054=121=311=id10=235", test)
suite.givenTheMessage([]byte(msg))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageNotRouted()
suite.Nil(rej, "Message type '%v' should not be rejected by the MessageRouter", test)
}
}
func (suite *MessageRouterTestSuite) TestSimpleRoute() {
suite.givenTheRoute(string(BeginStringFIX42), "D")
suite.givenTheRoute(string(BeginStringFIXT11), "A")
suite.givenAFIX42NewOrderSingle()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(string(BeginStringFIX42), "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestSimpleRouteWithReject() {
suite.givenTheRoute(string(BeginStringFIX42), "D")
suite.givenTheRoute(string(BeginStringFIXT11), "A")
suite.anticipateReject(NewMessageRejectError("some error", 5, nil))
suite.givenAFIX42NewOrderSingle()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(string(BeginStringFIX42), "D")
suite.Equal(suite.returnReject, rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAdminMessage() {
suite.givenTheRoute(string(BeginStringFIX42), "D")
suite.givenTheRoute(string(BeginStringFIXT11), "A")
suite.givenAFIXTLogonMessage()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(string(BeginStringFIXT11), "A")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXT50AppWithApplVerID() {
suite.givenTheRoute(BeginStringFIX42, "D")
suite.givenTheRoute(ApplVerIDFIX50, "D")
suite.givenTheRoute(ApplVerIDFIX50SP1, "D")
suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=740=160=20160424-16:48:2611=id21=310=120"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(ApplVerIDFIX50, "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAppWithApplVerID() {
suite.givenTheRoute(BeginStringFIX42, "D")
suite.givenTheRoute(ApplVerIDFIX50, "D")
suite.givenTheRoute(ApplVerIDFIX50SP1, "D")
suite.givenTheMessage([]byte("8=FIXT.1.19=8935=D49=TW34=356=ISLD52=20160424-16:48:261128=840=160=20160424-16:48:2611=id21=310=120"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(ApplVerIDFIX50SP1, "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAppWithDefaultApplVerID() {
suite.givenTheRoute(BeginStringFIX42, "D")
suite.givenTheRoute(ApplVerIDFIX50, "D")
suite.givenTheRoute(ApplVerIDFIX50SP1, "D")
suite.givenTargetDefaultApplVerIDForSession(
"8",
SessionID{BeginString: string(BeginStringFIXT11), SenderCompID: "ISLD", TargetCompID: "TW"},
)
suite.givenTheMessage([]byte("8=FIXT.1.19=8235=D49=TW34=356=ISLD52=20160424-16:48:2640=160=20160424-16:48:2611=id21=310=120"))
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(ApplVerIDFIX50SP1, "D")
suite.Nil(rej)
}