-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjarvis_test.go
116 lines (95 loc) · 2.62 KB
/
jarvis_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
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
package jarvis
import (
"bytes"
"context"
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)
func TestVerifyChecksum(t *testing.T) {
m := Message{
Type: Height,
length: 3,
params: 0x01020f,
cksum: 0x16,
}
assert.True(t, verifyChecksum(m))
m.cksum = 0x07
assert.False(t, verifyChecksum(m))
}
//nolint:funlen
func TestNextMessage(t *testing.T) {
ctx := context.Background()
j := New(bytes.NewReader(nil), InitMetrics("jarvis"))
m, err := j.NextMessage(ctx)
assert.Error(t, err)
assert.Nil(t, m)
j = New(bytes.NewReader([]byte{0x00}), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.Error(t, err)
assert.Nil(t, m)
// should error when the context is cancelled!
ctx, cancel := context.WithCancel(context.Background())
cancel()
j = New(bytes.NewReader([]byte{0x00}), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.Error(t, err)
assert.Nil(t, m)
ctx = context.Background()
// early close after incomplete message
j = New(bytes.NewReader([]byte{0x0f2, 0x0f2}), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.Error(t, err)
assert.Nil(t, m)
// invalid length messages
j = New(bytes.NewReader([]byte{
0x0f2, 0x0f2, 0x01, 0x04,
0x0f2, 0x0f2, 0x01, 0x04,
0x0f2, 0x0f2, 0x01, 0x04,
}), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.Error(t, err)
assert.Nil(t, m)
assert.Equal(t, 3.0, testutil.ToFloat64(j.metrics.invalidLenCount))
assert.Equal(t, 0.0, testutil.ToFloat64(j.metrics.checksumErrorsCount))
// bad checksum
msg := []byte{
0x0f2, 0x0f2, 0x01, 0x03, 0x01, 0x0a, 0x0f, 0x05, 0x7e,
}
j = New(bytes.NewReader(msg), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.NoError(t, err)
assert.Equal(t, &Message{
raw: msg,
addr: 0xf2,
Type: 0x01,
length: 0x03,
params: 0x010a0f,
cksum: 0x05,
}, m)
assert.Equal(t, 1.0, testutil.ToFloat64(j.metrics.checksumErrorsCount))
assert.Equal(t, 0.0, testutil.ToFloat64(j.metrics.invalidLenCount))
// legit message
msg = []byte{
0x0f2, 0x0f2, 0x01, 0x03, 0x01, 0x0a, 0x0f, 0x1e, 0x7e,
}
j = New(bytes.NewReader(msg), InitMetrics("jarvis"))
m, err = j.NextMessage(ctx)
assert.NoError(t, err)
assert.Equal(t, 0.0, testutil.ToFloat64(j.metrics.checksumErrorsCount))
assert.Equal(t, &Message{
raw: msg,
addr: 0xf2,
Type: 0x01,
length: 0x03,
params: 0x010a0f,
cksum: 0x1e,
}, m)
assert.Equal(t, 0.0, testutil.ToFloat64(j.metrics.invalidLenCount))
}
func TestMetricsCollector(t *testing.T) {
m := InitMetrics("foo")
probs, err := testutil.CollectAndLint(m)
assert.NoError(t, err)
assert.Empty(t, probs)
}