Skip to content

Commit 52d5e6e

Browse files
author
Sergey G. Grekhov
committed
#1260. WeakReference tests added
1 parent af9fadc commit 52d5e6e

8 files changed

+327
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
main() async {
26+
C? c = C(42);
27+
WeakReference<C> wr = WeakReference(c);
28+
Expect.equals(c, wr.target);
29+
triggerGc();
30+
await Future.delayed(Duration(milliseconds: 1));
31+
Expect.equals(c, wr.target);
32+
c = null;
33+
triggerGc();
34+
await Future.delayed(Duration(milliseconds: 1));
35+
Expect.isNull(wr.target);
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
main() async {
26+
List<C> refs = List.filled(5, C(42), growable: true);
27+
28+
WeakReference<C> wr = WeakReference(refs[0]);
29+
Expect.equals(refs[0], wr.target);
30+
triggerGc();
31+
await Future.delayed(Duration(milliseconds: 1));
32+
Expect.equals(refs[1], wr.target);
33+
refs.clear();
34+
triggerGc();
35+
await Future.delayed(Duration(milliseconds: 1));
36+
Expect.isNull(wr.target);
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
C? c = C(42);
26+
27+
main() async {
28+
WeakReference<C> wr = WeakReference(c!);
29+
Expect.equals(c, wr.target);
30+
triggerGc();
31+
await Future.delayed(Duration(milliseconds: 1));
32+
Expect.equals(c, wr.target);
33+
c = null;
34+
triggerGc();
35+
await Future.delayed(Duration(milliseconds: 1));
36+
Expect.isNull(wr.target);
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
main() async {
26+
C? c = C(42);
27+
dynamic d = c;
28+
WeakReference<C> wr = WeakReference(c);
29+
Expect.equals(c, wr.target);
30+
c = null;
31+
triggerGc();
32+
await Future.delayed(Duration(milliseconds: 1));
33+
Expect.isNotNull(wr.target);
34+
Expect.equals(d, wr.target);
35+
d = 42;
36+
triggerGc();
37+
await Future.delayed(Duration(milliseconds: 1));
38+
Expect.isNull(wr.target);
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "dart:async";
18+
import "gc_utils_lib.dart";
19+
import "../../Utils/expect.dart";
20+
21+
class C {
22+
int id;
23+
C(this.id);
24+
}
25+
26+
main() async {
27+
C? c1 = C(42);
28+
WeakReference<C> wr = WeakReference(c1);
29+
asyncStart();
30+
Future<C?>.delayed(Duration(milliseconds: 1), () => c1).then((C? c2) async {
31+
Expect.isNotNull(wr.target);
32+
Expect.equals(c1, wr.target);
33+
triggerGc();
34+
await Future.delayed(Duration(milliseconds: 5));
35+
Expect.isNotNull(wr.target);
36+
c2 = null;
37+
triggerGc();
38+
Expect.isNull(wr.target);
39+
asyncEnd();
40+
});
41+
Expect.isNotNull(wr.target);
42+
Expect.equals(c1, wr.target);
43+
await Future.delayed(Duration(milliseconds: 2));
44+
c1 = null;
45+
triggerGc();
46+
await Future.delayed(Duration(milliseconds: 1));
47+
Expect.isNotNull(wr.target);
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "dart:async";
18+
import "gc_utils_lib.dart";
19+
import "../../Utils/expect.dart";
20+
21+
class C {
22+
int id;
23+
C(this.id);
24+
}
25+
26+
main() async {
27+
C? c1 = C(42);
28+
WeakReference<C> wr = WeakReference(c1);
29+
asyncStart();
30+
Future<C?>.delayed(Duration(milliseconds: 1), () => c1).then((C? c2) async {
31+
Expect.isNotNull(wr.target);
32+
Expect.equals(c1, wr.target);
33+
triggerGc();
34+
await Future.delayed(Duration(milliseconds: 1));
35+
Expect.isNotNull(wr.target);
36+
c2 = null;
37+
triggerGc();
38+
await Future.delayed(Duration(milliseconds: 1));
39+
Expect.isNotNull(wr.target);
40+
asyncEnd();
41+
});
42+
Expect.isNotNull(wr.target);
43+
Expect.equals(c1, wr.target);
44+
await Future.delayed(Duration(milliseconds: 10));
45+
c1 = null;
46+
triggerGc();
47+
Expect.isNull(wr.target);
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
main() async {
26+
C? c1 = C(42);
27+
C? c2 = c1;
28+
WeakReference<C> wr = WeakReference(c1);
29+
Expect.equals(c1, wr.target);
30+
triggerGc();
31+
await Future.delayed(Duration(milliseconds: 1));
32+
Expect.equals(c1, wr.target);
33+
c1 = null;
34+
triggerGc();
35+
await Future.delayed(Duration(milliseconds: 1));
36+
Expect.isNotNull(wr.target);
37+
Expect.equals(c2, wr.target);
38+
c2 = null;
39+
triggerGc();
40+
await Future.delayed(Duration(milliseconds: 1));
41+
Expect.isNull(wr.target);
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion target property
6+
/// T? target
7+
/// The current object weakly referenced by this, if any.
8+
///
9+
/// The value is either the object supplied in the constructor, or null if the
10+
/// weak reference has been cleared.
11+
///
12+
/// @description Check that the value of this property value is either the
13+
/// object supplied in the constructor, or null if the weak reference has been
14+
/// cleared
15+
/// @author [email protected]
16+
17+
import "gc_utils_lib.dart";
18+
import "../../Utils/expect.dart";
19+
20+
class C {
21+
int id;
22+
C(this.id);
23+
}
24+
25+
main() async {
26+
C? c = C(42);
27+
WeakReference<C> wr1 = WeakReference(c);
28+
WeakReference<C> wr2 = WeakReference(c);
29+
Expect.equals(c, wr1.target);
30+
Expect.equals(c, wr2.target);
31+
triggerGc();
32+
await Future.delayed(Duration(milliseconds: 1));
33+
Expect.equals(c, wr1.target);
34+
Expect.equals(c, wr2.target);
35+
c = null;
36+
triggerGc();
37+
await Future.delayed(Duration(milliseconds: 1));
38+
Expect.isNull(wr1.target);
39+
Expect.isNull(wr2.target);
40+
}

0 commit comments

Comments
 (0)