-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathassert.go
81 lines (70 loc) · 1.36 KB
/
assert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package assert
import (
"net/http"
"testing"
"time"
)
func Equal[T comparable](t testing.TB, got, want T) {
t.Helper()
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func NoError(t testing.TB, err error) {
if err == nil {
return
}
t.Helper()
t.Fatalf("didnt expect an err, but got one %v", err)
}
func Error(t testing.TB, err error) {
t.Helper()
if err == nil {
t.Error("expected an error but didnt get one")
}
}
func SignalSent[T any](t testing.TB, signal <-chan T, signalName string) {
t.Helper()
select {
case <-signal:
case <-time.After(500 * time.Millisecond):
t.Errorf("timed out waiting %q to happen", signalName)
}
}
func CanGet(t testing.TB, url string) {
errChan := make(chan error)
go func() {
res, err := http.Get(url)
if err != nil {
errChan <- err
return
}
res.Body.Close()
errChan <- nil
}()
select {
case err := <-errChan:
NoError(t, err)
case <-time.After(3 * time.Second):
t.Errorf("timed out waiting for request to %q", url)
}
}
func CantGet(t testing.TB, url string) {
t.Helper()
errChan := make(chan error, 1)
go func() {
res, err := http.Get(url)
if err != nil {
errChan <- err
return
}
res.Body.Close()
errChan <- nil
}()
select {
case err := <-errChan:
Error(t, err)
case <-time.After(500 * time.Millisecond):
t.Errorf("timed out waiting for request to %q", url)
}
}