-
Notifications
You must be signed in to change notification settings - Fork 23
/
stream_test.go
178 lines (150 loc) · 4.04 KB
/
stream_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
package kinesis
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewStream(t *testing.T) {
assert := assert.New(t)
recreateTestStream(t)
tests := []struct {
name string
isSuccess bool
}{
{"foo", false},
{testStreamName, true},
}
svc := getTestClient(t)
for _, tt := range tests {
target := fmt.Sprintf("%+v", tt)
s, err := NewStream(svc, tt.name)
if !tt.isSuccess {
assert.Error(err, target)
assert.Nil(s, target)
continue
}
assert.NoError(err, target)
assert.NotNil(s, target)
assert.Equal(tt.name, s.nameWithPrefix, target)
assert.True(len(s.shardIDs) > 0, target, s.shardIDs)
}
}
func TestGetShardIDs(t *testing.T) {
assert := assert.New(t)
testStreamName2 := "test-stream-2"
svc := getTestClient(t)
svc.CreateStreamWithName(testStreamName2)
recreateTestStream(t)
tests := []struct {
name string
isSuccess bool
}{
{"foo", false},
{"bar", false},
{testStreamName, true},
{testStreamName2, true},
}
for _, tt := range tests {
target := fmt.Sprintf("%+v", tt)
s := &Stream{
service: svc,
nameWithPrefix: tt.name,
}
ids, err := s.GetShardIDs()
if !tt.isSuccess {
assert.Error(err, target)
assert.Nil(ids, target)
continue
}
assert.NoError(err, target)
assert.True(len(ids) > 0, target, ids)
}
}
func TestGetLatestRecords(t *testing.T) {
t.Skip("unstable test")
assert := assert.New(t)
recreateTestStream(t)
s := getTestStream(t)
// put data during GetLatestRecords... :(
go func() {
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
s.PutRecord([]byte("TestGetLatestRecords"))
}()
time.Sleep(1 * time.Millisecond)
list, err := s.GetLatestRecords()
assert.NoError(err)
assert.Len(list, 1)
res := list[0]
assert.True(res.Count > 0)
assert.NotEmpty(res.NextShardIterator)
assert.True(len(res.Items) > 0)
assert.Equal("TestGetLatestRecords", string(res.Items[0].Data))
}
func TestGetRecords(t *testing.T) {
assert := assert.New(t)
recreateTestStream(t)
s := getTestStream(t)
// empty result
s.PutRecord([]byte("TestGetRecords1"))
result, err := s.GetRecords(GetCondition{
ShardID: s.shardIDs[0],
ShardIteratorType: IteratorTypeLatest,
})
assert.NoError(err)
assert.Equal(0, result.Count)
assert.Equal(0, len(result.Items))
// get 1 record from the previous
s.PutRecord([]byte("TestGetRecords2"))
result, err = s.GetRecords(GetCondition{
ShardID: s.shardIDs[0],
ShardIteratorType: IteratorTypeLatest,
ShardIterator: result.NextShardIterator,
})
assert.NoError(err)
assert.Equal(1, result.Count)
assert.Equal(1, len(result.Items))
assert.Equal("TestGetRecords2", string(result.Items[0].Data))
// get 2 record from the beginning
result, err = s.GetRecords(GetCondition{
ShardID: s.shardIDs[0],
ShardIteratorType: IteratorTypeTrimHorizon,
})
assert.NoError(err)
assert.Equal(2, result.Count)
assert.Equal(2, len(result.Items))
assert.Equal("TestGetRecords1", string(result.Items[0].Data))
assert.Equal("TestGetRecords2", string(result.Items[1].Data))
}
func TestPutRecord(t *testing.T) {
assert := assert.New(t)
recreateTestStream(t)
s := getTestStream(t)
// before put
result, err := s.GetRecords(GetCondition{
ShardID: s.shardIDs[0],
ShardIteratorType: IteratorTypeTrimHorizon,
})
assert.NoError(err)
assert.Equal(0, result.Count)
assert.Equal(0, len(result.Items))
// execute put
err = s.PutRecord([]byte("TestPutRecord"))
assert.NoError(err)
// after put
result, err = s.GetRecords(GetCondition{
ShardID: s.shardIDs[0],
ShardIteratorType: IteratorTypeTrimHorizon,
})
assert.NoError(err)
assert.Equal(1, result.Count)
assert.Equal(1, len(result.Items))
assert.Equal("TestPutRecord", string(result.Items[0].Data))
}