forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtunnel_test.go
More file actions
81 lines (64 loc) · 1.74 KB
/
tunnel_test.go
File metadata and controls
81 lines (64 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package devtunnel_test
import (
"context"
"io"
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd/devtunnel"
)
// 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()
// It's not super useful for us to test this constantly, it'll only cause
// flakes is the tunnel becomes unavailable for some reason.
t.Skip()
// if testing.Short() {
// t.Skip()
// return
// }
ctx, cancelTun := context.WithCancel(context.Background())
defer cancelTun()
server := http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
BaseContext: func(_ net.Listener) context.Context {
return ctx
},
}
cfg, err := devtunnel.GenerateConfig()
require.NoError(t, err)
tun, errCh, err := devtunnel.NewWithConfig(ctx, slogtest.Make(t, nil), cfg)
require.NoError(t, err)
t.Log(tun.URL)
go server.Serve(tun.Listener)
defer tun.Listener.Close()
httpClient := &http.Client{
Timeout: 10 * time.Second,
}
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", tun.URL, nil)
require.NoError(t, err)
res, err := httpClient.Do(req)
require.NoError(t, err)
defer res.Body.Close()
_, _ = io.Copy(io.Discard, res.Body)
return res.StatusCode == http.StatusOK
}, time.Minute, time.Second)
httpClient.CloseIdleConnections()
assert.NoError(t, server.Close())
cancelTun()
select {
case <-errCh:
case <-time.After(10 * time.Second):
t.Error("tunnel did not close after 10 seconds")
}
}