Skip to content

Commit 5530f2a

Browse files
committed
fix: cli: server_test: avoid calling t.FailNow outside main goroutine
1 parent 53417bb commit 5530f2a

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

cli/server_test.go

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"os"
1818
"runtime"
1919
"strings"
20-
"sync"
2120
"testing"
2221
"time"
2322

@@ -45,12 +44,11 @@ func TestServer(t *testing.T) {
4544
require.NoError(t, err)
4645
defer closeFunc()
4746
ctx, cancelFunc := context.WithCancel(context.Background())
48-
done := make(chan struct{})
47+
defer cancelFunc()
4948
root, cfg := clitest.New(t, "server", "--address", ":0", "--postgres-url", connectionURL)
49+
errC := make(chan error)
5050
go func() {
51-
defer close(done)
52-
err = root.ExecuteContext(ctx)
53-
require.ErrorIs(t, err, context.Canceled)
51+
errC <- root.ExecuteContext(ctx)
5452
}()
5553
var client *codersdk.Client
5654
require.Eventually(t, func() bool {
@@ -71,8 +69,9 @@ func TestServer(t *testing.T) {
7169
})
7270
require.NoError(t, err)
7371
cancelFunc()
74-
<-done
72+
require.ErrorIs(t, <-errC, context.Canceled)
7573
})
74+
7675
t.Run("Development", func(t *testing.T) {
7776
t.Parallel()
7877
ctx, cancelFunc := context.WithCancel(context.Background())
@@ -82,26 +81,12 @@ func TestServer(t *testing.T) {
8281

8382
root, cfg := clitest.New(t, "server", "--dev", "--tunnel=false", "--address", ":0")
8483
var buf strings.Builder
84+
errC := make(chan error)
8585
root.SetOutput(&buf)
86-
var wg sync.WaitGroup
87-
wg.Add(1)
8886
go func() {
89-
defer wg.Done()
90-
91-
err := root.ExecuteContext(ctx)
92-
require.ErrorIs(t, err, context.Canceled)
93-
94-
// Verify that credentials were output to the terminal.
95-
assert.Contains(t, buf.String(), fmt.Sprintf("email: %s", wantEmail), "expected output %q; got no match", wantEmail)
96-
// Check that the password line is output and that it's non-empty.
97-
if _, after, found := strings.Cut(buf.String(), "password: "); found {
98-
before, _, _ := strings.Cut(after, "\n")
99-
before = strings.Trim(before, "\r") // Ensure no control character is left.
100-
assert.NotEmpty(t, before, "expected non-empty password; got empty")
101-
} else {
102-
t.Error("expected password line output; got no match")
103-
}
87+
errC <- root.ExecuteContext(ctx)
10488
}()
89+
10590
var token string
10691
require.Eventually(t, func() bool {
10792
var err error
@@ -119,8 +104,20 @@ func TestServer(t *testing.T) {
119104
require.NoError(t, err)
120105

121106
cancelFunc()
122-
wg.Wait()
107+
require.ErrorIs(t, <-errC, context.Canceled)
108+
109+
// Verify that credentials were output to the terminal.
110+
assert.Contains(t, buf.String(), fmt.Sprintf("email: %s", wantEmail), "expected output %q; got no match", wantEmail)
111+
// Check that the password line is output and that it's non-empty.
112+
if _, after, found := strings.Cut(buf.String(), "password: "); found {
113+
before, _, _ := strings.Cut(after, "\n")
114+
before = strings.Trim(before, "\r") // Ensure no control character is left.
115+
assert.NotEmpty(t, before, "expected non-empty password; got empty")
116+
} else {
117+
t.Error("expected password line output; got no match")
118+
}
123119
})
120+
124121
// Duplicated test from "Development" above to test setting email/password via env.
125122
// Cannot run parallel due to os.Setenv.
126123
//nolint:paralleltest
@@ -136,18 +133,11 @@ func TestServer(t *testing.T) {
136133
root, cfg := clitest.New(t, "server", "--dev", "--tunnel=false", "--address", ":0")
137134
var buf strings.Builder
138135
root.SetOutput(&buf)
139-
var wg sync.WaitGroup
140-
wg.Add(1)
136+
errC := make(chan error)
141137
go func() {
142-
defer wg.Done()
143-
144-
err := root.ExecuteContext(ctx)
145-
require.ErrorIs(t, err, context.Canceled)
146-
147-
// Verify that credentials were output to the terminal.
148-
assert.Contains(t, buf.String(), fmt.Sprintf("email: %s", wantEmail), "expected output %q; got no match", wantEmail)
149-
assert.Contains(t, buf.String(), fmt.Sprintf("password: %s", wantPassword), "expected output %q; got no match", wantPassword)
138+
errC <- root.ExecuteContext(ctx)
150139
}()
140+
151141
var token string
152142
require.Eventually(t, func() bool {
153143
var err error
@@ -165,8 +155,12 @@ func TestServer(t *testing.T) {
165155
require.NoError(t, err)
166156

167157
cancelFunc()
168-
wg.Wait()
158+
require.ErrorIs(t, <-errC, context.Canceled)
159+
// Verify that credentials were output to the terminal.
160+
assert.Contains(t, buf.String(), fmt.Sprintf("email: %s", wantEmail), "expected output %q; got no match", wantEmail)
161+
assert.Contains(t, buf.String(), fmt.Sprintf("password: %s", wantPassword), "expected output %q; got no match", wantPassword)
169162
})
163+
170164
t.Run("TLSBadVersion", func(t *testing.T) {
171165
t.Parallel()
172166
ctx, cancelFunc := context.WithCancel(context.Background())
@@ -202,10 +196,12 @@ func TestServer(t *testing.T) {
202196
certPath, keyPath := generateTLSCertificate(t)
203197
root, cfg := clitest.New(t, "server", "--dev", "--tunnel=false", "--address", ":0",
204198
"--tls-enable", "--tls-cert-file", certPath, "--tls-key-file", keyPath)
199+
errC := make(chan error)
205200
go func() {
206-
err := root.ExecuteContext(ctx)
207-
require.ErrorIs(t, err, context.Canceled)
201+
errC <- root.ExecuteContext(ctx)
208202
}()
203+
204+
// Verify HTTPS
209205
var accessURLRaw string
210206
require.Eventually(t, func() bool {
211207
var err error
@@ -226,6 +222,9 @@ func TestServer(t *testing.T) {
226222
}
227223
_, err = client.HasFirstUser(ctx)
228224
require.NoError(t, err)
225+
226+
cancelFunc()
227+
require.ErrorIs(t, <-errC, context.Canceled)
229228
})
230229
// This cannot be ran in parallel because it uses a signal.
231230
//nolint:paralleltest
@@ -284,14 +283,12 @@ func TestServer(t *testing.T) {
284283
ctx, cancelFunc := context.WithCancel(context.Background())
285284
defer cancelFunc()
286285
root, _ := clitest.New(t, "server", "--dev", "--tunnel=false", "--address", ":0", "--trace=true")
287-
done := make(chan struct{})
286+
errC := make(chan error)
288287
go func() {
289-
defer close(done)
290-
err := root.ExecuteContext(ctx)
291-
require.ErrorIs(t, err, context.Canceled)
288+
errC <- root.ExecuteContext(ctx)
292289
}()
293290
cancelFunc()
294-
<-done
291+
require.ErrorIs(t, <-errC, context.Canceled)
295292
require.Error(t, goleak.Find())
296293
})
297294
}

0 commit comments

Comments
 (0)