|
| 1 | +#include "node.h" |
| 2 | +#include "env.h" |
| 3 | +#include "v8.h" |
| 4 | +#include "libplatform/libplatform.h" |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include "gtest/gtest.h" |
| 8 | +#include "node_test_fixture.h" |
| 9 | + |
| 10 | +using node::Environment; |
| 11 | +using node::IsolateData; |
| 12 | +using node::CreateIsolateData; |
| 13 | +using node::FreeIsolateData; |
| 14 | +using node::CreateEnvironment; |
| 15 | +using node::FreeEnvironment; |
| 16 | +using node::AtExit; |
| 17 | +using node::RunAtExit; |
| 18 | + |
| 19 | +static bool called_cb_1 = false; |
| 20 | +static bool called_cb_2 = false; |
| 21 | +static void at_exit_callback1(void* arg); |
| 22 | +static void at_exit_callback2(void* arg); |
| 23 | +static std::string cb_1_arg; // NOLINT(runtime/string) |
| 24 | + |
| 25 | +class EnvironmentTest : public NodeTestFixture { |
| 26 | + public: |
| 27 | + class Env { |
| 28 | + public: |
| 29 | + Env(const v8::HandleScope& handle_scope, |
| 30 | + v8::Isolate* isolate, |
| 31 | + const Argv& argv) { |
| 32 | + context_ = v8::Context::New(isolate); |
| 33 | + CHECK(!context_.IsEmpty()); |
| 34 | + isolate_data_ = CreateIsolateData(isolate, uv_default_loop()); |
| 35 | + CHECK_NE(nullptr, isolate_data_); |
| 36 | + environment_ = CreateEnvironment(isolate_data_, |
| 37 | + context_, |
| 38 | + 1, *argv, |
| 39 | + argv.nr_args(), *argv); |
| 40 | + CHECK_NE(nullptr, environment_); |
| 41 | + } |
| 42 | + |
| 43 | + ~Env() { |
| 44 | + FreeIsolateData(isolate_data_); |
| 45 | + FreeEnvironment(environment_); |
| 46 | + } |
| 47 | + |
| 48 | + Environment* operator*() const { |
| 49 | + return environment_; |
| 50 | + } |
| 51 | + |
| 52 | + private: |
| 53 | + v8::Local<v8::Context> context_; |
| 54 | + IsolateData* isolate_data_; |
| 55 | + Environment* environment_; |
| 56 | + }; |
| 57 | + |
| 58 | + private: |
| 59 | + virtual void TearDown() { |
| 60 | + NodeTestFixture::TearDown(); |
| 61 | + called_cb_1 = false; |
| 62 | + called_cb_2 = false; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +TEST_F(EnvironmentTest, AtExitWithEnvironment) { |
| 67 | + const v8::HandleScope handle_scope(isolate_); |
| 68 | + const Argv argv; |
| 69 | + Env env {handle_scope, isolate_, argv}; |
| 70 | + |
| 71 | + AtExit(*env, at_exit_callback1); |
| 72 | + RunAtExit(*env); |
| 73 | + EXPECT_TRUE(called_cb_1); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_F(EnvironmentTest, AtExitWithArgument) { |
| 77 | + const v8::HandleScope handle_scope(isolate_); |
| 78 | + const Argv argv; |
| 79 | + Env env {handle_scope, isolate_, argv}; |
| 80 | + |
| 81 | + std::string arg{"some args"}; |
| 82 | + AtExit(*env, at_exit_callback1, static_cast<void*>(&arg)); |
| 83 | + RunAtExit(*env); |
| 84 | + EXPECT_EQ(arg, cb_1_arg); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) { |
| 88 | + const v8::HandleScope handle_scope(isolate_); |
| 89 | + const Argv argv; |
| 90 | + Env env1 {handle_scope, isolate_, argv}; |
| 91 | + Env env2 {handle_scope, isolate_, argv}; |
| 92 | + |
| 93 | + AtExit(*env1, at_exit_callback1); |
| 94 | + AtExit(*env2, at_exit_callback2); |
| 95 | + RunAtExit(*env1); |
| 96 | + EXPECT_TRUE(called_cb_1); |
| 97 | + EXPECT_FALSE(called_cb_2); |
| 98 | + |
| 99 | + RunAtExit(*env2); |
| 100 | + EXPECT_TRUE(called_cb_2); |
| 101 | +} |
| 102 | + |
| 103 | +static void at_exit_callback1(void* arg) { |
| 104 | + called_cb_1 = true; |
| 105 | + if (arg) { |
| 106 | + cb_1_arg = *static_cast<std::string*>(arg); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +static void at_exit_callback2(void* arg) { |
| 111 | + called_cb_2 = true; |
| 112 | +} |
0 commit comments