Skip to content

Commit 20bcb04

Browse files
authored
fix: use correct interval for healthcheck loop (#4212)
1 parent c86fc6e commit 20bcb04

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

agent/apphealth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func NewWorkspaceAppHealthReporter(logger slog.Logger, workspaceAgentApps Worksp
109109
mu.Unlock()
110110
}
111111

112-
t.Reset(time.Duration(app.Healthcheck.Interval))
112+
t.Reset(time.Duration(app.Healthcheck.Interval) * time.Second)
113113
}
114114
}()
115115
}

agent/apphealth_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/http/httptest"
77
"sync"
8+
"sync/atomic"
89
"testing"
910
"time"
1011

@@ -129,6 +130,37 @@ func TestAppHealth(t *testing.T) {
129130
return apps[0].Health == codersdk.WorkspaceAppHealthUnhealthy
130131
}, testutil.WaitLong, testutil.IntervalSlow)
131132
})
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+
Name: "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,
148+
},
149+
}
150+
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+
})
132164
}
133165

134166
func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.WorkspaceApp, handlers []http.Handler) (agent.WorkspaceAgentApps, func()) {

0 commit comments

Comments
 (0)