TestBatchSizeDecay verifies that the batch size decays when no messages are available. (see https://github.com/google/go-cloud/issues/2849).
(t *testing.T)
| 385 | // TestBatchSizeDecay verifies that the batch size decays when no messages are available. |
| 386 | // (see https://github.com/google/go-cloud/issues/2849). |
| 387 | func TestBatchSizeDecays(t *testing.T) { |
| 388 | ctx := context.Background() |
| 389 | fs := &failSub{} |
| 390 | // Allow multiple handlers and cap max batch size to ensure we get concurrency. |
| 391 | sub := NewSubscription(fs, &batcher.Options{MaxHandlers: 10, MaxBatchSize: 2}, nil) |
| 392 | defer sub.Shutdown(ctx) |
| 393 | |
| 394 | // Records the last batch size. |
| 395 | var mu sync.Mutex |
| 396 | lastMaxMessages := 0 |
| 397 | sub.preReceiveBatchHook = func(maxMessages int) { |
| 398 | mu.Lock() |
| 399 | defer mu.Unlock() |
| 400 | lastMaxMessages = maxMessages |
| 401 | } |
| 402 | |
| 403 | // Do some receives to allow the number of batches to increase past 1. |
| 404 | for range 100 { |
| 405 | m, err := sub.Receive(ctx) |
| 406 | if err != nil { |
| 407 | t.Fatalf("Receive: got %v, want nil", err) |
| 408 | } |
| 409 | m.Ack() |
| 410 | } |
| 411 | |
| 412 | // Tell the failSub to start returning no messages. |
| 413 | fs.mu.Lock() |
| 414 | fs.empty = true |
| 415 | fs.mu.Unlock() |
| 416 | |
| 417 | mu.Lock() |
| 418 | highWaterMarkBatchSize := lastMaxMessages |
| 419 | if lastMaxMessages <= 1 { |
| 420 | t.Fatal("max messages wasn't greater than 1") |
| 421 | } |
| 422 | mu.Unlock() |
| 423 | |
| 424 | // Make a bunch of calls to Receive to drain any outstanding |
| 425 | // messages, and wait some extra time during which we should |
| 426 | // continue polling, and the batch size should decay. |
| 427 | for { |
| 428 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 429 | defer cancel() |
| 430 | m, err := sub.Receive(ctx) |
| 431 | if err != nil { |
| 432 | // Expected: no more messages, and timed out. |
| 433 | break |
| 434 | } |
| 435 | // Drained a message. |
| 436 | m.Ack() |
| 437 | } |
| 438 | |
| 439 | // Verify that the batch size decayed. |
| 440 | mu.Lock() |
| 441 | if lastMaxMessages >= highWaterMarkBatchSize { |
| 442 | t.Fatalf("wanted batch size to decay; high water mark was %d, now %d", highWaterMarkBatchSize, lastMaxMessages) |
| 443 | } |
| 444 | mu.Unlock() |
nothing calls this directly
no test coverage detected