(t *testing.T)
| 345 | } |
| 346 | |
| 347 | func TestStatsCollector_backlog(t *testing.T) { |
| 348 | t.Parallel() |
| 349 | |
| 350 | rollupWindow := time.Minute |
| 351 | flush := make(chan chan<- struct{}, 1) |
| 352 | |
| 353 | start := dbtime.Now().Truncate(time.Minute).UTC() |
| 354 | var now atomic.Pointer[time.Time] |
| 355 | now.Store(&start) |
| 356 | |
| 357 | reporter := &fakeReporter{} |
| 358 | collector := workspaceapps.NewStatsCollector(workspaceapps.StatsCollectorOptions{ |
| 359 | Reporter: reporter, |
| 360 | ReportInterval: time.Hour, |
| 361 | RollupWindow: rollupWindow, |
| 362 | |
| 363 | Flush: flush, |
| 364 | Now: func() time.Time { return *now.Load() }, |
| 365 | }) |
| 366 | |
| 367 | reporter.setError(xerrors.New("some error")) |
| 368 | |
| 369 | // The first collected stat is "rolled up" and moved into the |
| 370 | // backlog during the first flush. On the second flush nothing is |
| 371 | // rolled up due to being unable to report the backlog. |
| 372 | for i := 0; i < 2; i++ { |
| 373 | collector.Collect(workspaceapps.StatsReport{ |
| 374 | SessionID: uuid.New(), |
| 375 | SessionStartedAt: start, |
| 376 | SessionEndedAt: start.Add(10 * time.Second), |
| 377 | Requests: 1, |
| 378 | }) |
| 379 | start = start.Add(time.Minute) |
| 380 | now.Store(&start) |
| 381 | |
| 382 | flushDone := make(chan struct{}, 1) |
| 383 | flush <- flushDone |
| 384 | <-flushDone |
| 385 | } |
| 386 | |
| 387 | // Flush was performed 2 times, 2 reports should have failed. |
| 388 | wantErrors := 2 |
| 389 | assert.Equal(t, wantErrors, reporter.errors()) |
| 390 | assert.Empty(t, reporter.stats()) |
| 391 | |
| 392 | reporter.setError(nil) |
| 393 | |
| 394 | // Flush again, this time the backlog should be reported in addition |
| 395 | // to the second collected stat being rolled up and reported. |
| 396 | flushDone := make(chan struct{}, 1) |
| 397 | flush <- flushDone |
| 398 | <-flushDone |
| 399 | |
| 400 | assert.Equal(t, wantErrors, reporter.errors()) |
| 401 | assert.Len(t, reporter.stats(), 2) |
| 402 | } |
| 403 | |
| 404 | func TestStatsCollector_Close(t *testing.T) { |
nothing calls this directly
no test coverage detected