-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathIdentityModeTests.swift
More file actions
153 lines (117 loc) · 3.94 KB
/
Copy pathIdentityModeTests.swift
File metadata and controls
153 lines (117 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import XCTest
import JavaScriptKit
@JSFunction(from: .global) func gc() throws(JSException) -> Void
@JSClass struct IdentityModeTestImports {
@JSFunction static func runJsIdentityModeTests() throws(JSException)
}
final class IdentityModeTests: XCTestCase {
func testRunJsIdentityModeTests() throws {
try IdentityModeTestImports.runJsIdentityModeTests()
}
/// Verifies that identity-cached wrappers are properly reclaimed by GC.
///
/// Creates an identity-mode object, crosses it multiple times (filling the
/// identity cache), drops all references, triggers GC + event loop ticks,
/// and verifies the Swift object is deallocated. This proves that the
/// WeakRef-based identity cache does not prevent garbage collection.
func testIdentityCachedWrapperIsReclaimedByGC() async throws {
RetainLeakSubject.deinits = 0
// Create object and cross it multiple times to fill identity cache
_retainLeakSubject = RetainLeakSubject(tag: 99)
weak var weakSubject = _retainLeakSubject
// Cross to JS 5 times (populates identity cache with WeakRef)
for _ in 0..<5 {
_ = getRetainLeakSubject()
}
// Drop Swift-side strong reference
_retainLeakSubject = nil
// JS wrapper should still be alive via the identity cache's WeakRef,
// but WeakRef doesn't prevent GC. Trigger GC + event loop ticks to
// let FinalizationRegistry fire and call deinit.
for _ in 0..<100 {
try gc()
try await Task.sleep(for: .milliseconds(0))
if weakSubject == nil {
break
}
}
// The identity-cached wrapper should have been collected,
// FinalizationRegistry should have fired, deinit should have run.
XCTAssertNil(weakSubject, "Identity-cached object should be deallocated after GC")
XCTAssertEqual(RetainLeakSubject.deinits, 1, "Deinit should fire exactly once")
}
}
@JS class IdentityTestSubject {
@JS var value: Int
@JS init(value: Int) {
self.value = value
}
@JS var currentValue: Int { value }
}
nonisolated(unsafe) private var _sharedSubject: IdentityTestSubject?
@JS func getSharedSubject() -> IdentityTestSubject {
if _sharedSubject == nil {
_sharedSubject = IdentityTestSubject(value: 42)
}
return _sharedSubject!
}
@JS func resetSharedSubject() {
_sharedSubject = nil
}
@JS class RetainLeakSubject {
nonisolated(unsafe) static var deinits: Int = 0
@JS var tag: Int
@JS init(tag: Int) {
self.tag = tag
}
deinit {
Self.deinits += 1
}
}
nonisolated(unsafe) private var _retainLeakSubject: RetainLeakSubject?
@JS func getRetainLeakSubject() -> RetainLeakSubject {
if _retainLeakSubject == nil {
_retainLeakSubject = RetainLeakSubject(tag: 1)
}
return _retainLeakSubject!
}
@JS func resetRetainLeakSubject() {
_retainLeakSubject = nil
}
@JS func getRetainLeakDeinits() -> Int {
RetainLeakSubject.deinits
}
@JS func resetRetainLeakDeinits() {
RetainLeakSubject.deinits = 0
}
// MARK: - Array identity tests
@JS class ArrayIdentityElement {
nonisolated(unsafe) static var deinits: Int = 0
@JS var tag: Int
@JS init(tag: Int) {
self.tag = tag
}
deinit {
Self.deinits += 1
}
}
nonisolated(unsafe) private var _arrayPool: [ArrayIdentityElement] = []
@JS func setupArrayPool(_ count: Int) {
_arrayPool = (0..<count).map { ArrayIdentityElement(tag: $0) }
}
@JS func getArrayPool() -> [ArrayIdentityElement] {
return _arrayPool
}
@JS func getArrayPoolElement(_ index: Int) -> ArrayIdentityElement? {
guard index >= 0, index < _arrayPool.count else { return nil }
return _arrayPool[index]
}
@JS func getArrayPoolDeinits() -> Int {
ArrayIdentityElement.deinits
}
@JS func resetArrayPoolDeinits() {
ArrayIdentityElement.deinits = 0
}
@JS func clearArrayPool() {
_arrayPool = []
}