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

Function TestStaleSessions

provisionersdk/cleanup_test.go:22–112  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20var now = time.Date(2023, time.June, 3, 4, 5, 6, 0, time.UTC)
21
22func TestStaleSessions(t *testing.T) {
23 t.Parallel()
24
25 prepare := func() (afero.Fs, slog.Logger) {
26 tempDir := t.TempDir()
27 fs := afero.NewBasePathFs(afero.NewOsFs(), tempDir)
28 logger := testutil.Logger(t).
29 Leveled(slog.LevelDebug).
30 Named("cleanup-test")
31 return fs, logger
32 }
33
34 t.Run("all sessions are stale", func(t *testing.T) {
35 t.Parallel()
36
37 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
38 defer cancel()
39
40 fs, logger := prepare()
41
42 // given
43 first := tfpath.Session(workDirectory, uuid.NewString())
44 addSessionFolder(t, fs, first, now.Add(-7*24*time.Hour))
45 second := tfpath.Session(workDirectory, uuid.NewString())
46 addSessionFolder(t, fs, second, now.Add(-8*24*time.Hour))
47 third := tfpath.Session(workDirectory, uuid.NewString())
48 addSessionFolder(t, fs, third, now.Add(-9*24*time.Hour))
49 // tfDir is a fake session that will clean up the others
50 tfDir := tfpath.Session(workDirectory, uuid.NewString())
51
52 // when
53 err := tfDir.CleanStaleSessions(ctx, logger, fs, now)
54 require.NoError(t, err)
55
56 // then
57 entries, err := afero.ReadDir(fs, workDirectory)
58 require.NoError(t, err)
59 require.Empty(t, entries, "all session leftovers should be removed")
60 })
61
62 t.Run("one session is stale", func(t *testing.T) {
63 t.Parallel()
64
65 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
66 defer cancel()
67
68 fs, logger := prepare()
69
70 // given
71 first := tfpath.Session(workDirectory, uuid.NewString())
72 addSessionFolder(t, fs, first, now.Add(-7*24*time.Hour))
73 second := tfpath.Session(workDirectory, uuid.NewString())
74 addSessionFolder(t, fs, second, now.Add(-6*24*time.Hour))
75 tfDir := tfpath.Session(workDirectory, uuid.NewString())
76
77 // when
78 err := tfDir.CleanStaleSessions(ctx, logger, fs, now)
79 require.NoError(t, err)

Callers

nothing calls this directly

Calls 14

LoggerFunction · 0.92
SessionFunction · 0.92
addSessionFolderFunction · 0.85
NamedMethod · 0.80
CleanStaleSessionsMethod · 0.80
ReadDirMethod · 0.80
WorkDirectoryMethod · 0.80
TempDirMethod · 0.65
RunMethod · 0.65
AddMethod · 0.65
NameMethod · 0.65
EmptyMethod · 0.45

Tested by

no test coverage detected