tailscale/util/limiter/limiter_test.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

219 lines
6.0 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package limiter
import (
"bytes"
"io"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
const testRefillInterval = time.Second
func TestLimiter(t *testing.T) {
// 1qps, burst of 10, 2 keys tracked
limiter := &Limiter[string]{
Size: 2,
Max: 10,
RefillInterval: testRefillInterval,
}
// Consume entire burst
now := time.Now().Truncate(testRefillInterval)
allowed(t, limiter, "foo", 10, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", 0)
allowed(t, limiter, "bar", 10, now)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", 0)
// Refill 1 token for both foo and bar
now = now.Add(time.Second + time.Millisecond)
allowed(t, limiter, "foo", 1, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", 0)
allowed(t, limiter, "bar", 1, now)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", 0)
// Refill 2 tokens for foo and bar
now = now.Add(2*time.Second + time.Millisecond)
allowed(t, limiter, "foo", 2, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", 0)
allowed(t, limiter, "bar", 2, now)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", 0)
// qux can burst 10, evicts foo so it can immediately burst 10 again too
allowed(t, limiter, "qux", 10, now)
denied(t, limiter, "qux", 1, now)
notInLimiter(t, limiter, "foo")
denied(t, limiter, "bar", 1, now) // refresh bar so foo lookup doesn't evict it - still throttled
allowed(t, limiter, "foo", 10, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", 0)
}
func TestLimiterOverdraft(t *testing.T) {
// 1qps, burst of 10, overdraft of 2, 2 keys tracked
limiter := &Limiter[string]{
Size: 2,
Max: 10,
Overdraft: 2,
RefillInterval: testRefillInterval,
}
// Consume entire burst, go 1 into debt
now := time.Now().Truncate(testRefillInterval).Add(time.Millisecond)
allowed(t, limiter, "foo", 10, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", -1)
allowed(t, limiter, "bar", 10, now)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", -1)
// Refill 1 token for both foo and bar.
// Still denied, still in debt.
now = now.Add(time.Second)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", -1)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", -1)
// Refill 2 tokens for foo and bar (1 available after debt), try
// to consume 4. Overdraft is capped to 2.
now = now.Add(2 * time.Second)
allowed(t, limiter, "foo", 1, now)
denied(t, limiter, "foo", 3, now)
hasTokens(t, limiter, "foo", -2)
allowed(t, limiter, "bar", 1, now)
denied(t, limiter, "bar", 3, now)
hasTokens(t, limiter, "bar", -2)
// Refill 1, not enough to allow.
now = now.Add(time.Second)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", -2)
denied(t, limiter, "bar", 1, now)
hasTokens(t, limiter, "bar", -2)
// qux evicts foo, foo can immediately burst 10 again.
allowed(t, limiter, "qux", 1, now)
hasTokens(t, limiter, "qux", 9)
notInLimiter(t, limiter, "foo")
allowed(t, limiter, "foo", 10, now)
denied(t, limiter, "foo", 1, now)
hasTokens(t, limiter, "foo", -1)
}
func TestDumpHTML(t *testing.T) {
limiter := &Limiter[string]{
Size: 3,
Max: 10,
Overdraft: 10,
RefillInterval: testRefillInterval,
}
now := time.Now().Truncate(testRefillInterval).Add(time.Millisecond)
allowed(t, limiter, "foo", 10, now)
denied(t, limiter, "foo", 2, now)
allowed(t, limiter, "bar", 4, now)
allowed(t, limiter, "qux", 1, now)
var out bytes.Buffer
limiter.DumpHTML(&out, false)
want := strings.Join([]string{
"<table>",
"<tr><th>Key</th><th>Tokens</th></tr>",
"<tr><td>qux</td><td>9</td></tr>",
"<tr><td>bar</td><td>6</td></tr>",
"<tr><td>foo</td><td><b>-2</b></td></tr>",
"</table>",
}, "")
if diff := cmp.Diff(out.String(), want); diff != "" {
t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
}
out.Reset()
limiter.DumpHTML(&out, true)
want = strings.Join([]string{
"<table>",
"<tr><th>Key</th><th>Tokens</th></tr>",
"<tr><td>foo</td><td>-2</td></tr>",
"</table>",
}, "")
if diff := cmp.Diff(out.String(), want); diff != "" {
t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
}
// Check that DumpHTML updates tokens even if the key wasn't hit
// organically.
now = now.Add(3 * time.Second)
out.Reset()
limiter.dumpHTML(&out, false, now)
want = strings.Join([]string{
"<table>",
"<tr><th>Key</th><th>Tokens</th></tr>",
"<tr><td>qux</td><td>10</td></tr>",
"<tr><td>bar</td><td>9</td></tr>",
"<tr><td>foo</td><td>1</td></tr>",
"</table>",
}, "")
if diff := cmp.Diff(out.String(), want); diff != "" {
t.Fatalf("wrong DumpHTML output (-got+want):\n%s", diff)
}
}
func TestDumpHTMLEmpty(t *testing.T) {
new(Limiter[string]).DumpHTML(io.Discard, false) // should not panic
}
func allowed(t *testing.T, limiter *Limiter[string], key string, count int, now time.Time) {
t.Helper()
for i := range count {
if !limiter.allow(key, now) {
toks, ok := limiter.tokensForTest(key)
t.Errorf("after %d times: allow(%q, %q) = false, want true (%d tokens available, in cache = %v)", i, key, now, toks, ok)
}
}
}
func denied(t *testing.T, limiter *Limiter[string], key string, count int, now time.Time) {
t.Helper()
for i := range count {
if limiter.allow(key, now) {
toks, ok := limiter.tokensForTest(key)
t.Errorf("after %d times: allow(%q, %q) = true, want false (%d tokens available, in cache = %v)", i, key, now, toks, ok)
}
}
}
func hasTokens(t *testing.T, limiter *Limiter[string], key string, want int64) {
t.Helper()
got, ok := limiter.tokensForTest(key)
if !ok {
t.Errorf("key %q missing from limiter", key)
} else if got != want {
t.Errorf("key %q has %d tokens, want %d", key, got, want)
}
}
func notInLimiter(t *testing.T, limiter *Limiter[string], key string) {
t.Helper()
if tokens, ok := limiter.tokensForTest(key); ok {
t.Errorf("key %q unexpectedly tracked by limiter, with %d tokens", key, tokens)
}
}