forked from regen-network/cosmosd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
63 lines (57 loc) · 1.35 KB
/
scanner_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
package main
import (
"bufio"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWaitForInfo(t *testing.T) {
cases := map[string]struct {
write []string
expectUpgrade *UpgradeInfo
expectErr bool
}{
"no info": {
write: []string{"some", "random\ninfo\n"},
},
"simple match": {
write: []string{"first line\n", `UPGRADE "myname" NEEDED at height 123: http://example.com`, "\nnext line\n"},
expectUpgrade: &UpgradeInfo{
Name: "myname",
Height: 123,
Info: "http://example.com",
},
},
"chunks": {
write: []string{"first l", "ine\nERROR 2020-02-03T11:22:33Z: UPGRADE ", `"split" NEEDED at height `, "789: {\"foo\":123} asgsdg", " \n LOG: next line"},
expectUpgrade: &UpgradeInfo{
Name: "split",
Height: 789,
Info: `{"foo":123}`,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
r, w := io.Pipe()
scan := bufio.NewScanner(r)
// write all info in separate routine
go func() {
for _, line := range tc.write {
n, err := w.Write([]byte(line))
assert.NoError(t, err)
assert.Equal(t, len(line), n)
}
w.Close()
}()
// now scan the info
info, err := WaitForUpdate(scan)
if tc.expectErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.expectUpgrade, info)
})
}
}