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

Function TestBatchStats

coderd/workspacestats/batcher_internal_test.go:22–129  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20)
21
22func TestBatchStats(t *testing.T) {
23 t.Parallel()
24
25 // Given: a fresh batcher with no data
26 ctx, cancel := context.WithCancel(context.Background())
27 t.Cleanup(cancel)
28 log := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
29 store, ps := dbtestutil.NewDB(t)
30
31 // Set up some test dependencies.
32 deps1 := setupDeps(t, store, ps)
33 deps2 := setupDeps(t, store, ps)
34 tick := make(chan time.Time)
35 flushed := make(chan int, 1)
36
37 b, closer, err := NewBatcher(ctx,
38 BatcherWithStore(store),
39 BatcherWithLogger(log),
40 func(b *DBBatcher) {
41 b.tickCh = tick
42 b.flushed = flushed
43 },
44 )
45 require.NoError(t, err)
46 t.Cleanup(closer)
47
48 // Given: no data points are added for workspace
49 // When: it becomes time to report stats
50 t1 := dbtime.Now()
51 // Signal a tick and wait for a flush to complete.
52 tick <- t1
53 f := <-flushed
54 require.Equal(t, 0, f, "expected no data to be flushed")
55 t.Log("flush 1 completed")
56
57 // Then: it should report no stats.
58 stats, err := store.GetWorkspaceAgentStats(ctx, t1)
59 require.NoError(t, err, "should not error getting stats")
60 require.Empty(t, stats, "should have no stats for workspace")
61
62 // Given: a single data point is added for workspace
63 t2 := t1.Add(time.Second)
64 t.Log("inserting 1 stat")
65 b.Add(t2.Add(time.Millisecond), deps1.Agent.ID, deps1.User.ID, deps1.Template.ID, deps1.Workspace.ID, randStats(t), false)
66
67 // When: it becomes time to report stats
68 // Signal a tick and wait for a flush to complete.
69 tick <- t2
70 f = <-flushed // Wait for a flush to complete.
71 require.Equal(t, 1, f, "expected one stat to be flushed")
72 t.Log("flush 2 completed")
73
74 // Then: it should report a single stat.
75 stats, err = store.GetWorkspaceAgentStats(ctx, t2)
76 require.NoError(t, err, "should not error getting stats")
77 require.Len(t, stats, 1, "should have stats for workspace")
78
79 // Given: a lot of data points are added for both workspaces

Callers

nothing calls this directly

Calls 15

AddMethod · 0.95
NewDBFunction · 0.92
NowFunction · 0.92
setupDepsFunction · 0.85
BatcherWithStoreFunction · 0.85
BatcherWithLoggerFunction · 0.85
randStatsFunction · 0.85
LogMethod · 0.80
NewBatcherFunction · 0.70
CleanupMethod · 0.65
LogfMethod · 0.65

Tested by

no test coverage detected