forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_router_test.go
175 lines (142 loc) · 5.8 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
package quickfix
import (
"fmt"
"testing"
"github.com/quickfixgo/quickfix/enum"
"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) {
msg, err := ParseMessage(msgBytes)
suite.Nil(err)
suite.NotNil(msg)
suite.msg = msg
var beginString FIXString
msg.Header.GetField(tagBeginString, &beginString)
var senderCompID FIXString
msg.Header.GetField(tagSenderCompID, &senderCompID)
var targetCompID FIXString
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,
}
sessions.newSession <- 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()
}
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) TestSimpleRoute() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.BeginStringFIXT11, "A")
suite.givenAFIX42NewOrderSingle()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(enum.BeginStringFIX42, "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestSimpleRouteWithReject() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.BeginStringFIXT11, "A")
suite.anticipateReject(NewMessageRejectError("some error", 5, nil))
suite.givenAFIX42NewOrderSingle()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(enum.BeginStringFIX42, "D")
suite.Equal(suite.returnReject, rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAdminMessage() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.BeginStringFIXT11, "A")
suite.givenAFIXTLogonMessage()
rej := suite.Route(suite.msg, suite.sessionID)
suite.verifyMessageRoutedBy(enum.BeginStringFIXT11, "A")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXT50AppWithApplVerID() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50SP1, "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(enum.ApplVerID_FIX50, "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAppWithApplVerID() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50SP1, "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(enum.ApplVerID_FIX50SP1, "D")
suite.Nil(rej)
}
func (suite *MessageRouterTestSuite) TestRouteFIXTAppWithDefaultApplVerID() {
suite.givenTheRoute(enum.BeginStringFIX42, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50, "D")
suite.givenTheRoute(enum.ApplVerID_FIX50SP1, "D")
suite.givenTargetDefaultApplVerIDForSession(
"8",
SessionID{BeginString: enum.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(enum.ApplVerID_FIX50SP1, "D")
suite.Nil(rej)
}