4
4
"context"
5
5
"io"
6
6
"net/http"
7
+ "sync/atomic"
7
8
"testing"
8
9
"time"
9
10
@@ -22,24 +23,66 @@ func TestDebugHealth(t *testing.T) {
22
23
t .Parallel ()
23
24
24
25
var (
26
+ calls = atomic.Int64 {}
25
27
ctx , cancel = context .WithTimeout (context .Background (), testutil .WaitShort )
26
28
sessionToken string
27
29
client = coderdtest .New (t , & coderdtest.Options {
28
30
HealthcheckFunc : func (_ context.Context , apiKey string ) * healthcheck.Report {
31
+ calls .Add (1 )
29
32
assert .Equal (t , sessionToken , apiKey )
30
- return & healthcheck.Report {}
33
+ return & healthcheck.Report {
34
+ Time : time .Now (),
35
+ }
31
36
},
37
+ HealthcheckRefresh : time .Hour , // Avoid flakes.
32
38
})
33
39
_ = coderdtest .CreateFirstUser (t , client )
34
40
)
35
41
defer cancel ()
36
42
37
43
sessionToken = client .SessionToken ()
38
- res , err := client .Request (ctx , "GET" , "/api/v2/debug/health" , nil )
39
- require .NoError (t , err )
40
- defer res .Body .Close ()
41
- _ , _ = io .ReadAll (res .Body )
42
- require .Equal (t , http .StatusOK , res .StatusCode )
44
+ for i := 0 ; i < 10 ; i ++ {
45
+ res , err := client .Request (ctx , "GET" , "/api/v2/debug/health" , nil )
46
+ require .NoError (t , err )
47
+ _ , _ = io .ReadAll (res .Body )
48
+ res .Body .Close ()
49
+ require .Equal (t , http .StatusOK , res .StatusCode )
50
+ }
51
+ // The healthcheck should only have been called once.
52
+ require .EqualValues (t , 1 , calls .Load ())
53
+ })
54
+
55
+ t .Run ("Forced" , func (t * testing.T ) {
56
+ t .Parallel ()
57
+
58
+ var (
59
+ calls = atomic.Int64 {}
60
+ ctx , cancel = context .WithTimeout (context .Background (), testutil .WaitShort )
61
+ sessionToken string
62
+ client = coderdtest .New (t , & coderdtest.Options {
63
+ HealthcheckFunc : func (_ context.Context , apiKey string ) * healthcheck.Report {
64
+ calls .Add (1 )
65
+ assert .Equal (t , sessionToken , apiKey )
66
+ return & healthcheck.Report {
67
+ Time : time .Now (),
68
+ }
69
+ },
70
+ HealthcheckRefresh : time .Hour , // Avoid flakes.
71
+ })
72
+ _ = coderdtest .CreateFirstUser (t , client )
73
+ )
74
+ defer cancel ()
75
+
76
+ sessionToken = client .SessionToken ()
77
+ for i := 0 ; i < 10 ; i ++ {
78
+ res , err := client .Request (ctx , "GET" , "/api/v2/debug/health?force=true" , nil )
79
+ require .NoError (t , err )
80
+ _ , _ = io .ReadAll (res .Body )
81
+ res .Body .Close ()
82
+ require .Equal (t , http .StatusOK , res .StatusCode )
83
+ }
84
+ // The healthcheck func should have been called each time.
85
+ require .EqualValues (t , 10 , calls .Load ())
43
86
})
44
87
45
88
t .Run ("Timeout" , func (t * testing.T ) {
0 commit comments