forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
128 lines (99 loc) · 3.38 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
package quickfix
import (
"bytes"
"testing"
)
func TestMemoryStore_IncMsgSeqNum(t *testing.T) {
store, _ := NewMemoryStoreFactory().Create(SessionID{})
var testCases = []struct {
expectedNextSeqNum int
}{
{1}, {2}, {3},
}
for _, tc := range testCases {
if store.NextSenderMsgSeqNum() != tc.expectedNextSeqNum {
t.Errorf("Did not get expected sender seq num %v, got %v", tc.expectedNextSeqNum, store.NextSenderMsgSeqNum())
}
store.IncrNextSenderMsgSeqNum()
if store.NextTargetMsgSeqNum() != tc.expectedNextSeqNum {
t.Errorf("Did not get expected target seq num %v, got %v", tc.expectedNextSeqNum, store.NextTargetMsgSeqNum())
}
store.IncrNextTargetMsgSeqNum()
}
}
func TestMemoryStore_SetMsgSeqNum(t *testing.T) {
store, _ := NewMemoryStoreFactory().Create(SessionID{})
store.SetNextSenderMsgSeqNum(50)
store.SetNextTargetMsgSeqNum(30)
if store.NextSenderMsgSeqNum() != 50 {
t.Errorf("Did not get expected sender seq num %v, got %v", 50, store.NextSenderMsgSeqNum())
}
if store.NextTargetMsgSeqNum() != 30 {
t.Errorf("Did not get expected target seq num %v, got %v", 30, store.NextTargetMsgSeqNum())
}
}
func TestMemoryStore_GetMessages(t *testing.T) {
store, _ := NewMemoryStoreFactory().Create(SessionID{})
messages := store.GetMessages(1, 2)
msg, ok := <-messages
if ok != false {
t.Error("Did not expect messages from empty store", msg)
}
store.SaveMessage(1, []byte("hello"))
store.SaveMessage(2, []byte("cruel"))
store.SaveMessage(3, []byte("world"))
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{}},
}
for _, tc := range testCases {
messages = store.GetMessages(tc.beginSeqNo, tc.endSeqNo)
expected := tc.expectedBytes
for {
msg, ok = <-messages
if len(expected) == 0 {
if ok == true {
t.Error("Did not expect additional messages", msg)
}
break
}
if ok != true {
t.Error("Did not get messages, expected ", expected[0])
}
if !bytes.Equal(msg, expected[0]) {
t.Error("Expected ", expected[0], " got ", msg)
}
expected = expected[1:]
}
}
}
func TestMemoryStore_Reset(t *testing.T) {
store, _ := NewMemoryStoreFactory().Create(SessionID{})
store.SetNextSenderMsgSeqNum(50)
store.SetNextTargetMsgSeqNum(30)
store.SaveMessage(1, []byte("hello"))
store.SaveMessage(2, []byte("cruel"))
store.SaveMessage(3, []byte("world"))
store.Reset()
messages := store.GetMessages(1, 3)
msg, ok := <-messages
if ok {
t.Error("Did not expect messages, got ", string(msg))
}
if store.NextSenderMsgSeqNum() != 1 {
t.Error("SenderMsgSeqNum should reset")
}
if store.NextTargetMsgSeqNum() != 1 {
t.Error("TargetMsgSeqNum should reset")
}
}