Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39cbc5b

Browse files
committed
use helper functions
1 parent47be715 commit39cbc5b

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-
casedeployment<-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-
casesnapshot<-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-
vardeployment*telemetry.Deployment
507-
varsnapshot*telemetry.Snapshot
508-
509-
select {
510-
case<-ctx.Done():
511-
t.Fatal("timed out collecting deployment")
512-
casedeployment=<-deploymentChan:
513-
}
514-
select {
515-
case<-ctx.Done():
516-
t.Fatal("timed out collecting snapshot")
517-
casesnapshot=<-snapshotChan:
518-
}
519-
520-
returndeployment,snapshot
506+
returntestutil.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+
funcSoftTryReceive[Aany](ctx context.Context,t testing.TB,c<-chanA) (A,bool) {
68+
t.Helper()
69+
select {
70+
case<-ctx.Done():
71+
t.Error("timeout")
72+
varaA
73+
returna,false
74+
casea:=<-c:
75+
returna,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+
funcAssertReceive[Aany](ctx context.Context,t testing.TB,c<-chanA) (A,bool) {
86+
t.Helper()
87+
select {
88+
case<-ctx.Done():
89+
t.Error("timeout")
90+
varaA
91+
returna,false
92+
casea,ok:=<-c:
93+
if!ok {
94+
t.Error("channel closed")
95+
}
96+
returna,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+
funcAssertSend[Aany](ctx context.Context,t testing.TB,cchan<-A,aA)bool {
107+
t.Helper()
108+
select {
109+
case<-ctx.Done():
110+
t.Error("timeout")
111+
returnfalse
112+
casec<-a:
113+
returntrue
114+
}
115+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp