-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsubscription_test.go
More file actions
284 lines (247 loc) · 7.31 KB
/
Copy pathsubscription_test.go
File metadata and controls
284 lines (247 loc) · 7.31 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hub
import (
"sync"
"testing"
"time"
"github.com/streamingfast/bstream"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testHandler is a mock handler that records all ProcessBlock calls
type testHandler struct {
mu sync.Mutex
calls []handlerCall
err error
}
type handlerCall struct {
blockNum uint64
blockID string
step bstream.StepType
partialIndex int32
}
func (h *testHandler) ProcessBlock(block *pbbstream.Block, obj any) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.err != nil {
return h.err
}
stepable := obj.(bstream.Stepable)
call := handlerCall{
blockNum: block.Number,
blockID: block.Id,
step: stepable.Step(),
partialIndex: block.PartialIndex,
}
h.calls = append(h.calls, call)
return nil
}
func (h *testHandler) getCalls() []handlerCall {
h.mu.Lock()
defer h.mu.Unlock()
return append([]handlerCall(nil), h.calls...)
}
func (h *testHandler) reset() {
h.mu.Lock()
defer h.mu.Unlock()
h.calls = nil
h.err = nil
}
// createPartialBlock creates a test block with a partial index
func createPartialBlock(id, previousID string, blockNum uint64, partialIndex int32) *bstream.PreprocessedBlock {
block := bstream.TestBlock(id, previousID)
block.Number = blockNum
block.PartialIndex = partialIndex
return &bstream.PreprocessedBlock{
Block: block,
Obj: &testStepable{step: bstream.StepPartial},
}
}
// createBlock creates a test block without partial index (regular block)
func createBlock(id, previousID string, blockNum uint64) *bstream.PreprocessedBlock {
block := bstream.TestBlock(id, previousID)
block.Number = blockNum
block.PartialIndex = 0
return &bstream.PreprocessedBlock{
Block: block,
Obj: &testStepable{step: bstream.StepNew},
}
}
// testStepable implements bstream.Stepable for testing
type testStepable struct {
step bstream.StepType
}
func (t *testStepable) Step() bstream.StepType {
return t.step
}
func (t *testStepable) FinalBlockHeight() uint64 {
return 0
}
func (t *testStepable) ReorgJunctionBlock() bstream.BlockRef {
return nil
}
func TestSubscription_Run_WithPartialBlocks(t *testing.T) {
tests := []struct {
name string
blocks []*bstream.PreprocessedBlock
expectedCalls []handlerCall
expectedNextID string // expected ID of block left in s.next after run completes
}{
{
name: "sequence of partial blocks - expect call with latest partial",
blocks: []*bstream.PreprocessedBlock{
createPartialBlock("3partial1", "2", 3, 1),
createPartialBlock("3partial2", "2", 3, 2),
createPartialBlock("3partial3", "2", 3, 3),
},
expectedCalls: []handlerCall{
{blockNum: 3, blockID: "3partial3", step: bstream.StepPartial, partialIndex: 3},
},
expectedNextID: "",
},
{
name: "partial blocks followed by different block number",
blocks: []*bstream.PreprocessedBlock{
createPartialBlock("3partial1", "2", 3, 1),
createPartialBlock("3partial2", "2", 3, 2),
createBlock("4", "3", 4),
},
expectedCalls: []handlerCall{
{blockNum: 3, blockID: "3partial2", step: bstream.StepPartial, partialIndex: 2},
{blockNum: 4, blockID: "4", step: bstream.StepNew, partialIndex: 0},
},
expectedNextID: "",
},
{
name: "single partial block",
blocks: []*bstream.PreprocessedBlock{
createPartialBlock("3partial1", "2", 3, 1),
},
expectedCalls: []handlerCall{
{blockNum: 3, blockID: "3partial1", step: bstream.StepPartial, partialIndex: 1},
},
expectedNextID: "",
},
{
name: "regular block without partials",
blocks: []*bstream.PreprocessedBlock{
createBlock("3", "2", 3),
},
expectedCalls: []handlerCall{
{blockNum: 3, blockID: "3", step: bstream.StepNew, partialIndex: 0},
},
expectedNextID: "",
},
{
name: "mixed sequence - partials then regular then partials",
blocks: []*bstream.PreprocessedBlock{
createPartialBlock("3partial1", "2", 3, 1),
createPartialBlock("3partial2", "2", 3, 2),
createBlock("4", "3", 4),
createPartialBlock("5partial1", "4", 5, 1),
createPartialBlock("5partial2", "4", 5, 2),
},
expectedCalls: []handlerCall{
{blockNum: 3, blockID: "3partial2", step: bstream.StepPartial, partialIndex: 2},
{blockNum: 4, blockID: "4", step: bstream.StepNew, partialIndex: 0},
{blockNum: 5, blockID: "5partial2", step: bstream.StepPartial, partialIndex: 2},
},
expectedNextID: "",
},
{
name: "empty blocks channel",
blocks: []*bstream.PreprocessedBlock{},
expectedCalls: nil,
expectedNextID: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &testHandler{}
sub := NewSubscription(handler, 10, true)
// Start run() in a goroutine
var runErr error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
runErr = sub.run()
}()
// Fill the channel with test blocks
for _, block := range tt.blocks {
err := sub.push(block)
require.NoError(t, err)
}
// Give some time for blocks to be processed
time.Sleep(10 * time.Millisecond)
// Shutdown the subscription
sub.Shutdown(nil)
// Wait for run() to complete
wg.Wait()
require.NoError(t, runErr)
// Verify the handler was called correctly
actualCalls := handler.getCalls()
assert.Equal(t, tt.expectedCalls, actualCalls)
// Verify s.next state
if tt.expectedNextID == "" {
assert.Nil(t, sub.next)
} else {
require.NotNil(t, sub.next)
assert.Equal(t, tt.expectedNextID, sub.next.Block.Id)
}
})
}
}
func TestSubscription_Push(t *testing.T) {
handler := &testHandler{}
sub := NewSubscription(handler, 2, true) // small channel size
// Should be able to push up to channel capacity
block1 := createBlock("1", "0", 1)
err := sub.push(block1)
assert.NoError(t, err)
block2 := createBlock("2", "1", 2)
err = sub.push(block2)
assert.NoError(t, err)
// Should fail when channel is full
block3 := createBlock("3", "2", 3)
err = sub.push(block3)
assert.Equal(t, ErrSubscriptionChannelFull, err)
}
func TestLookAhead(t *testing.T) {
// Test with empty channel
ch := make(chan *bstream.PreprocessedBlock, 2)
result := lookAhead(ch)
assert.Nil(t, result)
// Test with block in channel
block := createBlock("1", "0", 1)
ch <- block
result = lookAhead(ch)
require.NotNil(t, result)
assert.Equal(t, "1", result.Block.Id)
// Channel should now be empty
result = lookAhead(ch)
assert.Nil(t, result)
}
func TestNewSubscription(t *testing.T) {
handler := &testHandler{}
chanSize := 5
sub := NewSubscription(handler, chanSize, true)
assert.NotNil(t, sub)
assert.Equal(t, handler, sub.handler)
assert.Equal(t, chanSize, cap(sub.blocks))
assert.Nil(t, sub.next)
assert.NotNil(t, sub.Shutter)
}