forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCFinalizerTest.cpp
More file actions
131 lines (103 loc) · 3.53 KB
/
GCFinalizerTest.cpp
File metadata and controls
131 lines (103 loc) · 3.53 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include "hermes/VM/AllocResult.h"
#include "hermes/VM/DummyObject.h"
#include "hermes/VM/GC.h"
#include "hermes/VM/GCPointer-inline.h"
#include "hermes/VM/Handle.h"
#include "hermes/VM/HermesValueTraits.h"
#include <vector>
using namespace hermes::vm;
/// Testing finalizer support in whichever GC implementation the tests are
/// configured with.
namespace {
using testhelpers::DummyObject;
static DummyObject *createWithFinalizeCount(GC *gc, int *numFinalized) {
auto *obj = DummyObject::create(gc);
obj->finalizerCallback = std::make_unique<DummyObject::Callback>(
[numFinalized]() mutable { (*numFinalized)++; });
return obj;
}
TEST(GCFinalizerTest, NoDeadFinalizables) {
int finalized = 0;
auto runtime = DummyRuntime::create(kTestGCConfigSmall);
DummyRuntime &rt = *runtime;
GCScope scope{&rt};
DummyObject::create(&rt.getHeap());
rt.makeHandle(createWithFinalizeCount(&rt.getHeap(), &finalized));
rt.collect();
ASSERT_EQ(0, finalized);
}
TEST(GCFinalizerTest, FinalizablesOnly) {
int finalized = 0;
auto runtime = DummyRuntime::create(kTestGCConfigSmall);
DummyRuntime &rt = *runtime;
GCScope scope{&rt};
createWithFinalizeCount(&rt.getHeap(), &finalized);
rt.makeHandle(createWithFinalizeCount(&rt.getHeap(), &finalized));
rt.collect();
ASSERT_EQ(1, finalized);
}
TEST(GCFinalizerTest, MultipleCollect) {
int finalized = 0;
auto runtime = DummyRuntime::create(kTestGCConfigSmall);
DummyRuntime &rt = *runtime;
{
GCScope scope{&rt};
createWithFinalizeCount(&rt.getHeap(), &finalized);
DummyObject::create(&rt.getHeap());
createWithFinalizeCount(&rt.getHeap(), &finalized);
rt.makeHandle(createWithFinalizeCount(&rt.getHeap(), &finalized));
rt.makeHandle(DummyObject::create(&rt.getHeap()));
rt.collect();
ASSERT_EQ(2, finalized);
}
rt.collect();
ASSERT_EQ(3, finalized);
}
TEST(GCFinalizerTest, FinalizeAllOnRuntimeDestructDummyRuntime) {
int finalized = 0;
{
auto rt = DummyRuntime::create(kTestGCConfigSmall);
GCScope scope{rt.get()};
rt->makeHandle(createWithFinalizeCount(&rt->getHeap(), &finalized));
rt->makeHandle(createWithFinalizeCount(&rt->getHeap(), &finalized));
// Collect once to get the objects into the old gen, then a second time
// to get their mark bits set in their stable locations.
rt->collect();
rt->collect();
ASSERT_EQ(0, finalized);
// Now: does destructing the runtime with set mark bits run all the
// finalizers?
}
ASSERT_EQ(2, finalized);
}
TEST(GCFinalizerTest, FinalizeAllOnRuntimeDestructRealRuntime) {
int finalized = 0;
std::shared_ptr<Runtime> rt{Runtime::create(kTestRTConfig)};
{
GCScope gcScope(rt.get());
auto r1 = rt->makeHandle(HermesValue::encodeObjectValue(
createWithFinalizeCount(&rt->getHeap(), &finalized)));
auto r2 = rt->makeHandle(HermesValue::encodeObjectValue(
createWithFinalizeCount(&rt->getHeap(), &finalized)));
// Collect once to get the objects into the old gen, then a second time
// to get their mark bits set in their stable locations.
rt->collect("test");
rt->collect("test");
ASSERT_EQ(0, finalized);
(void)r1;
(void)r2;
}
// Now: does destructing the runtime with set mark bits run all the
// finalizers?
rt.reset();
ASSERT_EQ(2, finalized);
}
} // namespace