Files
coder/coderd/tunnel/tunnel_test.go
Kyle Carberry 26d24f4508 chore: Improve CI builds by caching Go modules (#528)
* chore: Improve CI builds by caching Go modules

* Skip running with `race` on non-Linux systems

* Fix darwin file descriptor error

* Fix log after close

* Improve PostgreSQL test speeds

* Fix parallel connections with PostgreSQL tests

* Fix CI flake

* Separate test/go into PostgreSQL
2022-03-22 17:09:04 -05:00

57 lines
1.3 KiB
Go

package tunnel_test
import (
"context"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/coderd/tunnel"
)
// The tunnel leaks a few goroutines that aren't impactful to production scenarios.
// func TestMain(m *testing.M) {
// goleak.VerifyTestMain(m)
// }
func TestTunnel(t *testing.T) {
t.Parallel()
if testing.Short() || os.Getenv("CI") != "" {
// This test has extreme inconsistency in CI.
// It's something with the networking in CI that causes this test to flake.
t.Skip()
return
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(srv.Close)
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
url, _, err := tunnel.New(ctx, srv.URL)
require.NoError(t, err)
t.Log(url)
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
var dnsErr *net.DNSError
// The name might take a bit to resolve!
if xerrors.As(err, &dnsErr) {
return false
}
require.NoError(t, err)
defer res.Body.Close()
return res.StatusCode == http.StatusOK
}, 5*time.Minute, 3*time.Second)
}