8000 something · coder/coder@b3798c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3798c4

Browse files
committed
something
1 parent e8c1b31 commit b3798c4

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

testutil/chan.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package testutil
2+
3+
import (
4+
"context"
5+
"testing"
6+
)
7+
8+
// TryRecvCtx will attempt to receive a value from the chan and return it. If
9+
// the context expires before a value can be received, it will fail the test. If
10+
// the channel is closed, the zero value of the channel type will be returned.
11+
//
12+
// Safety: Must only be called from the Go routine that created `t`.
13+
func TryRecvCtx[A any](ctx context.Context, t testing.TB, c <-chan A) A {
14+
t.Helper()
15+
select {
16+
case <-ctx.Done():
17+
t.Fatal("timeout")
18+
var a A
19+
return a
20+
case a := <-c:
21+
return a
22+
}
23+
}
24+
25+
// RequireRecvCtx will receive a value from the chan and return it. If the
26+
// context expires or the channel is closed before a value can be received,
27+
// it will fail the test.
28+
//
29+
// Safety: Must only be called from the Go routine that created `t`.
30+
func RequireRecvCtx[A any](ct 8000 x context.Context, t testing.TB, c <-chan A) A {
31+
t.Helper()
32+
select {
33+
case <-ctx.Done():
34+
t.Fatal("timeout")
35+
var a A
36+
return a
37+
case a, ok := <-c:
38+
if !ok {
39+
t.Fatal("channel closed")
40+
}
41+
return a
42+
}
43+
}
44+
45+
// RequireSendCtx will send the given value over the chan and then return. If
46+
// the context expires before the send succeeds, it will fail the test.
47+
//
48+
// Safety: Must only be called from the Go routine that created `t`.
49+
func RequireSendCtx[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
50+
t.Helper()
51+
select {
52+
case <-ctx.Done():
53+
t.Fatal("timeout")
54+
case c <- a:
55+
// OK!
56+
}
57+
}

testutil/ctx.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,3 @@ func Context(t *testing.T, dur time.Duration) context.Context {
1111
t.Cleanup(cancel)
1212
return ctx
1313
}
14-
15-
// TryRecvCtx will attempt to receive a value from the chan and return it. If
16-
// the context expires before a value can be received, it will fail the test. If
17-
// the channel is closed, the zero value of the channel type will be returned.
18-
//
19-
// Safety: Must only be called from the Go routine that created `t`.
20-
func TryRecvCtx[A any](ctx context.Context, t testing.TB, c <-chan A) A {
21-
t.Helper()
22-
select {
23-
case <-ctx.Done():
24-
t.Fatal("timeout")
25-
var a A
26-
return a
27-
case a := <-c:
28-
return a
29-
}
30-
}
31-
32-
// RequireRecvCtx will receive a value from the chan and return it. If the
33-
// context expires or the channel is closed before a value can be received,
34-
// it will fail the test.
35-
//
36-
// Safety: Must only be called from the Go routine that created `t`.
37-
func RequireRecvCtx[A any](ctx context.Context, t testing.TB, c <-chan A) A {
38-
t.Helper()
39-
select {
40-
case <-ctx.Done():
41-
t.Fatal("timeout")
42-
var a A
43-
return a
44-
case a, ok := <-c:
45-
if !ok {
46-
t.Fatal("channel closed")
47-
}
48-
return a
49-
}
50-
}
51-
52-
// RequireSendCtx will send the given value over the chan and then return. If
53-
// the context expires before the send succeeds, it will fail the test.
54-
//
55-
// Safety: Must only be called from the Go routine that created `t`.
56-
func RequireSendCtx[A any](ctx context.Context, t testing.TB, c chan<- A, a A) {
57-
t.Helper()
58-
select {
59-
case <-ctx.Done():
60-
t.Fatal("timeout")
61-
case c <- a:
62-
// OK!
63-
}
64-
}

0 commit comments

Comments
 (0)
0