Skip to content

Commit 39cbc5b

Browse files
committed
use helper functions
1 parent 47be715 commit 39cbc5b

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

coderd/telemetry/telemetry_test.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,10 @@ func mockTelemetryServer(ctx context.Context, t *testing.T) (*url.URL, chan *tel
450450
dd := &telemetry.Deployment{}
451451
err := json.NewDecoder(r.Body).Decode(dd)
452452
require.NoError(t, err)
453-
select {
454-
case <-ctx.Done():
455-
t.Fatal("timed out sending deployment")
456-
case deployment <- dd:
453+
ok := testutil.AssertSend(ctx, t, deployment, dd)
454+
if !ok {
455+
w.WriteHeader(http.StatusInternalServerError)
456+
return
457457
}
458458
// Ensure the header is sent only after deployment is sent
459459
w.WriteHeader(http.StatusAccepted)
@@ -463,10 +463,10 @@ func mockTelemetryServer(ctx context.Context, t *testing.T) (*url.URL, chan *tel
463463
ss := &telemetry.Snapshot{}
464464
err := json.NewDecoder(r.Body).Decode(ss)
465465
require.NoError(t, err)
466-
select {
467-
case <-ctx.Done():
468-
t.Fatal("timed out sending snapshot")
469-
case snapshot <- ss:
466+
ok := testutil.AssertSend(ctx, t, snapshot, ss)
467+
if !ok {
468+
w.WriteHeader(http.StatusInternalServerError)
469+
return
470470
}
471471
// Ensure the header is sent only after snapshot is sent
472472
w.WriteHeader(http.StatusAccepted)
@@ -487,7 +487,7 @@ func collectSnapshot(
487487
) (*telemetry.Deployment, *telemetry.Snapshot) {
488488
t.Helper()
489489

490-
serverURL, deploymentChan, snapshotChan := mockTelemetryServer(ctx, t)
490+
serverURL, deployment, snapshot := mockTelemetryServer(ctx, t)
491491

492492
options := telemetry.Options{
493493
Database: db,
@@ -503,19 +503,5 @@ func collectSnapshot(
503503
require.NoError(t, err)
504504
t.Cleanup(reporter.Close)
505505

506-
var deployment *telemetry.Deployment
507-
var snapshot *telemetry.Snapshot
508-
509-
select {
510-
case <-ctx.Done():
511-
t.Fatal("timed out collecting deployment")
512-
case deployment = <-deploymentChan:
513-
}
514-
select {
515-
case <-ctx.Done():
516-
t.Fatal("timed out collecting snapshot")
517-
case snapshot = <-snapshotChan:
518-
}
519-
520-
return deployment, snapshot
506+
return testutil.RequireReceive(ctx, t, deployment), testutil.RequireReceive(ctx, t, snapshot)
521507
}

testutil/chan.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,61 @@ func RequireSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
5555
// OK!
5656
}
5757
}
58+
59+
// SoftTryReceive will attempt to receive a value from the chan and return it. If
60+
// the context expires before a value can be received, it will mark the test as
61+
// failed but continue execution. If the channel is closed, the zero value of the
62+
// channel type will be returned.
63+
// The second return value indicates whether the receive was successful. In
64+
// particular, if the channel is closed, the second return value will be true.
65+
//
66+
// Safety: can be called from any goroutine.
67+
func SoftTryReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bool) {
68+
t.Helper()
69+
select {
70+
case <-ctx.Done():
71+
t.Error("timeout")
72+
var a A
73+
return a, false
74+
case a := <-c:
75+
return a, true
76+
}
77+
}
78+
79+
// AssertReceive will receive a value from the chan and return it. If the
80+
// context expires or the channel is closed before a value can be received,
81+
// it will mark the test as failed but continue execution.
82+
// The second return value indicates whether the receive was successful.
83+
//
84+
// Safety: can be called from any goroutine.
85+
func AssertReceive[A any](ctx context.Context, t testing.TB, c <-chan A) (A, bool) {
86+
t.Helper()
87+
select {
88+
case <-ctx.Done():
89+
t.Error("timeout")
90+
var a A
91+
return a, false
92+
case a, ok := <-c:
93+
if !ok {
94+
t.Error("channel closed")
95+
}
96+
return a, ok
97+
}
98+
}
99+
100+
// AssertSend will send the given value over the chan and then return. If
101+
// the context expires before the send succeeds, it will mark the test as failed
102+
// but continue execution.
103+
// The second return value indicates whether the send was successful.
104+
//
105+
// Safety: can be called from any goroutine.
106+
func AssertSend[A any](ctx context.Context, t testing.TB, c chan<- A, a A) bool {
107+
t.Helper()
108+
select {
109+
case <-ctx.Done():
110+
t.Error("timeout")
111+
return false
112+
case c <- a:
113+
return true
114+
}
115+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy