MCPcopy Index your code
hub / github.com/tailscale/tailscale / TestClientSendRateLimiting

Function TestClientSendRateLimiting

derp/client_test.go:154–204  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

152}
153
154func TestClientSendRateLimiting(t *testing.T) {
155 cw := new(countWriter)
156 c := &Client{
157 bw: bufio.NewWriter(cw),
158 clock: &tstest.Clock{},
159 }
160 c.setSendRateLimiter(ServerInfoMessage{})
161
162 pkt := make([]byte, 1000)
163 if err := c.send(key.NodePublic{}, pkt); err != nil {
164 t.Fatal(err)
165 }
166 writes1, bytes1 := cw.Stats()
167 if writes1 != 1 {
168 t.Errorf("writes = %v, want 1", writes1)
169 }
170
171 // Flood should all succeed.
172 cw.ResetStats()
173 for range 1000 {
174 if err := c.send(key.NodePublic{}, pkt); err != nil {
175 t.Fatal(err)
176 }
177 }
178 writes1K, bytes1K := cw.Stats()
179 if writes1K != 1000 {
180 t.Logf("writes = %v; want 1000", writes1K)
181 }
182 if got, want := bytes1K, bytes1*1000; got != want {
183 t.Logf("bytes = %v; want %v", got, want)
184 }
185
186 // Set a rate limiter
187 cw.ResetStats()
188 c.setSendRateLimiter(ServerInfoMessage{
189 TokenBucketBytesPerSecond: 1,
190 TokenBucketBytesBurst: int(bytes1 * 2),
191 })
192 for range 1000 {
193 if err := c.send(key.NodePublic{}, pkt); err != nil {
194 t.Fatal(err)
195 }
196 }
197 writesLimited, bytesLimited := cw.Stats()
198 if writesLimited == 0 || writesLimited == writes1K {
199 t.Errorf("limited conn's write count = %v; want non-zero, less than 1k", writesLimited)
200 }
201 if bytesLimited < bytes1*2 || bytesLimited >= bytes1K {
202 t.Errorf("limited conn's bytes count = %v; want >=%v, <%v", bytesLimited, bytes1K*2, bytes1K)
203 }
204}

Callers

nothing calls this directly

Calls 7

setSendRateLimiterMethod · 0.95
sendMethod · 0.95
StatsMethod · 0.80
ResetStatsMethod · 0.80
FatalMethod · 0.65
ErrorfMethod · 0.65
LogfMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…