Index: runtime/vm/dart_api_impl_test.cc |
diff --git a/runtime/vm/dart_api_impl_test.cc b/runtime/vm/dart_api_impl_test.cc |
index fcc4e79cd8cc88cc39f412f13ed1490c827bd007..2ab0656d87bdd4e945aa0c0591eb3f8dbac725df 100644 |
--- a/runtime/vm/dart_api_impl_test.cc |
+++ b/runtime/vm/dart_api_impl_test.cc |
@@ -1206,6 +1206,28 @@ TEST_CASE(MalformedStringToUTF8) { |
} |
+// Helper class to ensure new gen GC is triggered without any side effects. |
+// The normal call to CollectGarbage(Heap::kNew) could potentially trigger |
+// an old gen collection if there is a promotion failure and this could |
+// perturb the test. |
+class GCTestHelper : public AllStatic { |
+ public: |
+ static void CollectNewSpace(Heap::ApiCallbacks api_callbacks) { |
+ bool invoke_api_callbacks = (api_callbacks == Heap::kInvokeApiCallbacks); |
+ Isolate::Current()->heap()->new_space()->Scavenge(invoke_api_callbacks); |
+ } |
+ |
+ static void WaitForFinalizationTasks() { |
+ Thread* thread = Thread::Current(); |
+ Heap* heap = thread->isolate()->heap(); |
+ MonitorLocker ml(heap->finalization_tasks_lock()); |
+ while (heap->finalization_tasks() > 0) { |
+ ml.WaitWithSafepointCheck(thread); |
+ } |
+ } |
+}; |
+ |
+ |
static void ExternalStringCallbackFinalizer(void* peer) { |
*static_cast<int*>(peer) *= 2; |
} |
@@ -1242,9 +1264,11 @@ TEST_CASE(ExternalStringCallback) { |
EXPECT_EQ(40, peer8); |
EXPECT_EQ(41, peer16); |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT_EQ(40, peer8); |
EXPECT_EQ(41, peer16); |
Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT_EQ(80, peer8); |
EXPECT_EQ(82, peer16); |
} |
@@ -2381,8 +2405,10 @@ TEST_CASE(ExternalTypedDataCallback) { |
TransitionNativeToVM transition(thread); |
EXPECT(peer == 0); |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(peer == 0); |
Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(peer == 42); |
} |
} |
@@ -2439,6 +2465,7 @@ TEST_CASE(Float32x4List) { |
{ |
TransitionNativeToVM transition(thread); |
Isolate::Current()->heap()->CollectGarbage(Heap::kNew); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(peer == 42); |
} |
} |
@@ -2601,19 +2628,6 @@ UNIT_TEST_CASE(AssignToPersistentHandle) { |
} |
-// Helper class to ensure new gen GC is triggered without any side effects. |
-// The normal call to CollectGarbage(Heap::kNew) could potentially trigger |
-// an old gen collection if there is a promotion failure and this could |
-// perturb the test. |
-class GCTestHelper : public AllStatic { |
- public: |
- static void CollectNewSpace(Heap::ApiCallbacks api_callbacks) { |
- bool invoke_api_callbacks = (api_callbacks == Heap::kInvokeApiCallbacks); |
- Isolate::Current()->heap()->new_space()->Scavenge(invoke_api_callbacks); |
- } |
-}; |
- |
- |
static Dart_Handle AsHandle(Dart_PersistentHandle weak) { |
return Dart_HandleFromPersistent(weak); |
} |
@@ -2728,6 +2742,7 @@ TEST_CASE(WeakPersistentHandle) { |
TransitionNativeToVM transition(thread); |
// Garbage collect new space again. |
GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
+ GCTestHelper::WaitForFinalizationTasks(); |
} |
{ |
@@ -2743,6 +2758,7 @@ TEST_CASE(WeakPersistentHandle) { |
TransitionNativeToVM transition(thread); |
// Garbage collect old space again. |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
+ GCTestHelper::WaitForFinalizationTasks(); |
} |
{ |
@@ -2787,6 +2803,7 @@ TEST_CASE(WeakPersistentHandleCallback) { |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
EXPECT(peer == 0); |
GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(peer == 42); |
} |
} |
@@ -2813,6 +2830,7 @@ TEST_CASE(WeakPersistentHandleNoCallback) { |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
EXPECT(peer == 0); |
GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(peer == 0); |
} |
} |
@@ -2873,6 +2891,7 @@ TEST_CASE(WeakPersistentHandleExternalAllocationSize) { |
// Collect weakly referenced string, and promote strongly referenced string. |
GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
GCTestHelper::CollectNewSpace(Heap::kIgnoreApiCallbacks); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(heap->ExternalInWords(Heap::kNew) == 0); |
EXPECT(heap->ExternalInWords(Heap::kOld) == kWeak2ExternalSize / kWordSize); |
} |
@@ -2883,6 +2902,7 @@ TEST_CASE(WeakPersistentHandleExternalAllocationSize) { |
{ |
TransitionNativeToVM transition(thread); |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(heap->ExternalInWords(Heap::kOld) == 0); |
} |
} |
@@ -2928,6 +2948,7 @@ TEST_CASE(WeakPersistentHandleExternalAllocationSizeNewspaceGC) { |
{ |
TransitionNativeToVM transition(thread); |
Isolate::Current()->heap()->CollectGarbage(Heap::kOld); |
+ GCTestHelper::WaitForFinalizationTasks(); |
EXPECT(heap->ExternalInWords(Heap::kOld) == 0); |
} |
} |