Source file
src/net/http/async_test.go
1
2
3
4
5 package http_test
6
7 import (
8 "errors"
9 "internal/synctest"
10 )
11
12 var errStillRunning = errors.New("async op still running")
13
14 type asyncResult[T any] struct {
15 donec chan struct{}
16 res T
17 err error
18 }
19
20
21
22
23
24 func runAsync[T any](f func() (T, error)) *asyncResult[T] {
25 r := &asyncResult[T]{
26 donec: make(chan struct{}),
27 }
28 go func() {
29 defer close(r.donec)
30 r.res, r.err = f()
31 }()
32 synctest.Wait()
33 return r
34 }
35
36
37 func (r *asyncResult[T]) done() bool {
38 _, err := r.result()
39 return err != errStillRunning
40 }
41
42
43
44 func (r *asyncResult[T]) result() (T, error) {
45 select {
46 case <-r.donec:
47 return r.res, r.err
48 default:
49 var zero T
50 return zero, errStillRunning
51 }
52 }
53
View as plain text