-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
readlimit_test.go
134 lines (121 loc) · 2.69 KB
/
readlimit_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package capnp
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMessage_canRead(t *testing.T) {
t.Parallel()
type canReadCall struct {
sz Size
ok bool
}
tests := []struct {
name string
init uint64
calls []canReadCall
}{
{
name: "read a word with default limit",
calls: []canReadCall{
{8, true},
},
},
{
name: "reading a word from a high limit is okay",
init: 128,
calls: []canReadCall{
{8, true},
},
},
{
name: "reading a byte after depleting the limit fails",
init: 8,
calls: []canReadCall{
{8, true},
{1, false},
},
},
{
name: "reading a byte after hitting the limit fails",
init: 8,
calls: []canReadCall{
{8, true},
{1, false},
},
},
{
name: "reading a byte after hitting the limit in multiple calls fails",
init: 8,
calls: []canReadCall{
{6, true},
{2, true},
{1, false},
},
},
}
for _, test := range tests {
m := &Message{TraverseLimit: test.init}
for i, c := range test.calls {
ok := m.canRead(c.sz)
if ok != c.ok {
// TODO(light): show previous calls
t.Errorf("in %s, calls[%d] ok = %t; want %t", test.name, i, ok, c.ok)
}
}
}
}
func TestMessage_ResetReadLimit(t *testing.T) {
t.Parallel()
t.Helper()
for _, tt := range []struct {
limit int
initial, canRead Size
readLim uint64
}{
{
limit: 42,
initial: 42,
readLim: 8,
canRead: 8,
},
{
limit: 42,
initial: 40,
readLim: 8,
canRead: 9,
},
{},
{canRead: 1},
} {
t.Run(fmt.Sprintf("TraverseLimit=%d", tt.limit), func(t *testing.T) {
m := &Message{TraverseLimit: uint64(tt.limit)}
require.True(t, m.canRead(tt.initial), "should be able to read %s bytes", tt.initial)
m.ResetReadLimit(tt.readLim)
if tt.readLim < uint64(tt.canRead) {
assert.False(t, m.canRead(tt.canRead), "should fail to read %d bytes", tt.canRead)
} else {
assert.True(t, m.canRead(tt.canRead), "should succeed in reading %d bytes", tt.canRead)
}
})
}
}
func TestMessage_Unread(t *testing.T) {
t.Parallel()
t.Helper()
t.Run("UnreadFromTraverseLimit", func(t *testing.T) {
t.Parallel()
m := &Message{TraverseLimit: 42}
require.True(t, m.canRead(42), "should be able to read up to TraverseLimit")
m.Unread(8)
assert.True(t, m.canRead(8), "should be able to read 8 bytes after unreading")
})
t.Run("UnreadBeforeTraverseLimit", func(t *testing.T) {
t.Parallel()
m := &Message{TraverseLimit: 42}
require.True(t, m.canRead(40), "should be able to read fewer than 'TraverseLimit' bytes")
m.Unread(8)
assert.True(t, m.canRead(9), "should be able to read 9 bytes after unreading")
})
}