Skip to content

Commit 7378384

Browse files
alexmarkovCommit Queue
authored andcommitted
[gardening] Fix corelib/weak_reference_test
This test incorrectly expects that WeakReference.target should be cleared for a weakly reachable object. WeakReference specification doesn't guarantee that: https://github.com/dart-lang/sdk/blob/a9f684e62499e669294a07c3ce7d2aa7c545760a/sdk/lib/core/weak.dart#L75-L76 This change fixes the test to give up after certain number of iterations instead of hanging forever. Issue: #55518 Change-Id: Ief0ebe1452c83058a35fc1ba87e5a83924d43919 TEST=corelib/weak_reference_test Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363960 Reviewed-by: Siva Annamalai <[email protected]> Commit-Queue: Siva Annamalai <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Auto-Submit: Alexander Markov <[email protected]>
1 parent 33efdcd commit 7378384

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

tests/corelib/weak_reference_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ Future testWeakReferenceWeakness() async {
4141
foo = null;
4242
}
4343
asyncStart();
44-
while (weakReference.target != null) {
44+
// According to the WeakReference specification:
45+
//
46+
// There are no guarantees that a weak reference will ever be cleared
47+
// even if all references to its target are weak references.
48+
//
49+
// Wait a few iterations and give up if target is not cleared.
50+
const int numIterations = 10;
51+
int i = 0;
52+
for (; weakReference.target != null && i < numIterations; ++i) {
4553
produceGarbage();
4654
await Future.delayed(const Duration(milliseconds: 10));
4755
}
48-
Expect.isNull(weakReference.target);
56+
Expect.isTrue(i == numIterations || weakReference.target == null);
4957
asyncEnd();
5058
}

0 commit comments

Comments
 (0)