Skip to content

E2E test: Consumer::close() with autoCommit stores final offset#306

Merged
s2x merged 1 commit intomainfrom
oda-168-e2e-test-consumer-close-with-autocommit
Mar 29, 2026
Merged

E2E test: Consumer::close() with autoCommit stores final offset#306
s2x merged 1 commit intomainfrom
oda-168-e2e-test-consumer-close-with-autocommit

Conversation

@s2x
Copy link
Copy Markdown
Contributor

@s2x s2x commented Mar 29, 2026

Closes #168

Problem

Consumer::close() has logic to auto-commit the last offset when autoCommit > 0 and name !== null:

if ($this->autoCommit > 0 && $this->name !== null && $this->messagesProcessed > 0) {
    $this->storeOffset($this->lastOffset);
}

No E2E test verifies this behavior. The consumer_auto_commit.php example uses it, but there's no automated test.

Expected test

public function testAutoCommitOnClose(): void
{
    $consumerName = 'auto-commit-test-' . uniqid();

    // Publish 5 messages
    $producer->sendBatch([...]);
    $producer->waitForConfirms();

    // Create consumer with autoCommit
    $consumer = $connection->createConsumer(
        $stream, OffsetSpec::first(),
        name: $consumerName,
        autoCommit: 3
    );

    // Read all messages
    $messages = [];
    while (count($messages) < 5) {
        $messages = array_merge($messages, $consumer->read(timeout: 1));
    }

    // Close should store the last offset
    $consumer->close();

    // Query offset — should be the last message's offset
    $storedOffset = $connection->queryOffset($consumerName, $stream);
    $this->assertSame($messages[4]->getOffset(), $storedOffset);
}

Why it matters

  • Auto-commit on close prevents message loss on graceful shutdown
  • The messagesProcessed counter and lastOffset tracking must be correct
  • Without this test, a regression could cause offset loss on consumer restart

Acceptance criteria

  • Consumer with autoCommit stores offset on close()
  • Stored offset matches the last consumed message's offset
  • Consumer without autoCommit does NOT store offset on close()
  • Consumer with no messages processed does NOT store offset on close()

…age (#168)

Add three test cases to verify autoCommit behavior on consumer close:
- testAutoCommitOnCloseStoresLastOffset: verifies offset is stored when autoCommit > 0
- testNoAutoCommitOnCloseDoesNotStoreOffset: verifies no offset stored when autoCommit = 0
- testAutoCommitOnCloseWithNoMessagesDoesNotStoreOffset: verifies no offset stored when no messages consumed
@s2x s2x merged commit a6da9c2 into main Mar 29, 2026
7 checks passed
@s2x s2x deleted the oda-168-e2e-test-consumer-close-with-autocommit branch March 29, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E2E test: Consumer::close() with autoCommit stores final offset

1 participant