Skip to content

Commit 5d10e11

Browse files
committed
telemetry snapshot timeouts
1 parent 34494fb commit 5d10e11

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

coderd/telemetry/telemetry_test.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package telemetry_test
22

33
import (
4+
"context"
45
"database/sql"
56
"encoding/json"
67
"net/http"
@@ -115,7 +116,7 @@ func TestTelemetry(t *testing.T) {
115116
_ = dbgen.WorkspaceAgentMemoryResourceMonitor(t, db, database.WorkspaceAgentMemoryResourceMonitor{})
116117
_ = dbgen.WorkspaceAgentVolumeResourceMonitor(t, db, database.WorkspaceAgentVolumeResourceMonitor{})
117118

118-
_, snapshot := collectSnapshot(t, db, nil)
119+
_, snapshot := collectSnapshot(ctx, t, db, nil)
119120
require.Len(t, snapshot.ProvisionerJobs, 1)
120121
require.Len(t, snapshot.Licenses, 1)
121122
require.Len(t, snapshot.Templates, 1)
@@ -168,17 +169,19 @@ func TestTelemetry(t *testing.T) {
168169
})
169170
t.Run("HashedEmail", func(t *testing.T) {
170171
t.Parallel()
172+
ctx := testutil.Context(t, testutil.WaitMedium)
171173
db := dbmem.New()
172174
_ = dbgen.User(t, db, database.User{
173175
Email: "kyle@coder.com",
174176
})
175-
_, snapshot := collectSnapshot(t, db, nil)
177+
_, snapshot := collectSnapshot(ctx, t, db, nil)
176178
require.Len(t, snapshot.Users, 1)
177179
require.Equal(t, snapshot.Users[0].EmailHashed, "bb44bf07cf9a2db0554bba63a03d822c927deae77df101874496df5a6a3e896d@coder.com")
178180
})
179181
t.Run("HashedModule", func(t *testing.T) {
180182
t.Parallel()
181183
db, _ := dbtestutil.NewDB(t)
184+
ctx := testutil.Context(t, testutil.WaitMedium)
182185
pj := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{})
183186
_ = dbgen.WorkspaceModule(t, db, database.WorkspaceModule{
184187
JobID: pj.ID,
@@ -190,7 +193,7 @@ func TestTelemetry(t *testing.T) {
190193
Source: "https://internal-url.com/some-module",
191194
Version: "1.0.0",
192195
})
193-
_, snapshot := collectSnapshot(t, db, nil)
196+
_, snapshot := collectSnapshot(ctx, t, db, nil)
194197
require.Len(t, snapshot.WorkspaceModules, 2)
195198
modules := snapshot.WorkspaceModules
196199
sort.Slice(modules, func(i, j int) bool {
@@ -286,11 +289,11 @@ func TestTelemetry(t *testing.T) {
286289
db, _ := dbtestutil.NewDB(t)
287290

288291
// 1. No org sync settings
289-
deployment, _ := collectSnapshot(t, db, nil)
292+
deployment, _ := collectSnapshot(ctx, t, db, nil)
290293
require.False(t, *deployment.IDPOrgSync)
291294

292295
// 2. Org sync settings set in server flags
293-
deployment, _ = collectSnapshot(t, db, func(opts telemetry.Options) telemetry.Options {
296+
deployment, _ = collectSnapshot(ctx, t, db, func(opts telemetry.Options) telemetry.Options {
294297
opts.DeploymentConfig = &codersdk.DeploymentValues{
295298
OIDC: codersdk.OIDCConfig{
296299
OrganizationField: "organizations",
@@ -312,16 +315,17 @@ func TestTelemetry(t *testing.T) {
312315
AssignDefault: true,
313316
})
314317
require.NoError(t, err)
315-
deployment, _ = collectSnapshot(t, db, nil)
318+
deployment, _ = collectSnapshot(ctx, t, db, nil)
316319
require.True(t, *deployment.IDPOrgSync)
317320
})
318321
}
319322

320323
// nolint:paralleltest
321324
func TestTelemetryInstallSource(t *testing.T) {
322325
t.Setenv("CODER_TELEMETRY_INSTALL_SOURCE", "aws_marketplace")
326+
ctx := testutil.Context(t, testutil.WaitMedium)
323327
db := dbmem.New()
324-
deployment, _ := collectSnapshot(t, db, nil)
328+
deployment, _ := collectSnapshot(ctx, t, db, nil)
325329
require.Equal(t, "aws_marketplace", deployment.InstallSource)
326330
}
327331

@@ -436,7 +440,7 @@ func TestRecordTelemetryStatus(t *testing.T) {
436440
}
437441
}
438442

439-
func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, chan *telemetry.Snapshot) {
443+
func mockTelemetryServer(ctx context.Context, t *testing.T) (*url.URL, chan *telemetry.Deployment, chan *telemetry.Snapshot) {
440444
t.Helper()
441445
deployment := make(chan *telemetry.Deployment, 64)
442446
snapshot := make(chan *telemetry.Snapshot, 64)
@@ -446,7 +450,11 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
446450
dd := &telemetry.Deployment{}
447451
err := json.NewDecoder(r.Body).Decode(dd)
448452
require.NoError(t, err)
449-
deployment <- dd
453+
select {
454+
case <-ctx.Done():
455+
t.Fatal("timed out sending deployment")
456+
case deployment <- dd:
457+
}
450458
// Ensure the header is sent only after deployment is sent
451459
w.WriteHeader(http.StatusAccepted)
452460
})
@@ -455,7 +463,11 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
455463
ss := &telemetry.Snapshot{}
456464
err := json.NewDecoder(r.Body).Decode(ss)
457465
require.NoError(t, err)
458-
snapshot <- ss
466+
select {
467+
case <-ctx.Done():
468+
t.Fatal("timed out sending snapshot")
469+
case snapshot <- ss:
470+
}
459471
// Ensure the header is sent only after snapshot is sent
460472
w.WriteHeader(http.StatusAccepted)
461473
})
@@ -467,10 +479,15 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
467479
return serverURL, deployment, snapshot
468480
}
469481

470-
func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts telemetry.Options) telemetry.Options) (*telemetry.Deployment, *telemetry.Snapshot) {
482+
func collectSnapshot(
483+
ctx context.Context,
484+
t *testing.T,
485+
db database.Store,
486+
addOptionsFn func(opts telemetry.Options) telemetry.Options,
487+
) (*telemetry.Deployment, *telemetry.Snapshot) {
471488
t.Helper()
472489

473-
serverURL, deployment, snapshot := mockTelemetryServer(t)
490+
serverURL, deploymentChan, snapshotChan := mockTelemetryServer(ctx, t)
474491

475492
options := telemetry.Options{
476493
Database: db,
@@ -485,5 +502,20 @@ func collectSnapshot(t *testing.T, db database.Store, addOptionsFn func(opts tel
485502
reporter, err := telemetry.New(options)
486503
require.NoError(t, err)
487504
t.Cleanup(reporter.Close)
488-
return <-deployment, <-snapshot
505+
506+
var deployment *telemetry.Deployment
507+
var snapshot *telemetry.Snapshot
508+
509+
select {
510+
case <-ctx.Done():
511+
t.Fatal("timed out collecting deployment")
512+
case deployment = <-deploymentChan:
513+
}
514+
select {
515+
case <-ctx.Done():
516+
t.Fatal("timed out collecting snapshot")
517+
case snapshot = <-snapshotChan:
518+
}
519+
520+
return deployment, snapshot
489521
}

0 commit comments

Comments
 (0)