1 Star 0 Fork 0

muicx / quickfix

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
message_test.go 7.55 KB
一键复制 编辑 原始数据 按行查看 历史
package quickfix
import (
"bytes"
"reflect"
"testing"
"github.com/quickfixgo/quickfix/datadictionary"
"github.com/stretchr/testify/suite"
)
func BenchmarkParseMessage(b *testing.B) {
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
var msg Message
for i := 0; i < b.N; i++ {
_ = ParseMessage(&msg, rawMsg)
}
}
type MessageSuite struct {
QuickFIXSuite
msg *Message
}
func TestMessageSuite(t *testing.T) {
suite.Run(t, new(MessageSuite))
}
func (s *MessageSuite) SetupTest() {
s.msg = NewMessage()
}
func (s *MessageSuite) TestParseMessageEmpty() {
rawMsg := bytes.NewBufferString("")
err := ParseMessage(s.msg, rawMsg)
s.NotNil(err)
}
func (s *MessageSuite) TestParseMessage() {
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
err := ParseMessage(s.msg, rawMsg)
s.Nil(err)
s.True(bytes.Equal(rawMsg.Bytes(), s.msg.rawMessage.Bytes()), "Expected msg bytes to equal raw bytes")
expectedBodyBytes := []byte("11=10021=140=154=155=TSLA60=00010101-00:00:00.000")
s.True(bytes.Equal(s.msg.bodyBytes, expectedBodyBytes), "Incorrect body bytes, got %s", string(s.msg.bodyBytes))
s.Equal(14, len(s.msg.fields))
msgType, err := s.msg.MsgType()
s.Nil(err)
s.Equal("D", msgType)
s.True(s.msg.IsMsgTypeOf("D"))
s.False(s.msg.IsMsgTypeOf("A"))
}
func (s *MessageSuite) TestParseMessageWithDataDictionary() {
dict := new(datadictionary.DataDictionary)
dict.Header = &datadictionary.MessageDef{
Fields: map[int]*datadictionary.FieldDef{
10030: nil,
},
}
dict.Trailer = &datadictionary.MessageDef{
Fields: map[int]*datadictionary.FieldDef{
5050: nil,
},
}
rawMsg := bytes.NewBufferString("8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=039")
err := ParseMessageWithDataDictionary(s.msg, rawMsg, dict, dict)
s.Nil(err)
s.FieldEquals(Tag(10030), "CUST", s.msg.Header)
s.FieldEquals(Tag(5050), "HELLO", s.msg.Trailer)
}
func (s *MessageSuite) TestParseOutOfOrder() {
//allow fields out of order, save for validation
rawMsg := bytes.NewBufferString("8=FIX.4.09=8135=D11=id21=338=10040=154=155=MSFT34=249=TW52=20140521-22:07:0956=ISLD10=250")
s.Nil(ParseMessage(s.msg, rawMsg))
}
func (s *MessageSuite) TestBuild() {
s.msg.Header.SetField(tagBeginString, FIXString(BeginStringFIX44))
s.msg.Header.SetField(tagMsgType, FIXString("A"))
s.msg.Header.SetField(tagSendingTime, FIXString("20140615-19:49:56"))
s.msg.Body.SetField(Tag(553), FIXString("my_user"))
s.msg.Body.SetField(Tag(554), FIXString("secret"))
expectedBytes := []byte("8=FIX.4.49=4935=A52=20140615-19:49:56553=my_user554=secret10=072")
result := s.msg.build()
s.True(bytes.Equal(expectedBytes, result), "Unexpected bytes, got %s", string(result))
}
func (s *MessageSuite) TestReBuild() {
rawMsg := bytes.NewBufferString("8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039")
s.Nil(ParseMessage(s.msg, rawMsg))
s.msg.Header.SetField(tagOrigSendingTime, FIXString("20140515-19:49:56.659"))
s.msg.Header.SetField(tagSendingTime, FIXString("20140615-19:49:56"))
rebuildBytes := s.msg.build()
expectedBytes := []byte("8=FIX.4.29=12635=D34=249=TW52=20140615-19:49:5656=ISLD122=20140515-19:49:56.65911=10021=140=154=155=TSLA60=00010101-00:00:00.00010=128")
s.True(bytes.Equal(expectedBytes, rebuildBytes), "Unexpected bytes,\n +%s\n-%s", rebuildBytes, expectedBytes)
expectedBodyBytes := []byte("11=10021=140=154=155=TSLA60=00010101-00:00:00.000")
s.True(bytes.Equal(s.msg.bodyBytes, expectedBodyBytes), "Incorrect body bytes, got %s", string(s.msg.bodyBytes))
}
func (s *MessageSuite) TestReverseRoute() {
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
builder := s.msg.reverseRoute()
var testCases = []struct {
tag Tag
expectedValue string
}{
{tagTargetCompID, "TW"},
{tagTargetSubID, "KK"},
{tagTargetLocationID, "JV"},
{tagSenderCompID, "ISLD"},
{tagSenderSubID, "AP"},
{tagSenderLocationID, "RY"},
{tagDeliverToCompID, "JCD"},
{tagDeliverToSubID, "CS"},
{tagDeliverToLocationID, "BB"},
{tagOnBehalfOfCompID, "MG"},
{tagOnBehalfOfSubID, "CB"},
{tagOnBehalfOfLocationID, "BH"},
}
for _, tc := range testCases {
var field FIXString
s.Nil(builder.Header.GetField(tc.tag, &field))
s.Equal(tc.expectedValue, string(field))
}
}
func (s *MessageSuite) TestReverseRouteIgnoreEmpty() {
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=12835=D34=249=TW52=20060102-15:04:0556=ISLD115=116=CS128=MG129=CB11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
builder := s.msg.reverseRoute()
s.False(builder.Header.Has(tagDeliverToCompID), "Should not reverse if empty")
}
func (s *MessageSuite) TestReverseRouteFIX40() {
//onbehalfof/deliverto location id not supported in fix 4.0
s.Nil(ParseMessage(s.msg, bytes.NewBufferString("8=FIX.4.09=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123")))
builder := s.msg.reverseRoute()
s.False(builder.Header.Has(tagDeliverToLocationID), "delivertolocation id not supported in fix40")
s.False(builder.Header.Has(tagOnBehalfOfLocationID), "onbehalfof location id not supported in fix40")
}
func (s *MessageSuite) TestCopyIntoMessage() {
msgString := "8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP144=BB115=JCD116=CS128=MG129=CB142=JV143=RY145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=123"
msgBuf := bytes.NewBufferString(msgString)
s.Nil(ParseMessage(s.msg, msgBuf))
dest := NewMessage()
s.msg.CopyInto(dest)
checkFieldInt(s, dest.Header.FieldMap, int(tagMsgSeqNum), 2)
checkFieldInt(s, dest.Body.FieldMap, 21, 3)
checkFieldString(s, dest.Body.FieldMap, 11, "ID")
s.Equal(len(dest.bodyBytes), len(s.msg.bodyBytes))
// copying decouples the message from its input buffer, so the raw message will be re-rendered
renderedString := "8=FIX.4.29=17135=D34=249=TW50=KK52=20060102-15:04:0556=ISLD57=AP115=JCD116=CS128=MG129=CB142=JV143=RY144=BB145=BH11=ID21=338=10040=w54=155=INTC60=20060102-15:04:0510=033"
s.Equal(dest.String(), renderedString)
s.True(reflect.DeepEqual(s.msg.bodyBytes, dest.bodyBytes))
s.True(s.msg.IsMsgTypeOf("D"))
s.Equal(s.msg.ReceiveTime, dest.ReceiveTime)
s.True(reflect.DeepEqual(s.msg.fields, dest.fields))
// update the source message to validate the copy is truly deep
newMsgString := "8=FIX.4.49=4935=A52=20140615-19:49:56553=my_user554=secret10=072"
s.Nil(ParseMessage(s.msg, bytes.NewBufferString(newMsgString)))
s.True(s.msg.IsMsgTypeOf("A"))
s.Equal(s.msg.String(), newMsgString)
// clear the source buffer also
msgBuf.Reset()
s.True(dest.IsMsgTypeOf("D"))
s.Equal(dest.String(), renderedString)
}
func checkFieldInt(s *MessageSuite, fields FieldMap, tag, expected int) {
toCheck, _ := fields.GetInt(Tag(tag))
s.Equal(expected, toCheck)
}
func checkFieldString(s *MessageSuite, fields FieldMap, tag int, expected string) {
toCheck, err := fields.GetString(Tag(tag))
s.NoError(err)
s.Equal(expected, toCheck)
}
Go
1
https://gitee.com/bradhuang/quickfixgo.git
git@gitee.com:bradhuang/quickfixgo.git
bradhuang
quickfixgo
quickfix
dependabot/go_modules/github.com/mattn/go-sqlite3-1.14.9

搜索帮助