Skip to content

Commit 00c2b66

Browse files
committed
fix batcher maybe
1 parent 58cc6ee commit 00c2b66

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

coderd/batchstats/batcher.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,6 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
198198
defer func() {
199199
b.flushForced.Store(false)
200200
b.mu.Unlock()
201-
// Notify that a flush has completed. This only happens in tests.
202-
if b.flushed != nil {
203-
select {
204-
case <-ctx.Done():
205-
close(b.flushed)
206-
default:
207-
b.flushed <- count
208-
}
209-
}
210201
if count > 0 {
211202
elapsed := time.Since(start)
212203
b.log.Debug(ctx, "flush complete",
@@ -216,6 +207,15 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
216207
slog.F("reason", reason),
217208
)
218209
}
210+
// Notify that a flush has completed. This only happens in tests.
211+
if b.flushed != nil {
212+
select {
213+
case <-ctx.Done():
214+
close(b.flushed)
215+
default:
216+
b.flushed <- count
217+
}
218+
}
219219
}()
220220

221221
if len(b.buf.ID) == 0 {

coderd/batchstats/batcher_internal_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestBatchStats(t *testing.T) {
4646

4747
// Given: no data points are added for workspace
4848
// When: it becomes time to report stats
49-
t1 := time.Now()
49+
t1 := database.Now()
5050
// Signal a tick and wait for a flush to complete.
5151
tick <- t1
5252
f := <-flushed
@@ -59,7 +59,7 @@ func TestBatchStats(t *testing.T) {
5959
require.Empty(t, stats, "should have no stats for workspace")
6060

6161
// Given: a single data point is added for workspace
62-
t2 := time.Now()
62+
t2 := database.Now()
6363
t.Logf("inserting 1 stat")
6464
require.NoError(t, b.Add(deps1.Agent.ID, deps1.User.ID, deps1.Template.ID, deps1.Workspace.ID, randAgentSDKStats(t)))
6565

@@ -77,7 +77,7 @@ func TestBatchStats(t *testing.T) {
7777

7878
// Given: a lot of data points are added for both workspaces
7979
// (equal to batch size)
80-
t3 := time.Now()
80+
t3 := database.Now()
8181
done := make(chan struct{})
8282

8383
go func() {
@@ -105,15 +105,15 @@ func TestBatchStats(t *testing.T) {
105105
require.Len(t, stats, 2, "should have stats for both workspaces")
106106

107107
// Ensures that a subsequent flush pushes all the remaining data
108-
t4 := time.Now()
108+
t4 := database.Now()
109109
tick <- t4
110110
f2 := <-flushed
111111
t.Logf("flush 4 completed")
112112
expectedCount := defaultBufferSize - f
113113
require.Equal(t, expectedCount, f2, "did not flush expected remaining rows")
114114

115115
// Ensure that a subsequent flush does not push stale data.
116-
t5 := time.Now()
116+
t5 := database.Now()
117117
tick <- t5
118118
f = <-flushed
119119
require.Zero(t, f, "expected zero stats to have been flushed")

0 commit comments

Comments
 (0)