forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
223 lines (182 loc) · 7.78 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
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
// 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 [email protected] if any conditions of this licensing
// are not clear to you.
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 (s *MessageStoreTestSuite) TestMessageStore_SetNextMsgSeqNum_Refresh_IncrNextMsgSeqNum() {
// Given a MessageStore with the following sender and target seqnums
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(867))
s.Require().Nil(s.msgStore.SetNextTargetMsgSeqNum(5309))
// When the store is refreshed from its backing store
s.Require().Nil(s.msgStore.Refresh())
// Then the sender and target seqnums should still be
s.Equal(867, s.msgStore.NextSenderMsgSeqNum())
s.Equal(5309, s.msgStore.NextTargetMsgSeqNum())
// When the sender and target seqnums are incremented
s.Require().Nil(s.msgStore.IncrNextSenderMsgSeqNum())
s.Require().Nil(s.msgStore.IncrNextTargetMsgSeqNum())
// Then the sender and target seqnums should be
s.Equal(868, s.msgStore.NextSenderMsgSeqNum())
s.Equal(5310, s.msgStore.NextTargetMsgSeqNum())
// When the store is refreshed from its backing store
s.Require().Nil(s.msgStore.Refresh())
// Then the sender and target seqnums should still be
s.Equal(868, s.msgStore.NextSenderMsgSeqNum())
s.Equal(5310, s.msgStore.NextTargetMsgSeqNum())
}
func (s *MessageStoreTestSuite) TestMessageStore_Reset() {
// Given a MessageStore with the following sender and target seqnums
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(1234))
s.Require().Nil(s.msgStore.SetNextTargetMsgSeqNum(5678))
// When the store is reset
s.Require().Nil(s.msgStore.Reset())
// Then the sender and target seqnums should be
s.Equal(1, s.msgStore.NextSenderMsgSeqNum())
s.Equal(1, s.msgStore.NextTargetMsgSeqNum())
// When the store is refreshed from its backing store
s.Require().Nil(s.msgStore.Refresh())
// Then the sender and target seqnums should still be
s.Equal(1, s.msgStore.NextSenderMsgSeqNum())
s.Equal(1, s.msgStore.NextTargetMsgSeqNum())
}
func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
// 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 {
s.Require().Nil(s.msgStore.SaveMessage(seqNum, []byte(msg)))
}
// When the messages are retrieved from the MessageStore
actualMsgs, err := s.msgStore.GetMessages(1, 3)
s.Require().Nil(err)
// Then the messages should be
s.Require().Len(actualMsgs, 3)
s.Equal(expectedMsgsBySeqNum[1], string(actualMsgs[0]))
s.Equal(expectedMsgsBySeqNum[2], string(actualMsgs[1]))
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
// When the store is refreshed from its backing store
s.Require().Nil(s.msgStore.Refresh())
// And the messages are retrieved from the MessageStore
actualMsgs, err = s.msgStore.GetMessages(1, 3)
s.Require().Nil(err)
// Then the messages should still be
s.Require().Len(actualMsgs, 3)
s.Equal(expectedMsgsBySeqNum[1], string(actualMsgs[0]))
s.Equal(expectedMsgsBySeqNum[2], string(actualMsgs[1]))
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
}
func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMessage() {
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(420))
// 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 {
s.Require().Nil(s.msgStore.SaveMessageAndIncrNextSenderMsgSeqNum(seqNum, []byte(msg)))
}
s.Equal(423, s.msgStore.NextSenderMsgSeqNum())
// When the messages are retrieved from the MessageStore
actualMsgs, err := s.msgStore.GetMessages(1, 3)
s.Require().Nil(err)
// Then the messages should be
s.Require().Len(actualMsgs, 3)
s.Equal(expectedMsgsBySeqNum[1], string(actualMsgs[0]))
s.Equal(expectedMsgsBySeqNum[2], string(actualMsgs[1]))
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
// When the store is refreshed from its backing store
s.Require().Nil(s.msgStore.Refresh())
// And the messages are retrieved from the MessageStore
actualMsgs, err = s.msgStore.GetMessages(1, 3)
s.Require().Nil(err)
s.Equal(423, s.msgStore.NextSenderMsgSeqNum())
// Then the messages should still be
s.Require().Len(actualMsgs, 3)
s.Equal(expectedMsgsBySeqNum[1], string(actualMsgs[0]))
s.Equal(expectedMsgsBySeqNum[2], string(actualMsgs[1]))
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
}
func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_EmptyStore() {
// When messages are retrieved from an empty store
messages, err := s.msgStore.GetMessages(1, 2)
require.Nil(s.T(), err)
// Then no messages should be returned
require.Empty(s.T(), messages, "Did not expect messages from empty store")
}
func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
t := s.T()
// Given the following saved messages
require.Nil(t, s.msgStore.SaveMessage(1, []byte("hello")))
require.Nil(t, s.msgStore.SaveMessage(2, []byte("cruel")))
require.Nil(t, s.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 := s.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 (s *MessageStoreTestSuite) TestMessageStore_CreationTime() {
s.False(s.msgStore.CreationTime().IsZero())
t0 := time.Now()
s.Require().Nil(s.msgStore.Reset())
t1 := time.Now()
s.Require().True(s.msgStore.CreationTime().After(t0))
s.Require().True(s.msgStore.CreationTime().Before(t1))
}