mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-15 12:02:31 +00:00
util/lru, util/limiter: add debug helper to dump state as HTML
For use in tsweb debug handlers, so that we can easily inspect cache and limiter state when troubleshooting. Updates tailscale/corp#3601 Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
committed by
Dave Anderson
parent
d23b8ffb13
commit
95082a8dde
@@ -4,8 +4,12 @@
|
||||
package limiter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
const testRefillInterval = time.Second
|
||||
@@ -113,6 +117,64 @@ func TestLimiterOverdraft(t *testing.T) {
|
||||
hasTokens(t, l, "foo", -1)
|
||||
}
|
||||
|
||||
func TestDumpHTML(t *testing.T) {
|
||||
l := &Limiter[string]{
|
||||
Size: 3,
|
||||
Max: 10,
|
||||
Overdraft: 10,
|
||||
RefillInterval: testRefillInterval,
|
||||
}
|
||||
|
||||
now := time.Now().Truncate(testRefillInterval).Add(time.Millisecond)
|
||||
allowed(t, l, "foo", 10, now)
|
||||
denied(t, l, "foo", 2, now)
|
||||
allowed(t, l, "bar", 4, now)
|
||||
allowed(t, l, "qux", 1, now)
|
||||
|
||||
var out bytes.Buffer
|
||||
l.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()
|
||||
l.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()
|
||||
l.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 allowed(t *testing.T, l *Limiter[string], key string, count int, now time.Time) {
|
||||
t.Helper()
|
||||
for i := 0; i < count; i++ {
|
||||
|
||||
Reference in New Issue
Block a user