-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjoiningsource_test.go
More file actions
288 lines (235 loc) · 9.42 KB
/
Copy pathjoiningsource_test.go
File metadata and controls
288 lines (235 loc) · 9.42 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
285
286
287
288
// 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 bstream
import (
"errors"
"testing"
"time"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/dstore"
"github.com/stretchr/testify/assert"
"github.com/test-go/testify/require"
)
var errTestMock = errors.New("test failure")
func testHandler(failAt uint64) (HandlerFunc, chan *PreprocessedBlock) {
out := make(chan *PreprocessedBlock, 100)
return func(blk *pbbstream.Block, obj any) error {
if blk.Number == failAt {
return errTestMock
}
out <- &PreprocessedBlock{
Block: blk,
Obj: obj,
}
return nil
}, out
}
func TestJoiningSource_vanilla(t *testing.T) {
joiningBlock := uint64(4)
failingBlock := uint64(5)
fileSF := NewTestSourceFactory()
liveSF := NewTestSourceFactory()
var liveSrc *TestSource
handler, out := testHandler(failingBlock)
liveSF.FromBlockNumFunc = func(num uint64, h Handler) Source {
if num == joiningBlock {
src := NewTestSource(h)
liveSrc = src
return src
}
return nil
}
joiningSource := NewJoiningSource(fileSF, liveSF, handler, 2, nil, false, zlog)
go joiningSource.Run()
fileSrc := <-fileSF.Created
<-fileSrc.running // test fixture ready to push blocks
assert.Equal(t, uint64(2), fileSrc.StartBlockNum)
require.NoError(t, fileSrc.Push(TestBlock("00000002a", "00000001a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000003a", "00000002a"), nil))
require.EqualError(t, fileSrc.Push(TestBlock("00000004a", "00000003a"), nil),
stopSourceOnJoin.Error())
<-fileSrc.Terminated() // previous error causes termination
assert.Equal(t, 2, len(out))
require.NotNil(t, liveSrc, "we should have joined to live source")
require.NoError(t, liveSrc.Push(TestBlock("00000004a", "00000003a"), nil))
assert.Equal(t, 3, len(out))
require.EqualError(t, liveSrc.Push(TestBlock("00000005a", "00000004a"), nil),
errTestMock.Error())
<-liveSrc.Terminated()
<-joiningSource.Terminated()
}
// TestJoiningSource_indexedArchiveRejoinsLiveAtBufferFloor checks that an
// index-filtered archive stream re-joins the live source at the live-buffer floor
// even when its filter matches nothing in the merged/live overlap and the merger
// is behind (no merged file above the overlap exists). This is the end-to-end
// behaviour guaranteed by FileSourceWithLiveBlockFloorGetter (firehose-core #109):
// the file source stops index-skipping at the floor, reads that bundle entirely,
// and emits an overlap block so the joining source can switch to live.
func TestJoiningSource_indexedArchiveRejoinsLiveAtBufferFloor(t *testing.T) {
const liveFloor = uint64(250)
// A historical bundle (where the request starts) and the bundle overlapping the
// live buffer exist; everything above the overlap is missing (merger behind).
// The request starts historical (block 1), so the index governs the climb toward
// live: matching nothing, it would -- without the live floor -- skip straight past
// the overlap bundle up to LastIndexedBlock and then wait forever for a not-yet
// merged file, never emitting an overlap block and never re-joining live.
mergedStore := dstore.NewMockStore(nil)
mergedStore.SetFile(base(0), testBlocks(
TestBlockWithNumbers("1a", "0a", 1, 0),
))
mergedStore.SetFile(base(200), testBlocks(
TestBlockWithNumbers("250a", "249a", 250, 249),
TestBlockWithNumbers("251a", "250a", 251, 250),
))
liveSF := NewTestSourceFactory()
liveSF.LowestBlkNum = liveFloor
joined := make(chan *TestSource, 1)
liveSF.FromBlockNumFunc = func(num uint64, h Handler) Source {
if num >= liveFloor {
src := NewTestSource(h)
joined <- src
return src
}
return nil // below the floor: not a live block
}
fileSF := NewFileSourceFactory(mergedStore, dstore.NewMockStore(nil), zlog,
FileSourceWithBlockIndexProvider(&TestBlockIndexProvider{Blocks: nil, LastIndexedBlock: 100000}),
FileSourceWithLiveBlockFloorGetter(func() uint64 { return liveFloor }),
)
handler, out := testHandler(99999)
js := NewJoiningSource(fileSF, liveSF, handler, 1, nil, false, zlog)
go js.Run()
defer js.Shutdown(nil)
var liveSrc *TestSource
select {
case liveSrc = <-joined:
case <-time.After(2 * time.Second):
t.Fatal("indexed archive never re-joined the live source at the buffer floor")
}
// Live source now drives the stream forward; the historical block 1 was streamed
// from archive before the re-join.
require.NoError(t, liveSrc.Push(TestBlockWithNumbers("251a", "250a", 251, 250), nil))
deadline := time.After(2 * time.Second)
for {
select {
case b := <-out:
if b.Block.Number >= liveFloor {
require.Equal(t, uint64(251), b.Block.Number, "live block delivered after the re-join")
return
}
case <-deadline:
t.Fatal("expected a live block to be delivered after the re-join")
}
}
}
func TestJoiningSource_through_cursor(t *testing.T) {
joiningBlock := uint64(6)
failingBlock := uint64(9999)
fileSF := NewTestSourceFactory()
liveSF := NewTestSourceFactory()
cursor := &Cursor{
Step: StepNew,
Block: NewBlockRefFromID("00000005"),
HeadBlock: NewBlockRefFromID("00000005"),
LIB: NewBlockRefFromID("00000003"),
}
startBlock := uint64(3)
var liveSrc *TestSource
handler, out := testHandler(failingBlock)
liveSF.ThroughCursorFunc = func(start uint64, cursor *Cursor, h Handler) Source {
if start == joiningBlock {
src := NewTestSource(h)
src.Cursor = cursor
src.StartBlockNum = start
src.PassThroughCursor = true
liveSrc = src
return src
}
return nil
}
joiningSource := NewJoiningSource(fileSF, liveSF, handler, startBlock, cursor, true, zlog)
go joiningSource.Run()
fileSrc := <-fileSF.Created
<-fileSrc.running // test fixture ready to push blocks
assert.Equal(t, uint64(3), fileSrc.StartBlockNum)
assert.Equal(t, cursor, fileSrc.Cursor)
assert.True(t, fileSrc.PassThroughCursor)
require.NoError(t, fileSrc.Push(TestBlock("00000003a", "00000002a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000004a", "00000003a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000005a", "00000004a"), nil))
require.EqualError(t, fileSrc.Push(TestBlock("00000006a", "00000005a"), nil), stopSourceOnJoin.Error())
<-fileSrc.Terminated() // previous error causes termination
assert.Equal(t, 3, len(out)) // 3, 4, 5 (6 will be sent by live)
require.NotNil(t, liveSrc, "we should have joined to live source")
assert.Equal(t, uint64(6), liveSrc.StartBlockNum)
assert.Equal(t, cursor, liveSrc.Cursor)
assert.True(t, liveSrc.PassThroughCursor)
require.NoError(t, liveSrc.Push(TestBlock("00000006a", "00000005a"), nil))
assert.Equal(t, 4, len(out))
}
func TestJoiningSource_skip_file_source(t *testing.T) {
var fileSF ForkableSourceFactory //not used
liveSF := NewTestSourceFactory()
handler, out := testHandler(0)
joiningSource := NewJoiningSource(fileSF, liveSF, handler, 2, nil, false, zlog)
go joiningSource.Run()
liveSrc := <-liveSF.Created
<-liveSrc.running
assert.Equal(t, uint64(2), liveSrc.StartBlockNum)
require.NoError(t, liveSrc.Push(TestBlock("00000002a", "00000001a"), nil))
require.NoError(t, liveSrc.Push(TestBlock("00000003a", "00000002a"), nil))
assert.Len(t, out, 2)
joiningSource.Shutdown(errTestMock)
<-liveSrc.Terminated() // previous error causes termination
<-joiningSource.Terminated() // previous error causes termination
}
func TestJoiningSource_lowerLimitBackoff(t *testing.T) {
fileSF := NewTestSourceFactory()
liveSF := NewTestSourceFactory()
liveSF.LowestBlkNum = 8
joiningBlock := uint64(9)
var liveSrc *TestSource
liveSourceFactoryCalls := 0
handler, out := testHandler(0)
liveSF.FromBlockNumFunc = func(num uint64, h Handler) Source {
liveSourceFactoryCalls++
if num == joiningBlock {
src := NewTestSource(h)
liveSrc = src
return src
}
return nil
}
joiningSource := NewJoiningSource(fileSF, liveSF, handler, 1, nil, false, zlog)
go joiningSource.Run()
fileSrc := <-fileSF.Created
<-fileSrc.running
assert.Equal(t, uint64(1), fileSrc.StartBlockNum)
require.NoError(t, fileSrc.Push(TestBlock("00000001a", "00000000a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000002a", "00000001a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000003a", "00000002a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000004a", "00000003a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000005a", "00000004a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000006a", "00000005a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000007a", "00000006a"), nil))
require.NoError(t, fileSrc.Push(TestBlock("00000008a", "00000007a"), nil))
require.EqualError(t, fileSrc.Push(TestBlock("00000009a", "00000008a"), nil),
stopSourceOnJoin.Error())
<-fileSrc.Terminated() // previous error causes termination
assert.Equal(t, 8, len(out))
require.NotNil(t, liveSrc, "we should have joined to live source")
require.NoError(t, liveSrc.Push(TestBlock("00000009a", "00000008a"), nil))
assert.Equal(t, 9, len(out))
assert.Equal(t, 3, liveSourceFactoryCalls)
}