• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "node_internals.h"
2 #include "libplatform/libplatform.h"
3 
4 #include <string>
5 #include "gtest/gtest.h"
6 #include "node_test_fixture.h"
7 
8 // This task increments the given run counter and reposts itself until the
9 // repost counter reaches zero.
10 class RepostingTask : public v8::Task {
11  public:
RepostingTask(int repost_count,int * run_count,v8::Isolate * isolate,node::NodePlatform * platform)12   explicit RepostingTask(int repost_count,
13                          int* run_count,
14                          v8::Isolate* isolate,
15                          node::NodePlatform* platform)
16       : repost_count_(repost_count),
17         run_count_(run_count),
18         isolate_(isolate),
19         platform_(platform) {}
20 
21   // v8::Task implementation
Run()22   void Run() final {
23     ++*run_count_;
24     if (repost_count_ > 0) {
25       --repost_count_;
26       std::shared_ptr<v8::TaskRunner> task_runner =
27           platform_->GetForegroundTaskRunner(isolate_);
28       task_runner->PostTask(std::make_unique<RepostingTask>(
29           repost_count_, run_count_, isolate_, platform_));
30     }
31   }
32 
33  private:
34   int repost_count_;
35   int* run_count_;
36   v8::Isolate* isolate_;
37   node::NodePlatform* platform_;
38 };
39 
40 class PlatformTest : public EnvironmentTestFixture {};
41 
TEST_F(PlatformTest,SkipNewTasksInFlushForegroundTasks)42 TEST_F(PlatformTest, SkipNewTasksInFlushForegroundTasks) {
43   v8::Isolate::Scope isolate_scope(isolate_);
44   const v8::HandleScope handle_scope(isolate_);
45   const Argv argv;
46   Env env {handle_scope, argv};
47   int run_count = 0;
48   std::shared_ptr<v8::TaskRunner> task_runner =
49       platform->GetForegroundTaskRunner(isolate_);
50   task_runner->PostTask(
51       std::make_unique<RepostingTask>(2, &run_count, isolate_, platform.get()));
52   EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
53   EXPECT_EQ(1, run_count);
54   EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
55   EXPECT_EQ(2, run_count);
56   EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
57   EXPECT_EQ(3, run_count);
58   EXPECT_FALSE(platform->FlushForegroundTasks(isolate_));
59 }
60 
TEST_F(PlatformTest,TracingControllerNullptr)61 TEST_F(PlatformTest, TracingControllerNullptr) {
62   v8::TracingController* orig_controller = node::GetTracingController();
63   node::SetTracingController(nullptr);
64   EXPECT_EQ(node::GetTracingController(), nullptr);
65 
66   v8::Isolate::Scope isolate_scope(isolate_);
67   const v8::HandleScope handle_scope(isolate_);
68   const Argv argv;
69   Env env {handle_scope, argv};
70 
71   node::LoadEnvironment(*env, [&](const node::StartExecutionCallbackInfo& info)
72                                   -> v8::MaybeLocal<v8::Value> {
73     return v8::Null(isolate_);
74   });
75 
76   node::SetTracingController(orig_controller);
77   EXPECT_EQ(node::GetTracingController(), orig_controller);
78 }
79