|
| 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 | +classRepostingTask : publicv8::Task { |
| 11 | +public: |
| 12 | +explicitRepostingTask(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 |
| 22 | +voidRun() final { |
| 23 | + ++*run_count_; |
| 24 | +if (repost_count_ > 0) { |
| 25 | + --repost_count_; |
| 26 | + platform_->CallOnForegroundThread(isolate_, |
| 27 | +newRepostingTask(repost_count_, run_count_, isolate_, platform_)); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | +private: |
| 32 | +int repost_count_; |
| 33 | +int* run_count_; |
| 34 | + v8::Isolate* isolate_; |
| 35 | + node::NodePlatform* platform_; |
| 36 | +}; |
| 37 | + |
| 38 | +classPlatformTest : publicEnvironmentTestFixture {}; |
| 39 | + |
| 40 | +TEST_F(PlatformTest, SkipNewTasksInFlushForegroundTasks) { |
| 41 | + v8::Isolate::Scope isolate_scope(isolate_); |
| 42 | +const v8::HandleScope handle_scope(isolate_); |
| 43 | +const Argv argv; |
| 44 | + Env env {handle_scope, argv}; |
| 45 | +int run_count = 0; |
| 46 | + platform->CallOnForegroundThread( |
| 47 | + isolate_, newRepostingTask(2, &run_count, isolate_, platform.get())); |
| 48 | +EXPECT_TRUE(platform->FlushForegroundTasks(isolate_)); |
| 49 | +EXPECT_EQ(1, run_count); |
| 50 | +EXPECT_TRUE(platform->FlushForegroundTasks(isolate_)); |
| 51 | +EXPECT_EQ(2, run_count); |
| 52 | +EXPECT_TRUE(platform->FlushForegroundTasks(isolate_)); |
| 53 | +EXPECT_EQ(3, run_count); |
| 54 | +EXPECT_FALSE(platform->FlushForegroundTasks(isolate_)); |
| 55 | +} |
0 commit comments