Skip to content

Commit

Permalink
Allow running runtime_unittests in AOT mode. (flutter#8375)
Browse files Browse the repository at this point in the history
Previously, only the most basic tests were run in AOT mode.
  • Loading branch information
chinmaygarde authored Mar 30, 2019
1 parent ca7623e commit 972afdc
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 35 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ FILE: ../../../flutter/runtime/runtime_controller.cc
FILE: ../../../flutter/runtime/runtime_controller.h
FILE: ../../../flutter/runtime/runtime_delegate.cc
FILE: ../../../flutter/runtime/runtime_delegate.h
FILE: ../../../flutter/runtime/runtime_test.cc
FILE: ../../../flutter/runtime/runtime_test.h
FILE: ../../../flutter/runtime/service_protocol.cc
FILE: ../../../flutter/runtime/service_protocol.h
FILE: ../../../flutter/runtime/start_up.cc
Expand Down
4 changes: 3 additions & 1 deletion runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,18 @@ executable("runtime_unittests") {
"dart_isolate_unittests.cc",
"dart_service_isolate_unittests.cc",
"dart_vm_unittests.cc",
"runtime_test.cc",
"runtime_test.h",
]

deps = [
":libdart",
":runtime",
":runtime_fixtures",
"$flutter_root/common",
"$flutter_root/fml",
"$flutter_root/lib/snapshot",
"$flutter_root/testing",
"//third_party/dart/runtime:libdart_jit",
"//third_party/skia",
"//third_party/tonic",
]
Expand Down
69 changes: 36 additions & 33 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
#include "flutter/fml/thread.h"
#include "flutter/runtime/dart_isolate.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/runtime/runtime_test.h"
#include "flutter/testing/testing.h"
#include "flutter/testing/thread_test.h"
#include "third_party/tonic/scopes/dart_isolate_scope.h"

#if FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG
#define SKIP_IF_AOT() GTEST_SKIP()
#else
#define SKIP_IF_AOT() (void)0
#endif

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

namespace blink {
namespace testing {

using DartIsolateTest = ::testing::ThreadTest;
using DartIsolateTest = RuntimeTest;

TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
Settings settings = {};
SetSnapshotsAndAssets(settings);
settings.task_observer_add = [](intptr_t, fml::closure) {};
settings.task_observer_remove = [](intptr_t) {};
auto vm = DartVM::ForProcess(settings);
Expand Down Expand Up @@ -130,7 +127,7 @@ class AutoIsolateShutdown {
FML_DISALLOW_COPY_AND_ASSIGN(AutoIsolateShutdown);
};

std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
static std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
fml::RefPtr<fml::TaskRunner> task_runner,
std::string entrypoint) {
Settings settings = {};
Expand Down Expand Up @@ -174,34 +171,42 @@ std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
return {};
}

auto kernel_file_path =
fml::paths::JoinPaths({testing::GetFixturesPath(), "kernel_blob.bin"});
if (!DartVM::IsRunningPrecompiledCode()) {
auto kernel_file_path = fml::paths::JoinPaths(
{::testing::GetFixturesPath(), "kernel_blob.bin"});

if (!fml::IsFile(kernel_file_path)) {
FML_LOG(ERROR) << "Could not locate kernel file.";
return {};
}
if (!fml::IsFile(kernel_file_path)) {
FML_LOG(ERROR) << "Could not locate kernel file.";
return {};
}

auto kernel_file = fml::OpenFile(kernel_file_path.c_str(), false,
fml::FilePermission::kRead);
auto kernel_file = fml::OpenFile(kernel_file_path.c_str(), false,
fml::FilePermission::kRead);

if (!kernel_file.is_valid()) {
FML_LOG(ERROR) << "Kernel file descriptor was invalid.";
return {};
}
if (!kernel_file.is_valid()) {
FML_LOG(ERROR) << "Kernel file descriptor was invalid.";
return {};
}

auto kernel_mapping = std::make_unique<fml::FileMapping>(kernel_file);
auto kernel_mapping = std::make_unique<fml::FileMapping>(kernel_file);

if (kernel_mapping->GetMapping() == nullptr) {
FML_LOG(ERROR) << "Could not setup kernel mapping.";
return {};
}
if (kernel_mapping->GetMapping() == nullptr) {
FML_LOG(ERROR) << "Could not setup kernel mapping.";
return {};
}

if (!root_isolate->get()->PrepareForRunningFromKernel(
std::move(kernel_mapping))) {
FML_LOG(ERROR)
<< "Could not prepare to run the isolate from the kernel file.";
return {};
if (!root_isolate->get()->PrepareForRunningFromKernel(
std::move(kernel_mapping))) {
FML_LOG(ERROR)
<< "Could not prepare to run the isolate from the kernel file.";
return {};
}
} else {
if (!root_isolate->get()->PrepareForRunningFromPrecompiledCode()) {
FML_LOG(ERROR)
<< "Could not prepare to run the isolate from precompiled code.";
return {};
}
}

if (root_isolate->get()->GetPhase() != DartIsolate::Phase::Ready) {
Expand All @@ -219,21 +224,18 @@ std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate(
}

TEST_F(DartIsolateTest, IsolateCanLoadAndRunDartCode) {
SKIP_IF_AOT();
auto isolate = RunDartCodeInIsolate(GetCurrentTaskRunner(), "main");
ASSERT_TRUE(isolate);
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
}

TEST_F(DartIsolateTest, IsolateCannotLoadAndRunUnknownDartEntrypoint) {
SKIP_IF_AOT();
auto isolate =
RunDartCodeInIsolate(GetCurrentTaskRunner(), "thisShouldNotExist");
ASSERT_FALSE(isolate);
}

TEST_F(DartIsolateTest, CanRunDartCodeCodeSynchronously) {
SKIP_IF_AOT();
auto isolate = RunDartCodeInIsolate(GetCurrentTaskRunner(), "main");

ASSERT_TRUE(isolate);
Expand All @@ -247,4 +249,5 @@ TEST_F(DartIsolateTest, CanRunDartCodeCodeSynchronously) {
}));
}

} // namespace testing
} // namespace blink
1 change: 0 additions & 1 deletion runtime/dart_vm_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ TEST(DartVM, SimpleInitialization) {
auto vm = DartVM::ForProcess(settings);
ASSERT_TRUE(vm);
ASSERT_EQ(vm, DartVM::ForProcess(settings));
ASSERT_FALSE(DartVM::IsRunningPrecompiledCode());
}

TEST(DartVM, SimpleIsolateNameServer) {
Expand Down
2 changes: 2 additions & 0 deletions runtime/fixtures/simple_main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
void main() {
}

@pragma('vm:entry-point')
void sayHi() {
print("Hi");
}

@pragma('vm:entry-point')
void throwExceptionNow() {
throw("Hello");
}
87 changes: 87 additions & 0 deletions runtime/runtime_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "flutter/runtime/runtime_test.h"

#include "flutter/runtime/dart_vm.h"
#include "flutter/testing/testing.h"

namespace blink {
namespace testing {

RuntimeTest::RuntimeTest() = default;

RuntimeTest::~RuntimeTest() = default;

static std::unique_ptr<fml::Mapping> GetMapping(const fml::UniqueFD& directory,
const char* path,
bool executable) {
fml::UniqueFD file = fml::OpenFile(directory, path, false /* create */,
fml::FilePermission::kRead);
if (!file.is_valid()) {
return nullptr;
}

using Prot = fml::FileMapping::Protection;
std::unique_ptr<fml::FileMapping> mapping;
if (executable) {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead, Prot::kExecute});
} else {
mapping = std::make_unique<fml::FileMapping>(
file, std::initializer_list<Prot>{Prot::kRead});
}

if (mapping->GetSize() == 0 || mapping->GetMapping() == nullptr) {
return nullptr;
}

return mapping;
}

void RuntimeTest::SetSnapshotsAndAssets(Settings& settings) {
if (!assets_dir_.is_valid()) {
return;
}

settings.assets_dir = assets_dir_.get();

// In JIT execution, all snapshots are present within the binary itself and
// don't need to be explicitly suppiled by the embedder.
if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_data = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_data", false);
};

settings.isolate_snapshot_data = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_data", false);
};

if (DartVM::IsRunningPrecompiledCode()) {
settings.vm_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "vm_snapshot_instr", true);
};

settings.isolate_snapshot_instr = [this]() {
return GetMapping(assets_dir_, "isolate_snapshot_instr", true);
};
}
}
}

// |testing::ThreadTest|
void RuntimeTest::SetUp() {
assets_dir_ = fml::OpenDirectory(::testing::GetFixturesPath(), false,
fml::FilePermission::kRead);
ThreadTest::SetUp();
}

// |testing::ThreadTest|
void RuntimeTest::TearDown() {
ThreadTest::TearDown();
assets_dir_.reset();
}

} // namespace testing
} // namespace blink
37 changes: 37 additions & 0 deletions runtime/runtime_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_RUNTIME_RUNTIME_TEST_H_
#define FLUTTER_RUNTIME_RUNTIME_TEST_H_

#include "flutter/common/settings.h"
#include "flutter/fml/macros.h"
#include "flutter/testing/thread_test.h"

namespace blink {
namespace testing {

class RuntimeTest : public ::testing::ThreadTest {
public:
RuntimeTest();

~RuntimeTest();

void SetSnapshotsAndAssets(Settings& settings);

protected:
// |testing::ThreadTest|
void SetUp() override;

// |testing::ThreadTest|
void TearDown() override;

private:
fml::UniqueFD assets_dir_;
};

} // namespace testing
} // namespace blink

#endif // FLUTTER_RUNTIME_RUNTIME_TEST_H_
2 changes: 2 additions & 0 deletions testing/thread_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace testing {

// |testing::Test|
void ThreadTest::SetUp() {
thread_ = std::make_unique<fml::Thread>();
thread_task_runner_ = thread_->GetTaskRunner();
Expand All @@ -16,6 +17,7 @@ void ThreadTest::SetUp() {
current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
}

// |testing::Test|
void ThreadTest::TearDown() {
thread_task_runner_ = nullptr;
thread_ = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions testing/thread_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class ThreadTest : public Test {
fml::RefPtr<fml::TaskRunner> GetThreadTaskRunner();

protected:
// |testing::Test|
void SetUp() override;

// |testing::Test|
void TearDown() override;

private:
Expand Down

0 comments on commit 972afdc

Please sign in to comment.