@@ -19,148 +19,145 @@ import (
19
19
"github.com/coder/coder/testutil"
20
20
)
21
21
22
- func TestAppHealth (t * testing.T ) {
22
+ func TestAppHealth_Healthy (t * testing.T ) {
23
23
t .Parallel ()
24
- t .Run ("Healthy" , func (t * testing.T ) {
25
- t .Parallel ()
26
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
27
- defer cancel ()
28
- apps := []codersdk.WorkspaceApp {
29
- {
30
- Slug : "app1" ,
31
- Healthcheck : codersdk.Healthcheck {},
32
- Health : codersdk .WorkspaceAppHealthDisabled ,
24
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
25
+ defer cancel ()
26
+ apps := []codersdk.WorkspaceApp {
27
+ {
28
+ Slug : "app1" ,
29
+ Healthcheck : codersdk.Healthcheck {},
30
+ Health : codersdk .WorkspaceAppHealthDisabled ,
31
+ },
32
+ {
33
+ Slug : "app2" ,
34
+ Healthcheck : codersdk.Healthcheck {
35
+ // URL: We don't set the URL for this test because the setup will
36
+ // create a httptest server for us and set it for us.
37
+ Interval : 1 ,
38
+ Threshold : 1 ,
33
39
},
34
- {
35
- Slug : "app2" ,
36
- Healthcheck : codersdk.Healthcheck {
37
- // URL: We don't set the URL for this test because the setup will
38
- // create a httptest server for us and set it for us.
39
- Interval : 1 ,
40
- Threshold : 1 ,
41
- },
42
- Health : codersdk .WorkspaceAppHealthInitializing ,
43
- },
44
- }
45
- handlers := []http.Handler {
46
- nil ,
47
- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
48
- httpapi .Write (r .Context (), w , http .StatusOK , nil )
49
- }),
50
- }
51
- getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
52
- defer closeFn ()
40
+ Health : codersdk .WorkspaceAppHealthInitializing ,
41
+ },
42
+ }
43
+ handlers := []http.Handler {
44
+ nil ,
45
+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
46
+ httpapi .Write (r .Context (), w , http .StatusOK , nil )
47
+ }),
48
+ }
49
+ getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
50
+ defer closeFn ()
51
+ apps , err := getApps (ctx )
52
+ require .NoError (t , err )
53
+ require .EqualValues (t , codersdk .WorkspaceAppHealthDisabled , apps [0 ].Health )
54
+ require .Eventually (t , func () bool {
53
55
apps , err := getApps (ctx )
54
- require .NoError (t , err )
55
- require .EqualValues (t , codersdk .WorkspaceAppHealthDisabled , apps [0 ].Health )
56
- require .Eventually (t , func () bool {
57
- apps , err := getApps (ctx )
58
- if err != nil {
59
- return false
60
- }
56
+ if err != nil {
57
+ return false
58
+ }
61
59
62
- return apps [1 ].Health == codersdk .WorkspaceAppHealthHealthy
63
- }, testutil .WaitLong , testutil .IntervalSlow )
64
- })
65
-
66
- t .Run ("500" , func (t * testing.T ) {
67
- t .Parallel ()
68
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
69
- defer cancel ()
70
- apps := []codersdk.WorkspaceApp {
71
- {
72
- Slug : "app2" ,
73
- Healthcheck : codersdk.Healthcheck {
74
- // URL: We don't set the URL for this test because the setup will
75
- // create a httptest server for us and set it for us.
76
- Interval : 1 ,
77
- Threshold : 1 ,
78
- },
79
- Health : codersdk .WorkspaceAppHealthInitializing ,
60
+ return apps [1 ].Health == codersdk .WorkspaceAppHealthHealthy
61
+ }, testutil .WaitLong , testutil .IntervalSlow )
62
+ }
63
+
64
+ func TestAppHealth_500 (t * testing.T ) {
65
+ t .Parallel ()
66
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
67
+ defer cancel ()
68
+ apps := []codersdk.WorkspaceApp {
69
+ {
70
+ Slug : "app2" ,
71
+ Healthcheck : codersdk.Healthcheck {
72
+ // URL: We don't set the URL for this test because the setup will
73
+ // create a httptest server for us and set it for us.
74
+ Interval : 1 ,
75
+ Threshold : 1 ,
80
76
},
77
+ Health : codersdk .WorkspaceAppHealthInitializing ,
78
+ },
79
+ }
80
+ handlers := []http.Handler {
81
+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
82
+ httpapi .Write (r .Context (), w , http .StatusInternalServerError , nil )
83
+ }),
84
+ }
85
+ getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
86
+ defer closeFn ()
87
+ require .Eventually (t , func () bool {
88
+ apps , err := getApps (ctx )
89
+ if err != nil {
90
+ return false
81
91
}
82
- handlers := []http.Handler {
83
- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
84
- httpapi .Write (r .Context (), w , http .StatusInternalServerError , nil )
85
- }),
86
- }
87
- getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
88
- defer closeFn ()
89
- require .Eventually (t , func () bool {
90
- apps , err := getApps (ctx )
91
- if err != nil {
92
- return false
93
- }
94
92
95
- return apps [0 ].Health == codersdk .WorkspaceAppHealthUnhealthy
96
- }, testutil .WaitLong , testutil .IntervalSlow )
97
- })
98
-
99
- t .Run ("Timeout" , func (t * testing.T ) {
100
- t .Parallel ()
101
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
102
- defer cancel ()
103
- apps := []codersdk.WorkspaceApp {
104
- {
105
- Slug : "app2" ,
106
- Healthcheck : codersdk.Healthcheck {
107
- // URL: We don't set the URL for this test because the setup will
108
- // create a httptest server for us and set it for us.
109
- Interval : 1 ,
110
- Threshold : 1 ,
111
- },
112
- Health : codersdk .WorkspaceAppHealthInitializing ,
93
+ return apps [0 ].Health == codersdk .WorkspaceAppHealthUnhealthy
94
+ }, testutil .WaitLong , testutil .IntervalSlow )
95
+ }
96
+
97
+ func TestAppHealth_Timeout (t * testing.T ) {
98
+ t .Parallel ()
99
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
100
+ defer cancel ()
101
+ apps := []codersdk.WorkspaceApp {
102
+ {
103
+ Slug : "app2" ,
104
+ Healthcheck : codersdk.Healthcheck {
105
+ // URL: We don't set the URL for this test because the setup will
106
+ // create a httptest server for us and set it for us.
107
+ Interval : 1 ,
108
+ Threshold : 1 ,
113
109
},
110
+ Health : codersdk .WorkspaceAppHealthInitializing ,
111
+ },
112
+ }
113
+ handlers := []http.Handler {
114
+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
115
+ // sleep longer than the interval to cause the health check to time out
116
+ time .Sleep (2 * time .Second )
117
+ httpapi .Write (r .Context (), w , http .StatusOK , nil )
118
+ }),
119
+ }
120
+ getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
121
+ defer closeFn ()
122
+ require .Eventually (t , func () bool {
123
+ apps , err := getApps (ctx )
124
+ if err != nil {
125
+ return false
114
126
}
115
- handlers := []http.Handler {
116
- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
117
- // sleep longer than the interval to cause the health check to time out
118
- time .Sleep (2 * time .Second )
119
- httpapi .Write (r .Context (), w , http .StatusOK , nil )
120
- }),
121
- }
122
- getApps , closeFn := setupAppReporter (ctx , t , apps , handlers )
123
- defer closeFn ()
124
- require .Eventually (t , func () bool {
125
- apps , err := getApps (ctx )
126
- if err != nil {
127
- return false
128
- }
129
127
130
- return apps [0 ].Health == codersdk .WorkspaceAppHealthUnhealthy
131
- }, testutil .WaitLong , testutil .IntervalSlow )
132
- })
133
-
134
- t .Run ("NotSpamming" , func (t * testing.T ) {
135
- t .Parallel ()
136
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
137
- defer cancel ()
138
- apps := []codersdk.WorkspaceApp {
139
- {
140
- Slug : "app2" ,
141
- Healthcheck : codersdk.Healthcheck {
142
- // URL: We don't set the URL for this test because the setup will
143
- // create a httptest server for us and set it for us.
144
- Interval : 1 ,
145
- Threshold : 1 ,
146
- },
147
- Health : codersdk .WorkspaceAppHealthInitializing ,
128
+ return apps [0 ].Health == codersdk .WorkspaceAppHealthUnhealthy
129
+ }, testutil .WaitLong , testutil .IntervalSlow )
130
+ }
131
+
132
+ func TestAppHealth_NotSpamming (t * testing.T ) {
133
+ t .Parallel ()
134
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
135
+ defer cancel ()
136
+ apps := []codersdk.WorkspaceApp {
137
+ {
138
+ Slug : "app2" ,
139
+ Healthcheck : codersdk.Healthcheck {
140
+ // URL: We don't set the URL for this test because the setup will
141
+ // create a httptest server for us and set it for us.
142
+ Interval : 1 ,
143
+ Threshold : 1 ,
148
144
},
149
- }
145
+ Health : codersdk .WorkspaceAppHealthInitializing ,
146
+ },
147
+ }
150
148
151
- var counter = new (int32 )
152
- handlers := []http.Handler {
153
- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
154
- atomic .AddInt32 (counter , 1 )
155
- }),
156
- }
157
- _ , closeFn := setupAppReporter (ctx , t , apps , handlers )
158
- defer closeFn ()
159
- // Ensure we haven't made more than 2 (expected 1 + 1 for buffer) requests in the last second.
160
- // if there is a bug where we are spamming the healthcheck route this will catch it.
161
- time .Sleep (time .Second )
162
- require .LessOrEqual (t , * counter , int32 (2 ))
163
- })
149
+ counter := new (int32 )
150
+ handlers := []http.Handler {
151
+ http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
152
+ atomic .AddInt32 (counter , 1 )
153
+ }),
154
+ }
155
+ _ , closeFn := setupAppReporter (ctx , t , apps , handlers )
156
+ defer closeFn ()
157
+ // Ensure we haven't made more than 2 (expected 1 + 1 for buffer) requests in the last second.
158
+ // if there is a bug where we are spamming the healthcheck route this will catch it.
159
+ time .Sleep (time .Second )
160
+ require .LessOrEqual (t , * counter , int32 (2 ))
164
161
}
165
162
166
163
func setupAppReporter (ctx context.Context , t * testing.T , apps []codersdk.WorkspaceApp , handlers []http.Handler ) (agent.WorkspaceAgentApps , func ()) {
0 commit comments