mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-21 14:11:56 +00:00
ipn: Resolve some resource leaks in test.
Updates tailscale/corp#255. Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
This commit is contained in:
parent
ea8f92b312
commit
3ed2124356
@ -5,6 +5,7 @@
|
|||||||
package ipn
|
package ipn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tailscale.com/control/controlclient"
|
"tailscale.com/control/controlclient"
|
||||||
@ -104,6 +105,9 @@ type Options struct {
|
|||||||
LegacyConfigPath string
|
LegacyConfigPath string
|
||||||
// Notify is called when backend events happen.
|
// Notify is called when backend events happen.
|
||||||
Notify func(Notify) `json:"-"`
|
Notify func(Notify) `json:"-"`
|
||||||
|
// HTTPTestClient is an optional HTTP client to pass to controlclient
|
||||||
|
// (for tests only).
|
||||||
|
HTTPTestClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backend is the interface between Tailscale frontends
|
// Backend is the interface between Tailscale frontends
|
||||||
|
@ -8,6 +8,7 @@ package ipn
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
@ -40,6 +41,10 @@ func init() {
|
|||||||
|
|
||||||
func TestIPN(t *testing.T) {
|
func TestIPN(t *testing.T) {
|
||||||
tstest.PanicOnLog()
|
tstest.PanicOnLog()
|
||||||
|
rc := tstest.NewResourceCheck()
|
||||||
|
defer rc.Assert(t)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
// This gets reassigned inside every test, so that the connections
|
// This gets reassigned inside every test, so that the connections
|
||||||
// all log using the "current" t.Logf function. Sigh.
|
// all log using the "current" t.Logf function. Sigh.
|
||||||
@ -66,27 +71,32 @@ func TestIPN(t *testing.T) {
|
|||||||
ctl.ServeHTTP(w, r)
|
ctl.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
https := httptest.NewServer(http.HandlerFunc(ctlHandler))
|
https := httptest.NewServer(http.HandlerFunc(ctlHandler))
|
||||||
|
https.Config.ErrorLog = logger.StdLogger(logf)
|
||||||
serverURL := https.URL
|
serverURL := https.URL
|
||||||
defer https.Close()
|
|
||||||
defer https.CloseClientConnections()
|
|
||||||
|
|
||||||
tmpdir, err := ioutil.TempDir("", "ipntest")
|
tmpdir, err := ioutil.TempDir("", "ipntest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("create tempdir: %v\n", err)
|
t.Fatalf("create tempdir: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctl, err = control.New(tmpdir, tmpdir, tmpdir, serverURL, true, logf)
|
ctl, err = control.New(tmpdir, tmpdir, tmpdir, serverURL, true, logf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("create control server: %v\n", ctl)
|
t.Fatalf("create control server: %v\n", ctl)
|
||||||
}
|
}
|
||||||
|
defer ctl.Shutdown()
|
||||||
|
defer https.Close()
|
||||||
|
defer https.CloseClientConnections()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
if _, err := ctl.DB().FindOrCreateUser("google", "test1@example.com", "", ""); err != nil {
|
if _, err := ctl.DB().FindOrCreateUser("google", "test1@example.com", "", ""); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
n1 := newNode(t, logf, "n1", https, false)
|
n1 := newNode(t, ctx, logf, "n1", https, false)
|
||||||
defer n1.Backend.Shutdown()
|
defer n1.Backend.Shutdown()
|
||||||
n1.Backend.StartLoginInteractive()
|
n1.Backend.StartLoginInteractive()
|
||||||
|
|
||||||
n2 := newNode(t, logf, "n2", https, true)
|
n2 := newNode(t, ctx, logf, "n2", https, true)
|
||||||
defer n2.Backend.Shutdown()
|
defer n2.Backend.Shutdown()
|
||||||
n2.Backend.StartLoginInteractive()
|
n2.Backend.StartLoginInteractive()
|
||||||
|
|
||||||
@ -126,7 +136,7 @@ func TestIPN(t *testing.T) {
|
|||||||
authNode(t, ctl, n2.Backend)
|
authNode(t, ctl, n2.Backend)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-time.After(3 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
t.Fatalf("\n\n\nFATAL: timed out waiting for notifications.\n\n\n")
|
t.Fatalf("\n\n\nFATAL: timed out waiting for notifications.\n\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,7 +220,7 @@ type testNode struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new IPN node.
|
// Create a new IPN node.
|
||||||
func newNode(t *testing.T, logfx logger.Logf, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
|
func newNode(t *testing.T, ctx context.Context, logfx logger.Logf, prefix string, https *httptest.Server, weirdPrefs bool) testNode {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
logfe := logger.WithPrefix(logfx, prefix+"e: ")
|
logfe := logger.WithPrefix(logfx, prefix+"e: ")
|
||||||
@ -254,6 +264,7 @@ func newNode(t *testing.T, logfx logger.Logf, prefix string, https *httptest.Ser
|
|||||||
}
|
}
|
||||||
|
|
||||||
n.Start(Options{
|
n.Start(Options{
|
||||||
|
HTTPTestClient: httpc,
|
||||||
FrontendLogID: prefix + "-f",
|
FrontendLogID: prefix + "-f",
|
||||||
Prefs: prefs,
|
Prefs: prefs,
|
||||||
Notify: func(n Notify) {
|
Notify: func(n Notify) {
|
||||||
@ -275,7 +286,7 @@ func newNode(t *testing.T, logfx logger.Logf, prefix string, https *httptest.Ser
|
|||||||
}
|
}
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
if _, err := httpc.Do(req); err != nil {
|
if _, err := httpc.Do(req.WithContext(ctx)); err != nil {
|
||||||
logf("BrowseToURL: %v\n", err)
|
logf("BrowseToURL: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,13 +116,14 @@ func NewLocalBackend(logf logger.Logf, logid string, store StateStore, e wgengin
|
|||||||
// Shutdown halts the backend and all its sub-components. The backend
|
// Shutdown halts the backend and all its sub-components. The backend
|
||||||
// can no longer be used after Shutdown returns.
|
// can no longer be used after Shutdown returns.
|
||||||
func (b *LocalBackend) Shutdown() {
|
func (b *LocalBackend) Shutdown() {
|
||||||
b.ctxCancel()
|
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
cli := b.c
|
cli := b.c
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
if cli != nil {
|
if cli != nil {
|
||||||
cli.Shutdown()
|
cli.Shutdown()
|
||||||
}
|
}
|
||||||
|
b.ctxCancel()
|
||||||
b.e.Close()
|
b.e.Close()
|
||||||
b.e.Wait()
|
b.e.Wait()
|
||||||
}
|
}
|
||||||
@ -258,6 +259,7 @@ func (b *LocalBackend) Start(opts Options) error {
|
|||||||
Hostinfo: hi,
|
Hostinfo: hi,
|
||||||
KeepAlive: true,
|
KeepAlive: true,
|
||||||
NewDecompressor: b.newDecompressor,
|
NewDecompressor: b.newDecompressor,
|
||||||
|
HTTPTestClient: opts.HTTPTestClient,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user