Skip to content

Commit

Permalink
Add tests that launch isolates in a relaunched VM.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde committed Oct 9, 2018
1 parent 1f55807 commit 2add29d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ executable("runtime_unittests") {
":runtime",
":runtime_fixtures",
"$flutter_root/fml",
"$flutter_root/shell/common",
"$flutter_root/lib/snapshot",
"$flutter_root/testing",
"//third_party/dart/runtime:libdart_jit",
Expand Down
15 changes: 5 additions & 10 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include "flutter/testing/testing.h"
#include "flutter/testing/thread_test.h"

#define CURRENT_TEST_NAME \
std::string { \
::testing::UnitTest::GetInstance()->current_test_info()->name() \
}

namespace blink {

using DartIsolateTest = ::testing::ThreadTest;
Expand All @@ -23,11 +18,11 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
settings.task_observer_remove = [](intptr_t) {};
auto vm = DartVM::ForProcess(settings);
ASSERT_TRUE(vm);
TaskRunners task_runners(CURRENT_TEST_NAME, //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner() //
TaskRunners task_runners(testing::GetCurrentTestName(), //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner(), //
GetCurrentTaskRunner() //
);
auto weak_isolate = DartIsolate::CreateRootIsolate(
vm.get(), // vm
Expand Down
8 changes: 6 additions & 2 deletions runtime/dart_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,6 @@ DartVM::DartVM(const Settings& settings,

DartUI::InitForGlobal();

Dart_SetFileModifiedCallback(&DartFileModifiedCallback);

{
TRACE_EVENT0("flutter", "Dart_Initialize");
Dart_InitializeParams params = {};
Expand Down Expand Up @@ -438,6 +436,8 @@ DartVM::DartVM(const Settings& settings,
}
}

Dart_SetFileModifiedCallback(&DartFileModifiedCallback);

// Allow streaming of stdout and stderr by the Dart vm.
Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback,
&ServiceStreamCancelCallback);
Expand All @@ -449,12 +449,16 @@ DartVM::~DartVM() {
if (Dart_CurrentIsolate() != nullptr) {
Dart_ExitIsolate();
}

char* result = Dart_Cleanup();

if (result != nullptr) {
FML_LOG(ERROR) << "Could not cleanly shut down the Dart VM. Message: \""
<< result << "\".";
free(result);
}

dart::bin::CleanupDartIo();
}

const Settings& DartVM::GetSettings() const {
Expand Down
50 changes: 50 additions & 0 deletions runtime/dart_vm_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// found in the LICENSE file.

#include "flutter/runtime/dart_vm.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/testing/testing.h"
#include "flutter/testing/thread_test.h"
#include "gtest/gtest.h"

namespace blink {
Expand Down Expand Up @@ -40,4 +43,51 @@ TEST(DartVM, CanReinitializeVMOverAndOver) {
}
}

using DartVMThreadTest = ::testing::ThreadTest;

TEST_F(DartVMThreadTest, CanRunIsolatesInANewVM) {
for (size_t i = 0; i < 1000; ++i) {
// VM should not already be running.
ASSERT_FALSE(DartVM::ForProcessIfInitialized());
auto vm = DartVM::ForProcess(kTestSettings);
ASSERT_TRUE(vm);
ASSERT_TRUE(DartVM::ForProcessIfInitialized());

Settings settings = {};

settings.task_observer_add = [](intptr_t, fml::closure) {};
settings.task_observer_remove = [](intptr_t) {};

auto labels = testing::GetCurrentTestName() + std::to_string(i);
shell::ThreadHost host(labels, shell::ThreadHost::Type::UI |
shell::ThreadHost::Type::GPU |
shell::ThreadHost::Type::IO);

TaskRunners task_runners(
labels, // task runner labels
GetCurrentTaskRunner(), // platform task runner
host.gpu_thread->GetTaskRunner(), // GPU task runner
host.ui_thread->GetTaskRunner(), // UI task runner
host.io_thread->GetTaskRunner() // IO task runner
);

auto weak_isolate = DartIsolate::CreateRootIsolate(
vm.get(), // vm
vm->GetIsolateSnapshot(), // isolate snapshot
vm->GetSharedSnapshot(), // shared snapshot
std::move(task_runners), // task runners
nullptr, // window
{}, // resource context
nullptr, // unref qeueue
"main.dart", // advisory uri
"main" // advisory entrypoint
);

auto root_isolate = weak_isolate.lock();
ASSERT_TRUE(root_isolate);
ASSERT_EQ(root_isolate->GetPhase(), DartIsolate::Phase::LibrariesSetup);
ASSERT_TRUE(root_isolate->Shutdown());
}
}

} // namespace blink
4 changes: 3 additions & 1 deletion testing/testing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace testing {

//
std::string GetCurrentTestName() {
return UnitTest::GetInstance()->current_test_info()->name();
}

} // namespace testing
4 changes: 4 additions & 0 deletions testing/testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef TESTING_TESTING_H_
#define TESTING_TESTING_H_

#include <string>

#include "gtest/gtest.h"

namespace testing {
Expand All @@ -14,6 +16,8 @@ namespace testing {
// error.
const char* GetFixturesPath();

std::string GetCurrentTestName();

} // namespace testing

#endif // TESTING_TESTING_H_

0 comments on commit 2add29d

Please sign in to comment.