forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
174 lines (139 loc) · 6.02 KB
/
store_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
package quickfix
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
// MessageStoreTestSuite is the suite of all tests that should be run against all MessageStore implementations
type MessageStoreTestSuite struct {
suite.Suite
msgStore MessageStore
}
// MemoryStoreTestSuite runs all tests in the MessageStoreTestSuite against the MemoryStore implementation
type MemoryStoreTestSuite struct {
MessageStoreTestSuite
}
func (suite *MemoryStoreTestSuite) SetupTest() {
var err error
suite.msgStore, err = NewMemoryStoreFactory().Create(SessionID{})
require.Nil(suite.T(), err)
}
func TestMemoryStoreTestSuite(t *testing.T) {
suite.Run(t, new(MemoryStoreTestSuite))
}
func (suite *MessageStoreTestSuite) TestMessageStore_SetNextMsgSeqNum_Refresh_IncrNextMsgSeqNum() {
t := suite.T()
// Given a MessageStore with the following sender and target seqnums
suite.msgStore.SetNextSenderMsgSeqNum(867)
suite.msgStore.SetNextTargetMsgSeqNum(5309)
// When the store is refreshed from its backing store
suite.msgStore.Refresh()
// Then the sender and target seqnums should still be
assert.Equal(t, 867, suite.msgStore.NextSenderMsgSeqNum())
assert.Equal(t, 5309, suite.msgStore.NextTargetMsgSeqNum())
// When the sender and target seqnums are incremented
require.Nil(t, suite.msgStore.IncrNextSenderMsgSeqNum())
require.Nil(t, suite.msgStore.IncrNextTargetMsgSeqNum())
// Then the sender and target seqnums should be
assert.Equal(t, 868, suite.msgStore.NextSenderMsgSeqNum())
assert.Equal(t, 5310, suite.msgStore.NextTargetMsgSeqNum())
// When the store is refreshed from its backing store
suite.msgStore.Refresh()
// Then the sender and target seqnums should still be
assert.Equal(t, 868, suite.msgStore.NextSenderMsgSeqNum())
assert.Equal(t, 5310, suite.msgStore.NextTargetMsgSeqNum())
}
func (suite *MessageStoreTestSuite) TestMessageStore_Reset() {
t := suite.T()
// Given a MessageStore with the following sender and target seqnums
suite.msgStore.SetNextSenderMsgSeqNum(1234)
suite.msgStore.SetNextTargetMsgSeqNum(5678)
// When the store is reset
require.Nil(t, suite.msgStore.Reset())
// Then the sender and target seqnums should be
assert.Equal(t, 1, suite.msgStore.NextSenderMsgSeqNum())
assert.Equal(t, 1, suite.msgStore.NextTargetMsgSeqNum())
// When the store is refreshed from its backing store
suite.msgStore.Refresh()
// Then the sender and target seqnums should still be
assert.Equal(t, 1, suite.msgStore.NextSenderMsgSeqNum())
assert.Equal(t, 1, suite.msgStore.NextTargetMsgSeqNum())
}
func (suite *MessageStoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
t := suite.T()
// Given the following saved messages
expectedMsgsBySeqNum := map[int]string{
1: "In the frozen land of Nador",
2: "they were forced to eat Robin's minstrels",
3: "and there was much rejoicing",
}
for seqNum, msg := range expectedMsgsBySeqNum {
require.Nil(t, suite.msgStore.SaveMessage(seqNum, []byte(msg)))
}
// When the messages are retrieved from the MessageStore
actualMsgs, err := suite.msgStore.GetMessages(1, 3)
require.Nil(t, err)
// Then the messages should be
require.Len(t, actualMsgs, 3)
assert.Equal(t, expectedMsgsBySeqNum[1], string(actualMsgs[0]))
assert.Equal(t, expectedMsgsBySeqNum[2], string(actualMsgs[1]))
assert.Equal(t, expectedMsgsBySeqNum[3], string(actualMsgs[2]))
// When the store is refreshed from its backing store
suite.msgStore.Refresh()
// And the messages are retrieved from the MessageStore
actualMsgs, err = suite.msgStore.GetMessages(1, 3)
require.Nil(t, err)
// Then the messages should still be
require.Len(t, actualMsgs, 3)
assert.Equal(t, expectedMsgsBySeqNum[1], string(actualMsgs[0]))
assert.Equal(t, expectedMsgsBySeqNum[2], string(actualMsgs[1]))
assert.Equal(t, expectedMsgsBySeqNum[3], string(actualMsgs[2]))
}
func (suite *MessageStoreTestSuite) TestMessageStore_GetMessages_EmptyStore() {
// When messages are retrieved from an empty store
messages, err := suite.msgStore.GetMessages(1, 2)
require.Nil(suite.T(), err)
// Then no messages should be returned
require.Empty(suite.T(), messages, "Did not expect messages from empty store")
}
func (suite *MessageStoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
t := suite.T()
// Given the following saved messages
require.Nil(t, suite.msgStore.SaveMessage(1, []byte("hello")))
require.Nil(t, suite.msgStore.SaveMessage(2, []byte("cruel")))
require.Nil(t, suite.msgStore.SaveMessage(3, []byte("world")))
// When the following requests are made to the store
var testCases = []struct {
beginSeqNo, endSeqNo int
expectedBytes [][]byte
}{
{beginSeqNo: 1, endSeqNo: 1, expectedBytes: [][]byte{[]byte("hello")}},
{beginSeqNo: 1, endSeqNo: 2, expectedBytes: [][]byte{[]byte("hello"), []byte("cruel")}},
{beginSeqNo: 1, endSeqNo: 3, expectedBytes: [][]byte{[]byte("hello"), []byte("cruel"), []byte("world")}},
{beginSeqNo: 1, endSeqNo: 4, expectedBytes: [][]byte{[]byte("hello"), []byte("cruel"), []byte("world")}},
{beginSeqNo: 2, endSeqNo: 3, expectedBytes: [][]byte{[]byte("cruel"), []byte("world")}},
{beginSeqNo: 3, endSeqNo: 3, expectedBytes: [][]byte{[]byte("world")}},
{beginSeqNo: 3, endSeqNo: 4, expectedBytes: [][]byte{[]byte("world")}},
{beginSeqNo: 4, endSeqNo: 4, expectedBytes: [][]byte{}},
{beginSeqNo: 4, endSeqNo: 10, expectedBytes: [][]byte{}},
}
// Then the returned messages should be
for _, tc := range testCases {
actualMsgs, err := suite.msgStore.GetMessages(tc.beginSeqNo, tc.endSeqNo)
require.Nil(t, err)
require.Len(t, actualMsgs, len(tc.expectedBytes))
for i, expectedMsg := range tc.expectedBytes {
assert.Equal(t, string(expectedMsg), string(actualMsgs[i]))
}
}
}
func (suite *MessageStoreTestSuite) TestMessageStore_CreationTime() {
assert.False(suite.T(), suite.msgStore.CreationTime().IsZero())
t0 := time.Now()
suite.msgStore.Reset()
t1 := time.Now()
require.True(suite.T(), suite.msgStore.CreationTime().After(t0))
require.True(suite.T(), suite.msgStore.CreationTime().Before(t1))
}