[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:

a9f684e624/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: https://github.com/dart-lang/sdk/issues/55518
Change-Id: Ief0ebe1452c83058a35fc1ba87e5a83924d43919
TEST=corelib/weak_reference_test
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363960
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Auto-Submit: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2024-04-22 18:00:19 +00:00 committed by Commit Queue
parent 33efdcd76a
commit 73783847fd

View file

@ -41,10 +41,18 @@ Future testWeakReferenceWeakness() async {
foo = null;
}
asyncStart();
while (weakReference.target != null) {
// According to the WeakReference specification:
//
// There are no guarantees that a weak reference will ever be cleared
// even if all references to its target are weak references.
//
// Wait a few iterations and give up if target is not cleared.
const int numIterations = 10;
int i = 0;
for (; weakReference.target != null && i < numIterations; ++i) {
produceGarbage();
await Future.delayed(const Duration(milliseconds: 10));
}
Expect.isNull(weakReference.target);
Expect.isTrue(i == numIterations || weakReference.target == null);
asyncEnd();
}