(t *testing.T)
| 162 | } |
| 163 | |
| 164 | func TestAppHealth_Timeout(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 167 | defer cancel() |
| 168 | apps := []codersdk.WorkspaceApp{ |
| 169 | { |
| 170 | ID: uuid.UUID{2}, |
| 171 | Slug: "app2", |
| 172 | Healthcheck: codersdk.Healthcheck{ |
| 173 | // URL: We don't set the URL for this test because the setup will |
| 174 | // create a httptest server for us and set it for us. |
| 175 | Interval: 1, |
| 176 | Threshold: 1, |
| 177 | }, |
| 178 | Health: codersdk.WorkspaceAppHealthInitializing, |
| 179 | }, |
| 180 | } |
| 181 | |
| 182 | handlers := []http.Handler{ |
| 183 | http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 184 | // allow the request to time out |
| 185 | <-r.Context().Done() |
| 186 | }), |
| 187 | } |
| 188 | mClock := quartz.NewMock(t) |
| 189 | start := mClock.Now() |
| 190 | |
| 191 | // for this test, it's easier to think in the number of milliseconds elapsed |
| 192 | // since start. |
| 193 | ms := func(n int) time.Time { |
| 194 | return start.Add(time.Duration(n) * time.Millisecond) |
| 195 | } |
| 196 | healthcheckTrap := mClock.Trap().TickerFunc("healthcheck") |
| 197 | defer healthcheckTrap.Close() |
| 198 | reportTrap := mClock.Trap().TickerFunc("report") |
| 199 | defer reportTrap.Close() |
| 200 | timeoutTrap := mClock.Trap().AfterFunc("timeout") |
| 201 | defer timeoutTrap.Close() |
| 202 | |
| 203 | fakeAPI, closeFn := setupAppReporter(ctx, t, apps, handlers, mClock) |
| 204 | defer closeFn() |
| 205 | healthcheckTrap.MustWait(ctx).MustRelease(ctx) |
| 206 | // advance the clock 1ms before the report ticker starts, so that it's not |
| 207 | // simultaneous with the checks. |
| 208 | mClock.Set(ms(1)).MustWait(ctx) |
| 209 | reportTrap.MustWait(ctx).MustRelease(ctx) |
| 210 | |
| 211 | w := mClock.Set(ms(1000)) // 1st check starts |
| 212 | timeoutTrap.MustWait(ctx).MustRelease(ctx) |
| 213 | mClock.Set(ms(1001)).MustWait(ctx) // report tick, no change |
| 214 | mClock.Set(ms(1999)) // timeout pops |
| 215 | w.MustWait(ctx) // 1st check finished |
| 216 | w = mClock.Set(ms(2000)) // 2nd check starts |
| 217 | timeoutTrap.MustWait(ctx).MustRelease(ctx) |
| 218 | mClock.Set(ms(2001)).MustWait(ctx) // report tick, no change |
| 219 | mClock.Set(ms(2999)) // timeout pops |
| 220 | w.MustWait(ctx) // 2nd check finished |
| 221 | // app is now unhealthy after 2 timeouts |
nothing calls this directly
no test coverage detected