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

Function TestTracker

coderd/workspacestats/tracker_test.go:27–108  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestTracker(t *testing.T) {
28 t.Parallel()
29
30 ctrl := gomock.NewController(t)
31 mDB := dbmock.NewMockStore(ctrl)
32 log := testutil.Logger(t)
33
34 tickCh := make(chan time.Time)
35 flushCh := make(chan int, 1)
36 wut := workspacestats.NewTracker(mDB,
37 workspacestats.TrackerWithLogger(log),
38 workspacestats.TrackerWithTickFlush(tickCh, flushCh),
39 )
40 defer wut.Close()
41
42 // 1. No marked workspaces should imply no flush.
43 now := dbtime.Now()
44 tickCh <- now
45 count := <-flushCh
46 require.Equal(t, 0, count, "expected zero flushes")
47
48 // 2. One marked workspace should cause a flush.
49 ids := []uuid.UUID{uuid.New()}
50 now = dbtime.Now()
51 wut.Add(ids[0])
52 mDB.EXPECT().BatchUpdateWorkspaceLastUsedAt(gomock.Any(), database.BatchUpdateWorkspaceLastUsedAtParams{
53 LastUsedAt: now,
54 IDs: ids,
55 }).Times(1)
56 tickCh <- now
57 count = <-flushCh
58 require.Equal(t, 1, count, "expected one flush with one id")
59
60 // 3. Lots of marked workspaces should also cause a flush.
61 for i := 0; i < 31; i++ {
62 ids = append(ids, uuid.New())
63 }
64
65 // Sort ids so mDB know what to expect.
66 sort.Slice(ids, func(i, j int) bool {
67 return bytes.Compare(ids[i][:], ids[j][:]) < 0
68 })
69
70 now = dbtime.Now()
71 mDB.EXPECT().BatchUpdateWorkspaceLastUsedAt(gomock.Any(), database.BatchUpdateWorkspaceLastUsedAtParams{
72 LastUsedAt: now,
73 IDs: ids,
74 })
75 for _, id := range ids {
76 wut.Add(id)
77 }
78 tickCh <- now
79 count = <-flushCh
80 require.Equal(t, len(ids), count, "incorrect number of ids flushed")
81
82 // 4. Try to cause a race condition!
83 now = dbtime.Now()
84 // Difficult to know what to EXPECT here, so we won't check strictly here.

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
NewMockStoreFunction · 0.92
LoggerFunction · 0.92
NewTrackerFunction · 0.92
TrackerWithLoggerFunction · 0.92
TrackerWithTickFlushFunction · 0.92
NowFunction · 0.92
CompareMethod · 0.80
CloseMethod · 0.65
NewMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected