forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
329 lines (278 loc) · 8.14 KB
/
testing.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// 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 (
"bufio"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"testing"
"time"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/dbin"
"github.com/streamingfast/shutter"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
type TestBlockIndexProvider struct {
Blocks []uint64
LastIndexedBlock uint64
ThrowError error
}
func (t *TestBlockIndexProvider) BlocksInRange(lowBlockNum uint64, bundleSize uint64) (out []uint64, err error) {
if t.ThrowError != nil {
return nil, t.ThrowError
}
if lowBlockNum > t.LastIndexedBlock {
return nil, fmt.Errorf("no indexed file here")
}
for _, blkNum := range t.Blocks {
if blkNum >= lowBlockNum && blkNum < (lowBlockNum+bundleSize) {
out = append(out, blkNum)
}
}
return out, nil
}
func bRef(id string) BlockRef {
return NewBlockRefFromID(id)
}
type TestSourceFactory struct {
Created chan *TestSource
FromBlockNumFunc func(uint64, Handler) Source
FromCursorFunc func(*Cursor, Handler) Source
ThroughCursorFunc func(uint64, *Cursor, Handler) Source
LowestBlkNum uint64
}
func NewTestSourceFactory() *TestSourceFactory {
return &TestSourceFactory{
Created: make(chan *TestSource, 10),
}
}
func (t *TestSourceFactory) LowestBlockNum() uint64 {
return t.LowestBlkNum
}
func (t *TestSourceFactory) NewSource(h Handler) Source {
src := NewTestSource(h)
t.Created <- src
return src
}
func (t *TestSourceFactory) NewSourceFromRef(ref BlockRef, h Handler) Source {
src := NewTestSource(h)
src.StartBlockID = ref.ID()
t.Created <- src
return src
}
func (t *TestSourceFactory) SourceFromBlockNum(blockNum uint64, h Handler) Source {
if t.FromBlockNumFunc != nil {
return t.FromBlockNumFunc(blockNum, h)
}
src := NewTestSource(h)
src.StartBlockNum = blockNum
t.Created <- src
return src
}
func (t *TestSourceFactory) SourceFromCursor(cursor *Cursor, h Handler) Source {
if t.FromCursorFunc != nil {
return t.FromCursorFunc(cursor, h)
}
src := NewTestSource(h)
src.Cursor = cursor
t.Created <- src
return src
}
func (t *TestSourceFactory) SourceThroughCursor(start uint64, cursor *Cursor, h Handler) Source {
if t.ThroughCursorFunc != nil {
return t.ThroughCursorFunc(start, cursor, h)
}
src := NewTestSource(h)
src.StartBlockNum = start
src.Cursor = cursor
src.PassThroughCursor = true
t.Created <- src
return src
}
func NewTestSource(h Handler) *TestSource {
return &TestSource{
Shutter: shutter.New(),
handler: h,
running: make(chan interface{}),
logger: zlog,
}
}
type TestSource struct {
handler Handler
logger *zap.Logger
*shutter.Shutter
running chan interface{}
StartBlockID string
StartBlockNum uint64
Cursor *Cursor
PassThroughCursor bool
}
func (t *TestSource) SetLogger(logger *zap.Logger) {
t.logger = logger
}
func (t *TestSource) Run() {
close(t.running)
<-t.Terminating()
}
func (t *TestSource) Push(b *pbbstream.Block, obj interface{}) error {
err := t.handler.ProcessBlock(b, obj)
if err != nil {
t.Shutdown(err)
}
return err
}
var testBlockDateLayout = "2006-01-02T15:04:05.000"
type ParsableTestBlock struct {
ID string `json:"id,omitempty"`
ParentID string `json:"prev,omitempty"`
ParentNum uint64 `json:"prevnum,omitempty"`
Number uint64 `json:"num,omitempty"`
LIBNum uint64 `json:"libnum,omitempty"`
Timestamp string `json:"time,omitempty"`
Kind int32 `json:"kind,omitempty"`
Version int32 `json:"version,omitempty"`
}
func TestBlock(id, prev string) *pbbstream.Block {
return TestBlockFromJSON(fmt.Sprintf(`{"id":%q,"prev": %q}`, id, prev))
}
func TestBlockWithNumbers(id, prev string, num, prevNum uint64) *pbbstream.Block {
return TestBlockFromJSON(fmt.Sprintf(`{"id":%q,"prev": %q,"prevnum": %d, "num": %d}`, id, prev, prevNum, num))
}
func TestBlockWithTimestamp(id, prev string, timestamp time.Time) *pbbstream.Block {
return TestBlockFromJSON(fmt.Sprintf(`{"id":%q,"prev":%q,"time":"%s"}`, id, prev, timestamp.Format(testBlockDateLayout)))
}
func TestBlockWithLIBNum(id, previousID string, newLIB uint64) *pbbstream.Block {
return TestBlockFromJSON(TestJSONBlockWithLIBNum(id, previousID, newLIB))
}
func TestJSONBlockWithLIBNum(id, previousID string, newLIB uint64) string {
return fmt.Sprintf(`{"id":%q,"prev":%q,"libnum":%d}`, id, previousID, newLIB)
}
func TestBlockFromJSON(jsonContent string) *pbbstream.Block {
obj := new(ParsableTestBlock)
err := json.Unmarshal([]byte(jsonContent), obj)
if err != nil {
panic(fmt.Errorf("unable to read payload %q: %w", jsonContent, err))
}
blockTime := time.Time{}
if obj.Timestamp != "" {
t, err := time.Parse("2006-01-02T15:04:05.999", obj.Timestamp)
if err != nil {
panic(fmt.Errorf("unable to parse timestamp %q: %w", obj.Timestamp, err))
}
blockTime = t
}
number := obj.Number
if number == 0 {
number = blocknum(obj.ID)
}
previousNum := obj.ParentNum
if previousNum == 0 {
previousNum = blocknum(obj.ParentID)
}
block := &pbbstream.Block{
Id: obj.ID,
Number: number,
ParentId: obj.ParentID,
ParentNum: previousNum,
Timestamp: timestamppb.New(blockTime),
LibNum: obj.LIBNum,
Payload: &anypb.Any{
TypeUrl: "type.googleapis.com/sf.bsream.type.v1.TestBlock",
Value: []byte(jsonContent),
},
}
return block
}
// copies the eos behavior for simpler tests
func blocknum(blockID string) uint64 {
b := blockID
if len(blockID) < 8 { // shorter version, like 8a for 00000008a
b = fmt.Sprintf("%09s", blockID)
}
bin, err := hex.DecodeString(b[:8])
if err != nil {
return 0
}
return uint64(binary.BigEndian.Uint32(bin))
}
type TestBlockReader struct {
scanner *bufio.Scanner
}
func (r *TestBlockReader) Read() (*pbbstream.Block, error) {
success := r.scanner.Scan()
if !success {
err := r.scanner.Err()
if err == nil {
err = io.EOF
}
return nil, err
}
t := r.scanner.Text()
return TestBlockFromJSON(t), nil
}
type TestBlockWriterBin struct {
DBinWriter *dbin.Writer
}
func (w *TestBlockWriterBin) Write(blk *pbbstream.Block) error {
bytes, err := proto.Marshal(blk)
if err != nil {
return fmt.Errorf("unable to marshal proto block: %w", err)
}
return w.DBinWriter.WriteMessage(bytes)
}
type TestBlockReaderBin struct {
DBinReader *dbin.Reader
}
func (l *TestBlockReaderBin) Read() (*pbbstream.Block, error) {
message, err := l.DBinReader.ReadMessage()
if len(message) > 0 {
blk := new(pbbstream.Block)
err = proto.Unmarshal(message, blk)
if err != nil {
return nil, fmt.Errorf("unable to read block proto: %w", err)
}
return blk, nil
}
if err == io.EOF {
return nil, err
}
return nil, fmt.Errorf("failed reading next dbin message: %w", err)
}
func AssertCursorEqual(t *testing.T, expected, actual *Cursor) {
t.Helper()
assert.Equal(t, expected.Step, actual.Step)
AssertBlockRefEqual(t, expected.Block, actual.Block)
AssertBlockRefEqual(t, expected.LIB, actual.LIB)
AssertBlockRefEqual(t, expected.HeadBlock, actual.HeadBlock)
}
func AssertBlockRefEqual(t *testing.T, expected, actual BlockRef) {
t.Helper()
assert.Equal(t, expected.ID(), actual.ID())
assert.Equal(t, expected.Num(), actual.Num())
}
func AssertProtoEqual(t *testing.T, expected, actual proto.Message) {
t.Helper()
// We use a custom comparison function and than rely on a standard `assert.Equal` so we get some
// diffing information. Ideally, a better diff would be displayed, good enough for now.
if !proto.Equal(expected, actual) {
assert.Equal(t, expected, actual)
}
}