Skip to content

test: Add GC lifecycle test for identity-cached wrappers#7

Merged
krodak merged 1 commit intomainfrom
feat/identity-gc-lifecycle-test-v2
Apr 27, 2026
Merged

test: Add GC lifecycle test for identity-cached wrappers#7
krodak merged 1 commit intomainfrom
feat/identity-gc-lifecycle-test-v2

Conversation

@krodak
Copy link
Copy Markdown
Collaborator

@krodak krodak commented Apr 27, 2026

Overview

Add a test proving that the WeakRef-based identity cache does not prevent garbage collection of Swift heap objects.

The existing GC lifecycle test (testJSWrapperIsDeallocatedAfterFinalization) only covers non-identity classes. This adds an equivalent for @JS(identityMode: true) classes where the identity cache holds a WeakRef to the wrapper.

What it tests

Creates a RetainLeakSubject (identity-mode class), crosses it to JS 5 times (filling the identity cache with a WeakRef entry), drops all Swift-side references, triggers GC + event loop ticks, and verifies:

  • The Swift object is deallocated (weakSubject == nil)
  • deinit fires exactly once

This covers the scenario where only TypeScript holds a reference to an identity-cached Swift object, then that reference becomes unreachable.

What changed

  • IdentityModeTests.swift — Added testIdentityCachedWrapperIsReclaimedByGC async test. Added @JSFunction gc() import (same pattern as SwiftClassSupportTests).
  • Generated/BridgeJS.swift + Generated/JavaScript/BridgeJS.json — Regenerated to include gc import binding.

Verify that WeakRef-based identity cache does not prevent garbage collection.
Creates an identity-mode object, crosses it 5 times (filling identity cache),
drops all references, triggers GC + event loop ticks, and asserts the Swift
object is deallocated and deinit fires exactly once.
@krodak krodak merged commit cf928b8 into main Apr 27, 2026
13 checks passed
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.

1 participant