tailscale/ipn/ipnserver/server_test.go
Wendi Yu 0c69b4e00d
Implement rate limiting on log messages (#356)
Implement rate limiting on log messages

Addresses issue #317, where logs can get spammed with the same message
nonstop. Created a rate limiting closure on logging functions, which
limits the number of messages being logged per second based on format
string. To keep memory usage as constant as possible, the previous cache
purging at periodic time intervals has been replaced by an LRU that
discards the oldest string when the capacity of the cache is reached.


Signed-off-by: Wendi Yu <wendi.yu@yahoo.ca>
2020-05-08 13:21:36 -06:00

82 lines
1.8 KiB
Go

// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ipnserver_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"tailscale.com/types/logger"
"tailscale.com/ipn"
"tailscale.com/ipn/ipnserver"
"tailscale.com/safesocket"
"tailscale.com/wgengine"
)
func TestRunMultipleAccepts(t *testing.T) {
t.Skipf("TODO(bradfitz): finish this test, once other fires are out")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
td, err := ioutil.TempDir("", "TestRunMultipleAccepts")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(td)
socketPath := filepath.Join(td, "tailscale.sock")
ulogf := func(format string, args ...interface{}) {
format = strings.TrimRight(format, "\n")
println(fmt.Sprintf(format, args...))
t.Logf(format, args...)
}
logf := logger.RateLimitedFn(ulogf, 1, 1, 100)
connect := func() {
for i := 1; i <= 2; i++ {
logf("connect %d ...", i)
c, err := safesocket.Connect(socketPath, 0)
if err != nil {
t.Fatalf("safesocket.Connect: %v\n", err)
}
clientToServer := func(b []byte) {
ipn.WriteMsg(c, b)
}
bc := ipn.NewBackendClient(logf, clientToServer)
prefs := ipn.NewPrefs()
bc.SetPrefs(prefs)
c.Close()
}
}
logTriggerTestf := func(format string, args ...interface{}) {
logf(format, args...)
if strings.HasPrefix(format, "Listening on ") {
connect()
}
}
eng, err := wgengine.NewFakeUserspaceEngine(logf, 0)
if err != nil {
t.Fatal(err)
}
defer eng.Close()
opts := ipnserver.Options{
SocketPath: socketPath,
}
t.Logf("pre-Run")
err = ipnserver.Run(ctx, logTriggerTestf, "dummy_logid", opts, eng)
t.Logf("ipnserver.Run = %v", err)
}