Skip to content

test(storage): isolate Sinon sandboxes and fake timers in unit tests - #9919

Draft
danieljbruce wants to merge 2 commits into
googleapis:mainfrom
danieljbruce:fix/storage-bun-tests
Draft

danieljbruce wants to merge 2 commits into
googleapis:mainfrom
danieljbruce:fix/storage-bun-tests

Conversation

@danieljbruce

Copy link
Copy Markdown
Contributor

Description

Isolates Sinon sandboxes and fake timers across Storage unit tests.

Impact

Fixes Bun presubmit unit test timeouts caused by leaked global timers.

Changes

  • Isolate sinon.useFakeTimers and global.setTimeout spying in handwritten/storage/test/resumable-upload.ts within a dedicated sinon.SinonSandbox so sandbox.restore() restores the spy before uninstalling the fake clock, preventing a leaked spy on Sinon's default sandbox from overwriting global.setTimeout with a dead fake timer when sinon.restore() runs later.
  • Replace default-sandbox sinon calls (stub, spy, useFakeTimers, restore) with per-suite sandboxes in handwritten/storage/test/file.ts, handwritten/storage/test/signer.ts, handwritten/storage/test/index.ts, and handwritten/storage/test/transfer-manager.ts, and delete (global as any).resumableUpload in file.ts teardown.
  • Restrict the fs.createReadStream stub in handwritten/storage/test/bucket.ts to capture only the target textfile.txt stream so unrelated concurrent reads (such as ADC lookup) cannot overwrite readStream.

Testing

  • Added a regression test in handwritten/storage/test/resumable-upload.ts verifying that global.setTimeout remains functional after exponential back off teardown and a subsequent sinon.restore().
  • Verified the resumable-upload.js -> file.js -> util.js execution order passes under both Bun 1.4.2 and Node.js (--no-parallel).
  • Ran the full handwritten/storage unit test suite 5 times under Bun 1.4.2 and 5 times under Node.js (1,104 passing, 0 failures across all 10 runs).
  • Verified ci/run_conditional_tests.sh --strict (Node.js and Bun), node ./bin/linter.mjs --strict, pnpm --dir handwritten/storage run lint, and core/test-utils breaking-change check all pass.

Alternatives

  • Restoring setTimeoutSpy manually before clock.restore() on Sinon's default sandbox alone would leave other Storage tests exposed to cross-file state leaks whenever test execution order varies across runtimes.
  • Enforcing alphabetical file sorting in bin/run-test.cjs would only mask the test-order dependency rather than fixing the underlying fake-timer and sandbox leak.
  • Not merging this PR leaves presubmit-bun failing on pull requests that touch handwritten/storage (as seen in chore(deps): fix Renovate configuration and remove stale file #9912) whenever resumable-upload.js runs before file.js.

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors several test files to use Sinon sandboxes instead of global Sinon methods to prevent test pollution and ensure proper cleanup. While this is a great improvement, in transfer-manager.ts, a sandbox is created inline but never restored, which could lead to resource leaks in persistent test runner environments. It is recommended to declare this sandbox at the suite level and restore it in an after hook.

describe('Transfer Manager', () => {
const BUCKET_NAME = 'test-bucket';
const STORAGE = sinon.stub(
const STORAGE = sinon.createSandbox().stub(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating an isolated sandbox with sinon.createSandbox() is a great way to prevent test pollution. However, since this sandbox is never restored, it can lead to resource leaks in persistent test runner environments (such as watch mode). Consider declaring the sandbox at the suite level and restoring it in an after hook.

  const transferManagerSandbox = sinon.createSandbox();
  const STORAGE = transferManagerSandbox.stub(

@danieljbruce

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors several test files to use sandbox-specific Sinon methods instead of global Sinon methods, ensuring better test isolation and preventing global state leakage. Feedback on these changes highlights two main areas for improvement: first, in resumable-upload.ts, a custom restoreStubs() helper should be extracted and called via setImmediate to safely restore stubs across asynchronous boundaries; second, in transfer-manager.ts, defining a stubbed STORAGE instance at the module level is an anti-pattern that can cause test pollution, and it should instead be instantiated and stubbed within test hooks.

describe('Transfer Manager', () => {
const BUCKET_NAME = 'test-bucket';
const STORAGE = sinon.stub(
const STORAGE = sinon.createSandbox().stub(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Defining a stubbed STORAGE instance at the module level using sinon.createSandbox().stub(...) without ever restoring it is an anti-pattern. While it isolates the stubs from global sinon.restore() calls, sharing a single stubbed instance across all tests in the file can lead to test pollution if any test modifies the stub's behavior.

Consider instantiating and stubbing STORAGE within a before or beforeEach hook, and restoring the sandbox in after or afterEach to ensure clean test isolation.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant