Skip to content

Commit 1fee5b9

Browse files
committed
fix batcher maybe
1 parent 827de08 commit 1fee5b9

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
@@ -199,15 +199,6 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
199199
defer func() {
200200
b.flushForced.Store(false)
201201
b.mu.Unlock()
202-
// Notify that a flush has completed. This only happens in tests.
203-
if b.flushed != nil {
204-
select {
205-
case <-ctx.Done():
206-
close(b.flushed)
207-
default:
208-
b.flushed <- count
209-
}
210-
}
211202
if count > 0 {
212203
elapsed := time.Since(start)
213204
b.log.Debug(ctx, "flush complete",
@@ -217,6 +208,15 @@ func (b *Batcher) flush(ctx context.Context, forced bool, reason string) {
217208
slog.F("reason", reason),
218209
)
219210
}
211+
// Notify that a flush has completed. This only happens in tests.
212+
if b.flushed != nil {
213+
select {
214+
case <-ctx.Done():
215+
close(b.flushed)
216+
default:
217+
b.flushed <- count
218+
}
219+
}
220220
}()
221221

222222
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)