Skip to content

Commit 84b2956

Browse files
committed
Refactor external stores into packages
1 parent 5290cae commit 84b2956

File tree

12 files changed

+396
-223
lines changed

12 files changed

+396
-223
lines changed

store_test.go renamed to internal/testsuite/store_suite.go

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,99 +13,84 @@
1313
// Contact [email protected] if any conditions of this licensing
1414
// are not clear to you.
1515

16-
package quickfix
16+
package testsuite
1717

1818
import (
19-
"testing"
2019
"time"
2120

21+
"github.com/quickfixgo/quickfix"
2222
"github.com/stretchr/testify/assert"
2323
"github.com/stretchr/testify/require"
2424
"github.com/stretchr/testify/suite"
2525
)
2626

27-
// MessageStoreTestSuite is the suite of all tests that should be run against all MessageStore implementations.
28-
type MessageStoreTestSuite struct {
27+
type StoreTestSuite struct {
2928
suite.Suite
30-
msgStore MessageStore
29+
MsgStore quickfix.MessageStore
3130
}
3231

33-
// MemoryStoreTestSuite runs all tests in the MessageStoreTestSuite against the MemoryStore implementation.
34-
type MemoryStoreTestSuite struct {
35-
MessageStoreTestSuite
36-
}
37-
38-
func (suite *MemoryStoreTestSuite) SetupTest() {
39-
var err error
40-
suite.msgStore, err = NewMemoryStoreFactory().Create(SessionID{})
41-
require.Nil(suite.T(), err)
42-
}
43-
44-
func TestMemoryStoreTestSuite(t *testing.T) {
45-
suite.Run(t, new(MemoryStoreTestSuite))
46-
}
4732

48-
func (s *MessageStoreTestSuite) TestMessageStore_SetNextMsgSeqNum_Refresh_IncrNextMsgSeqNum() {
33+
func (s *StoreTestSuite) TestMessageStore_SetNextMsgSeqNum_Refresh_IncrNextMsgSeqNum() {
4934
// Given a MessageStore with the following sender and target seqnums
50-
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(867))
51-
s.Require().Nil(s.msgStore.SetNextTargetMsgSeqNum(5309))
35+
s.Require().Nil(s.MsgStore.SetNextSenderMsgSeqNum(867))
36+
s.Require().Nil(s.MsgStore.SetNextTargetMsgSeqNum(5309))
5237

5338
// When the store is refreshed from its backing store
54-
s.Require().Nil(s.msgStore.Refresh())
39+
s.Require().Nil(s.MsgStore.Refresh())
5540

5641
// Then the sender and target seqnums should still be
57-
s.Equal(867, s.msgStore.NextSenderMsgSeqNum())
58-
s.Equal(5309, s.msgStore.NextTargetMsgSeqNum())
42+
s.Equal(867, s.MsgStore.NextSenderMsgSeqNum())
43+
s.Equal(5309, s.MsgStore.NextTargetMsgSeqNum())
5944

6045
// When the sender and target seqnums are incremented
61-
s.Require().Nil(s.msgStore.IncrNextSenderMsgSeqNum())
62-
s.Require().Nil(s.msgStore.IncrNextTargetMsgSeqNum())
46+
s.Require().Nil(s.MsgStore.IncrNextSenderMsgSeqNum())
47+
s.Require().Nil(s.MsgStore.IncrNextTargetMsgSeqNum())
6348

6449
// Then the sender and target seqnums should be
65-
s.Equal(868, s.msgStore.NextSenderMsgSeqNum())
66-
s.Equal(5310, s.msgStore.NextTargetMsgSeqNum())
50+
s.Equal(868, s.MsgStore.NextSenderMsgSeqNum())
51+
s.Equal(5310, s.MsgStore.NextTargetMsgSeqNum())
6752

6853
// When the store is refreshed from its backing store
69-
s.Require().Nil(s.msgStore.Refresh())
54+
s.Require().Nil(s.MsgStore.Refresh())
7055

7156
// Then the sender and target seqnums should still be
72-
s.Equal(868, s.msgStore.NextSenderMsgSeqNum())
73-
s.Equal(5310, s.msgStore.NextTargetMsgSeqNum())
57+
s.Equal(868, s.MsgStore.NextSenderMsgSeqNum())
58+
s.Equal(5310, s.MsgStore.NextTargetMsgSeqNum())
7459
}
7560

76-
func (s *MessageStoreTestSuite) TestMessageStore_Reset() {
61+
func (s *StoreTestSuite) TestMessageStore_Reset() {
7762
// Given a MessageStore with the following sender and target seqnums
78-
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(1234))
79-
s.Require().Nil(s.msgStore.SetNextTargetMsgSeqNum(5678))
63+
s.Require().Nil(s.MsgStore.SetNextSenderMsgSeqNum(1234))
64+
s.Require().Nil(s.MsgStore.SetNextTargetMsgSeqNum(5678))
8065

8166
// When the store is reset
82-
s.Require().Nil(s.msgStore.Reset())
67+
s.Require().Nil(s.MsgStore.Reset())
8368

8469
// Then the sender and target seqnums should be
85-
s.Equal(1, s.msgStore.NextSenderMsgSeqNum())
86-
s.Equal(1, s.msgStore.NextTargetMsgSeqNum())
70+
s.Equal(1, s.MsgStore.NextSenderMsgSeqNum())
71+
s.Equal(1, s.MsgStore.NextTargetMsgSeqNum())
8772

8873
// When the store is refreshed from its backing store
89-
s.Require().Nil(s.msgStore.Refresh())
74+
s.Require().Nil(s.MsgStore.Refresh())
9075

9176
// Then the sender and target seqnums should still be
92-
s.Equal(1, s.msgStore.NextSenderMsgSeqNum())
93-
s.Equal(1, s.msgStore.NextTargetMsgSeqNum())
77+
s.Equal(1, s.MsgStore.NextSenderMsgSeqNum())
78+
s.Equal(1, s.MsgStore.NextTargetMsgSeqNum())
9479
}
9580

96-
func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
81+
func (s *StoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
9782
// Given the following saved messages
9883
expectedMsgsBySeqNum := map[int]string{
9984
1: "In the frozen land of Nador",
10085
2: "they were forced to eat Robin's minstrels",
10186
3: "and there was much rejoicing",
10287
}
10388
for seqNum, msg := range expectedMsgsBySeqNum {
104-
s.Require().Nil(s.msgStore.SaveMessage(seqNum, []byte(msg)))
89+
s.Require().Nil(s.MsgStore.SaveMessage(seqNum, []byte(msg)))
10590
}
10691

10792
// When the messages are retrieved from the MessageStore
108-
actualMsgs, err := s.msgStore.GetMessages(1, 3)
93+
actualMsgs, err := s.MsgStore.GetMessages(1, 3)
10994
s.Require().Nil(err)
11095

11196
// Then the messages should be
@@ -115,10 +100,10 @@ func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
115100
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
116101

117102
// When the store is refreshed from its backing store
118-
s.Require().Nil(s.msgStore.Refresh())
103+
s.Require().Nil(s.MsgStore.Refresh())
119104

120105
// And the messages are retrieved from the MessageStore
121-
actualMsgs, err = s.msgStore.GetMessages(1, 3)
106+
actualMsgs, err = s.MsgStore.GetMessages(1, 3)
122107
s.Require().Nil(err)
123108

124109
// Then the messages should still be
@@ -128,8 +113,8 @@ func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_GetMessage() {
128113
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
129114
}
130115

131-
func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMessage() {
132-
s.Require().Nil(s.msgStore.SetNextSenderMsgSeqNum(420))
116+
func (s *StoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMessage() {
117+
s.Require().Nil(s.MsgStore.SetNextSenderMsgSeqNum(420))
133118

134119
// Given the following saved messages
135120
expectedMsgsBySeqNum := map[int]string{
@@ -138,12 +123,12 @@ func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMes
138123
3: "and there was much rejoicing",
139124
}
140125
for seqNum, msg := range expectedMsgsBySeqNum {
141-
s.Require().Nil(s.msgStore.SaveMessageAndIncrNextSenderMsgSeqNum(seqNum, []byte(msg)))
126+
s.Require().Nil(s.MsgStore.SaveMessageAndIncrNextSenderMsgSeqNum(seqNum, []byte(msg)))
142127
}
143-
s.Equal(423, s.msgStore.NextSenderMsgSeqNum())
128+
s.Equal(423, s.MsgStore.NextSenderMsgSeqNum())
144129

145130
// When the messages are retrieved from the MessageStore
146-
actualMsgs, err := s.msgStore.GetMessages(1, 3)
131+
actualMsgs, err := s.MsgStore.GetMessages(1, 3)
147132
s.Require().Nil(err)
148133

149134
// Then the messages should be
@@ -153,13 +138,13 @@ func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMes
153138
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
154139

155140
// When the store is refreshed from its backing store
156-
s.Require().Nil(s.msgStore.Refresh())
141+
s.Require().Nil(s.MsgStore.Refresh())
157142

158143
// And the messages are retrieved from the MessageStore
159-
actualMsgs, err = s.msgStore.GetMessages(1, 3)
144+
actualMsgs, err = s.MsgStore.GetMessages(1, 3)
160145
s.Require().Nil(err)
161146

162-
s.Equal(423, s.msgStore.NextSenderMsgSeqNum())
147+
s.Equal(423, s.MsgStore.NextSenderMsgSeqNum())
163148

164149
// Then the messages should still be
165150
s.Require().Len(actualMsgs, 3)
@@ -168,22 +153,22 @@ func (s *MessageStoreTestSuite) TestMessageStore_SaveMessage_AndIncrement_GetMes
168153
s.Equal(expectedMsgsBySeqNum[3], string(actualMsgs[2]))
169154
}
170155

171-
func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_EmptyStore() {
156+
func (s *StoreTestSuite) TestMessageStore_GetMessages_EmptyStore() {
172157
// When messages are retrieved from an empty store
173-
messages, err := s.msgStore.GetMessages(1, 2)
158+
messages, err := s.MsgStore.GetMessages(1, 2)
174159
require.Nil(s.T(), err)
175160

176161
// Then no messages should be returned
177162
require.Empty(s.T(), messages, "Did not expect messages from empty store")
178163
}
179164

180-
func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
165+
func (s *StoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
181166
t := s.T()
182167

183168
// Given the following saved messages
184-
require.Nil(t, s.msgStore.SaveMessage(1, []byte("hello")))
185-
require.Nil(t, s.msgStore.SaveMessage(2, []byte("cruel")))
186-
require.Nil(t, s.msgStore.SaveMessage(3, []byte("world")))
169+
require.Nil(t, s.MsgStore.SaveMessage(1, []byte("hello")))
170+
require.Nil(t, s.MsgStore.SaveMessage(2, []byte("cruel")))
171+
require.Nil(t, s.MsgStore.SaveMessage(3, []byte("world")))
187172

188173
// When the following requests are made to the store
189174
var testCases = []struct {
@@ -203,7 +188,7 @@ func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
203188

204189
// Then the returned messages should be
205190
for _, tc := range testCases {
206-
actualMsgs, err := s.msgStore.GetMessages(tc.beginSeqNo, tc.endSeqNo)
191+
actualMsgs, err := s.MsgStore.GetMessages(tc.beginSeqNo, tc.endSeqNo)
207192
require.Nil(t, err)
208193
require.Len(t, actualMsgs, len(tc.expectedBytes))
209194
for i, expectedMsg := range tc.expectedBytes {
@@ -212,12 +197,12 @@ func (s *MessageStoreTestSuite) TestMessageStore_GetMessages_VariousRanges() {
212197
}
213198
}
214199

215-
func (s *MessageStoreTestSuite) TestMessageStore_CreationTime() {
216-
s.False(s.msgStore.CreationTime().IsZero())
200+
func (s *StoreTestSuite) TestMessageStore_CreationTime() {
201+
s.False(s.MsgStore.CreationTime().IsZero())
217202

218203
t0 := time.Now()
219-
s.Require().Nil(s.msgStore.Reset())
204+
s.Require().Nil(s.MsgStore.Reset())
220205
t1 := time.Now()
221-
s.Require().True(s.msgStore.CreationTime().After(t0))
222-
s.Require().True(s.msgStore.CreationTime().Before(t1))
206+
s.Require().True(s.MsgStore.CreationTime().After(t0))
207+
s.Require().True(s.MsgStore.CreationTime().Before(t1))
223208
}

memorystore.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) quickfixengine.org All rights reserved.
2+
//
3+
// This file may be distributed under the terms of the quickfixengine.org
4+
// license as defined by quickfixengine.org and appearing in the file
5+
// LICENSE included in the packaging of this file.
6+
//
7+
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
8+
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
9+
// PARTICULAR PURPOSE.
10+
//
11+
// See http://www.quickfixengine.org/LICENSE for licensing information.
12+
//
13+
// Contact [email protected] if any conditions of this licensing
14+
// are not clear to you.
15+
16+
package quickfix
17+
18+
import (
19+
"time"
20+
21+
"github.com/pkg/errors"
22+
)
23+
24+
25+
type memoryStore struct {
26+
senderMsgSeqNum, targetMsgSeqNum int
27+
creationTime time.Time
28+
messageMap map[int][]byte
29+
}
30+
31+
func (store *memoryStore) NextSenderMsgSeqNum() int {
32+
return store.senderMsgSeqNum + 1
33+
}
34+
35+
func (store *memoryStore) NextTargetMsgSeqNum() int {
36+
return store.targetMsgSeqNum + 1
37+
}
38+
39+
func (store *memoryStore) IncrNextSenderMsgSeqNum() error {
40+
store.senderMsgSeqNum++
41+
return nil
42+
}
43+
44+
func (store *memoryStore) IncrNextTargetMsgSeqNum() error {
45+
store.targetMsgSeqNum++
46+
return nil
47+
}
48+
49+
func (store *memoryStore) SetNextSenderMsgSeqNum(nextSeqNum int) error {
50+
store.senderMsgSeqNum = nextSeqNum - 1
51+
return nil
52+
}
53+
func (store *memoryStore) SetNextTargetMsgSeqNum(nextSeqNum int) error {
54+
store.targetMsgSeqNum = nextSeqNum - 1
55+
return nil
56+
}
57+
58+
func (store *memoryStore) CreationTime() time.Time {
59+
return store.creationTime
60+
}
61+
62+
func (store *memoryStore) SetCreationTime(t time.Time) {
63+
store.creationTime = t
64+
}
65+
66+
func (store *memoryStore) Reset() error {
67+
store.senderMsgSeqNum = 0
68+
store.targetMsgSeqNum = 0
69+
store.creationTime = time.Now()
70+
store.messageMap = nil
71+
return nil
72+
}
73+
74+
func (store *memoryStore) Refresh() error {
75+
// NOP, nothing to refresh.
76+
return nil
77+
}
78+
79+
func (store *memoryStore) Close() error {
80+
// NOP, nothing to close.
81+
return nil
82+
}
83+
84+
func (store *memoryStore) SaveMessage(seqNum int, msg []byte) error {
85+
if store.messageMap == nil {
86+
store.messageMap = make(map[int][]byte)
87+
}
88+
89+
store.messageMap[seqNum] = msg
90+
return nil
91+
}
92+
93+
func (store *memoryStore) SaveMessageAndIncrNextSenderMsgSeqNum(seqNum int, msg []byte) error {
94+
err := store.SaveMessage(seqNum, msg)
95+
if err != nil {
96+
return err
97+
}
98+
return store.IncrNextSenderMsgSeqNum()
99+
}
100+
101+
func (store *memoryStore) GetMessages(beginSeqNum, endSeqNum int) ([][]byte, error) {
102+
var msgs [][]byte
103+
for seqNum := beginSeqNum; seqNum <= endSeqNum; seqNum++ {
104+
if m, ok := store.messageMap[seqNum]; ok {
105+
msgs = append(msgs, m)
106+
}
107+
}
108+
return msgs, nil
109+
}
110+
111+
type memoryStoreFactory struct{}
112+
113+
func (f memoryStoreFactory) Create(sessionID SessionID) (MessageStore, error) {
114+
m := new(memoryStore)
115+
if err := m.Reset(); err != nil {
116+
return m, errors.Wrap(err, "reset")
117+
}
118+
return m, nil
119+
}
120+
121+
// NewMemoryStoreFactory returns a MessageStoreFactory instance that created in-memory MessageStores.
122+
func NewMemoryStoreFactory() MessageStoreFactory { return memoryStoreFactory{} }

session_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,15 @@ func (s *SessionSuite) TestCheckSessionTimeInRange() {
346346
s.session.State = test.before
347347

348348
now := time.Now().UTC()
349-
store := new(memoryStore)
349+
memStore, memErr := NewMemoryStoreFactory().Create(s.sessionID)
350+
s.Require().Nil(memErr)
351+
350352
if test.before.IsSessionTime() {
351-
s.Require().Nil(store.Reset())
353+
s.Require().Nil(memStore.Reset())
352354
} else {
353-
store.creationTime = now.Add(time.Duration(-1) * time.Minute)
355+
memStore.SetCreationTime(now.Add(time.Duration(-1) * time.Minute))
354356
}
355-
s.session.store = store
357+
s.session.store = memStore
356358
s.IncrNextSenderMsgSeqNum()
357359
s.IncrNextTargetMsgSeqNum()
358360

0 commit comments

Comments
 (0)