forked from capnproto/go-capnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readlimit_test.go
149 lines (145 loc) · 3.44 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package capnp
import "testing"
func TestReadLimiter_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.ReadLimiter().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 TestReadLimiter_Reset(t *testing.T) {
{
m := &Message{TraverseLimit: 42}
t.Log(" m := &Message{TraverseLimit: 42}")
ok := m.ReadLimiter().canRead(42)
t.Logf(" m.ReadLimiter().canRead(42) -> %t", ok)
m.ReadLimiter().Reset(8)
t.Log(" m.ReadLimiter().Reset(8)")
if m.ReadLimiter().canRead(8) {
t.Log(" m.ReadLimiter().canRead(8) -> true")
} else {
t.Error("!! m.ReadLimiter().canRead(8) -> false; want true")
}
}
t.Log()
{
m := &Message{TraverseLimit: 42}
t.Log(" m := &Message{TraverseLimit: 42}")
ok := m.ReadLimiter().canRead(40)
t.Logf(" m.ReadLimiter().canRead(40) -> %t", ok)
m.ReadLimiter().Reset(8)
t.Log(" m.ReadLimiter().Reset(8)")
if m.ReadLimiter().canRead(9) {
t.Error("!! m.ReadLimiter().canRead(9) -> true; want false")
} else {
t.Log(" m.ReadLimiter().canRead(9) -> false")
}
}
t.Log()
{
m := new(Message)
t.Log(" m := new(Message)")
m.ReadLimiter().Reset(0)
t.Log(" m.ReadLimiter().Reset(0)")
if !m.ReadLimiter().canRead(0) {
t.Error("!! m.ReadLimiter().canRead(0) -> false; want true")
} else {
t.Log(" m.ReadLimiter().canRead(0) -> true")
}
}
t.Log()
{
m := new(Message)
t.Log(" m := new(Message)")
m.ReadLimiter().Reset(0)
t.Log(" m.ReadLimiter().Reset(0)")
if m.ReadLimiter().canRead(1) {
t.Error("!! m.ReadLimiter().canRead(1) -> true; want false")
} else {
t.Log(" m.ReadLimiter().canRead(1) -> false")
}
}
}
func TestReadLimiter_Unread(t *testing.T) {
{
m := &Message{TraverseLimit: 42}
t.Log(" m := &Message{TraverseLimit: 42}")
ok := m.ReadLimiter().canRead(42)
t.Logf(" m.ReadLimiter().canRead(42) -> %t", ok)
m.ReadLimiter().Unread(8)
t.Log(" m.ReadLimiter().Unread(8)")
if m.ReadLimiter().canRead(8) {
t.Log(" m.ReadLimiter().canRead(8) -> true")
} else {
t.Error("!! m.ReadLimiter().canRead(8) -> false; want true")
}
}
t.Log()
{
m := &Message{TraverseLimit: 42}
t.Log(" m := &Message{TraverseLimit: 42}")
ok := m.ReadLimiter().canRead(40)
t.Logf(" m.ReadLimiter().canRead(40) -> %t", ok)
m.ReadLimiter().Unread(8)
t.Log(" m.ReadLimiter().Unread(8)")
if m.ReadLimiter().canRead(9) {
t.Log(" m.ReadLimiter().canRead(9) -> true")
} else {
t.Error("!! m.ReadLimiter().canRead(9) -> false; want true")
}
}
}