tailscale/net/portmapper/portmapper_test.go
julianknodt caceeff374 net/portmapper: add stateful prober
Previously, the prober was stateless, and probe needed to be called manually whenever additional
probes were required. This adds a stateful prober, which can theoretically reuse clients
between runs and have smarter strategies for delays between retries, which may be crucial
depending on how slow UPnP is.

Signed-off-by: julianknodt <julianknodt@gmail.com>
2021-06-14 12:55:03 -07:00

57 lines
1.4 KiB
Go

// Copyright (c) 2021 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 portmapper
import (
"context"
"os"
"strconv"
"testing"
"time"
)
func TestCreateOrGetMapping(t *testing.T) {
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
t.Skip("skipping test without HIT_NETWORK=1")
}
c := NewClient(t.Logf)
c.SetLocalPort(1234)
for i := 0; i < 2; i++ {
if i > 0 {
time.Sleep(100 * time.Millisecond)
}
ext, err := c.CreateOrGetMapping(context.Background())
t.Logf("Got: %v, %v", ext, err)
}
}
func TestClientProbe(t *testing.T) {
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
t.Skip("skipping test without HIT_NETWORK=1")
}
c := NewClient(t.Logf)
c.NewProber(context.Background())
for i := 0; i < 30; i++ {
if i > 0 {
time.Sleep(100 * time.Millisecond)
}
res, err := c.Prober.CurrentStatus()
t.Logf("Got(t=%dms): %+v, %v", i*100, res, err)
}
}
func TestClientProbeThenMap(t *testing.T) {
if v, _ := strconv.ParseBool(os.Getenv("HIT_NETWORK")); !v {
t.Skip("skipping test without HIT_NETWORK=1")
}
c := NewClient(t.Logf)
c.SetLocalPort(1234)
c.NewProber(context.Background())
res, err := c.Prober.StatusBlock()
t.Logf("Probe: %+v, %v", res, err)
ext, err := c.CreateOrGetMapping(context.Background())
t.Logf("CreateOrGetMapping: %v, %v", ext, err)
}