forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesource_test.go
More file actions
108 lines (95 loc) · 2.61 KB
/
filesource_test.go
File metadata and controls
108 lines (95 loc) · 2.61 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
// 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 (
"encoding/json"
"fmt"
"testing"
"time"
"go.uber.org/zap"
"github.com/streamingfast/dstore"
"github.com/stretchr/testify/require"
)
func TestRetryableError(t *testing.T) {
err := fmt.Errorf("hehe")
ret := retryableError{err}
require.True(t, isRetryable(ret))
require.False(t, isRetryable(err))
require.Equal(t, ret.Error(), err.Error())
}
func testBlocks(in ...interface{}) (out []byte) {
var blks []ParsableTestBlock
for i := 0; i < len(in); i += 4 {
blks = append(blks, ParsableTestBlock{
Number: uint64(in[i].(int)),
ID: in[i+1].(string),
PreviousID: in[i+2].(string),
LIBNum: uint64(in[i+3].(int)),
})
}
for _, blk := range blks {
b, err := json.Marshal(blk)
if err != nil {
panic(err)
}
out = append(out, b...)
out = append(out, '\n')
}
return
}
func base(in int) string {
return fmt.Sprintf("%010d", in)
}
func TestFileSource_Run(t *testing.T) {
bs := dstore.NewMockStore(nil)
bs.SetFile(base(0), testBlocks(
1, "1a", "", 0,
2, "2a", "", 0,
))
bs.SetFile(base(100), testBlocks(
3, "3a", "", 0,
4, "4a", "", 0,
))
expectedBlockCount := 4
preProcessCount := 0
preprocessor := PreprocessFunc(func(blk *Block) (interface{}, error) {
preProcessCount++
return blk.ID(), nil
})
testDone := make(chan interface{})
handlerCount := 0
expectedBlockNum := uint64(1)
handler := HandlerFunc(func(blk *Block, obj interface{}) error {
zlog.Debug("test : received block", zap.Stringer("block_ref", blk))
require.Equal(t, expectedBlockNum, blk.Number)
expectedBlockNum++
handlerCount++
require.Equal(t, uint64(handlerCount), blk.Num())
require.Equal(t, blk.ID(), obj)
if handlerCount >= expectedBlockCount {
close(testDone)
}
return nil
})
fs := NewFileSource(bs, 1, 1, preprocessor, handler)
go fs.Run()
select {
case <-testDone:
require.Equal(t, expectedBlockCount, preProcessCount)
require.Equal(t, expectedBlockCount, handlerCount)
case <-time.After(100 * time.Millisecond):
t.Error("Test timeout")
}
fs.Shutdown(nil)
}