MCPcopy Index your code
hub / github.com/coder/coder / TestAppHealth_500

Function TestAppHealth_500

agent/apphealth_test.go:115–162  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

113}
114
115func TestAppHealth_500(t *testing.T) {
116 t.Parallel()
117 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
118 defer cancel()
119 apps := []codersdk.WorkspaceApp{
120 {
121 ID: uuid.UUID{2},
122 Slug: "app2",
123 Healthcheck: codersdk.Healthcheck{
124 // URL: We don't set the URL for this test because the setup will
125 // create a httptest server for us and set it for us.
126 Interval: 1,
127 Threshold: 1,
128 },
129 Health: codersdk.WorkspaceAppHealthInitializing,
130 },
131 }
132 handlers := []http.Handler{
133 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
134 httpapi.Write(r.Context(), w, http.StatusInternalServerError, nil)
135 }),
136 }
137
138 mClock := quartz.NewMock(t)
139 healthcheckTrap := mClock.Trap().TickerFunc("healthcheck")
140 defer healthcheckTrap.Close()
141 reportTrap := mClock.Trap().TickerFunc("report")
142 defer reportTrap.Close()
143
144 fakeAPI, closeFn := setupAppReporter(ctx, t, slices.Clone(apps), handlers, mClock)
145 defer closeFn()
146 healthcheckTrap.MustWait(ctx).MustRelease(ctx)
147 // advance the clock 1ms before the report ticker starts, so that it's not
148 // simultaneous with the checks.
149 mClock.Advance(time.Millisecond).MustWait(ctx)
150 reportTrap.MustWait(ctx).MustRelease(ctx)
151
152 mClock.Advance(999 * time.Millisecond).MustWait(ctx) // check gets triggered
153 mClock.Advance(time.Millisecond).MustWait(ctx) // report gets triggered, but unsent since we are at the threshold
154
155 mClock.Advance(999 * time.Millisecond).MustWait(ctx) // 2nd check, crosses threshold
156 mClock.Advance(time.Millisecond).MustWait(ctx) // 2nd report, sends update
157
158 update := testutil.TryReceive(ctx, t, fakeAPI.AppHealthCh())
159 require.Len(t, update.GetUpdates(), 1)
160 applyUpdate(t, apps, update)
161 require.Equal(t, codersdk.WorkspaceAppHealthUnhealthy, apps[0].Health)
162}
163
164func TestAppHealth_Timeout(t *testing.T) {
165 t.Parallel()

Callers

nothing calls this directly

Calls 11

WriteFunction · 0.92
TryReceiveFunction · 0.92
setupAppReporterFunction · 0.85
applyUpdateFunction · 0.85
AppHealthChMethod · 0.80
GetUpdatesMethod · 0.80
ContextMethod · 0.65
CloseMethod · 0.65
CloneMethod · 0.45
LenMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected