|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
7 | 7 | "sync"
|
| 8 | + "sync/atomic" |
8 | 9 | "testing"
|
9 | 10 | "time"
|
10 | 11 |
|
@@ -129,6 +130,37 @@ func TestAppHealth(t *testing.T) {
|
129 | 130 | return apps[0].Health == codersdk.WorkspaceAppHealthUnhealthy
|
130 | 131 | }, testutil.WaitLong, testutil.IntervalSlow)
|
131 | 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 | + 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 | + }) |
132 | 164 | }
|
133 | 165 |
|
134 | 166 | func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.WorkspaceApp, handlers []http.Handler) (agent.WorkspaceAgentApps, func()) {
|
|
0 commit comments