Skip to content

Commit 619ec92

Browse files
authored
test(coderd/database): fix DST issue in dbpurge test (#13170)
Fixes #13165
1 parent e76b595 commit 619ec92

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

coderd/database/dbpurge/dbpurge_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package dbpurge_test
22

33
import (
4+
"bufio"
5+
"bytes"
46
"context"
57
"database/sql"
8+
"encoding/json"
9+
"fmt"
610
"testing"
711
"time"
812

@@ -41,9 +45,6 @@ func TestPurge(t *testing.T) {
4145
func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
4246
t.Parallel()
4347

44-
// https://github.com/coder/coder/issues/13165
45-
t.Skip()
46-
4748
db, _ := dbtestutil.NewDB(t)
4849
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
4950

@@ -53,50 +54,59 @@ func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
5354
if t.Failed() {
5455
t.Logf("Test failed, printing rows...")
5556
ctx := testutil.Context(t, testutil.WaitShort)
57+
buf := &bytes.Buffer{}
58+
enc := json.NewEncoder(buf)
59+
enc.SetIndent("", "\t")
5660
wasRows, err := db.GetWorkspaceAgentStats(ctx, now.AddDate(0, -7, 0))
5761
if err == nil {
58-
for _, row := range wasRows {
59-
t.Logf("workspace agent stat: %v", row)
60-
}
62+
_, _ = fmt.Fprintf(buf, "workspace agent stats: ")
63+
_ = enc.Encode(wasRows)
6164
}
6265
tusRows, err := db.GetTemplateUsageStats(context.Background(), database.GetTemplateUsageStatsParams{
6366
StartTime: now.AddDate(0, -7, 0),
6467
EndTime: now,
6568
})
6669
if err == nil {
67-
for _, row := range tusRows {
68-
t.Logf("template usage stat: %v", row)
69-
}
70+
_, _ = fmt.Fprintf(buf, "template usage stats: ")
71+
_ = enc.Encode(tusRows)
72+
}
73+
s := bufio.NewScanner(buf)
74+
for s.Scan() {
75+
t.Log(s.Text())
7076
}
77+
_ = s.Err()
7178
}
7279
}()
7380

7481
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
7582
defer cancel()
7683

7784
// given
85+
// Note: We use increments of 2 hours to ensure we avoid any DST
86+
// conflicts, verifying DST behavior is beyond the scope of this
87+
// test.
7888
// Let's use RxBytes to identify stat entries.
79-
// Stat inserted 6 months + 1 hour ago, should be deleted.
89+
// Stat inserted 6 months + 2 hour ago, should be deleted.
8090
first := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
81-
CreatedAt: now.AddDate(0, -6, 0).Add(-time.Hour),
91+
CreatedAt: now.AddDate(0, -6, 0).Add(-2 * time.Hour),
8292
ConnectionCount: 1,
8393
ConnectionMedianLatencyMS: 1,
8494
RxBytes: 1111,
8595
SessionCountSSH: 1,
8696
})
8797

88-
// Stat inserted 6 months - 1 hour ago, should not be deleted before rollup.
98+
// Stat inserted 6 months - 2 hour ago, should not be deleted before rollup.
8999
second := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
90-
CreatedAt: now.AddDate(0, -6, 0).Add(time.Hour),
100+
CreatedAt: now.AddDate(0, -6, 0).Add(2 * time.Hour),
91101
ConnectionCount: 1,
92102
ConnectionMedianLatencyMS: 1,
93103
RxBytes: 2222,
94104
SessionCountSSH: 1,
95105
})
96106

97-
// Stat inserted 6 months - 1 day - 2 hour ago, should not be deleted at all.
107+
// Stat inserted 6 months - 1 day - 4 hour ago, should not be deleted at all.
98108
third := dbgen.WorkspaceAgentStat(t, db, database.WorkspaceAgentStat{
99-
CreatedAt: now.AddDate(0, -6, 0).AddDate(0, 0, 1).Add(2 * time.Hour),
109+
CreatedAt: now.AddDate(0, -6, 0).AddDate(0, 0, 1).Add(4 * time.Hour),
100110
ConnectionCount: 1,
101111
ConnectionMedianLatencyMS: 1,
102112
RxBytes: 3333,

0 commit comments

Comments
 (0)