summaryrefslogtreecommitdiff
path: root/test/cctest
diff options
context:
space:
mode:
authorUlan Degenbaev <ulan@chromium.org>2018-04-12 22:01:11 +0200
committerAnatoli Papirovski <apapirovski@mac.com>2018-04-25 10:40:16 +0200
commitd3edf2fcde0c37206dc339aab7333212d20aa0f1 (patch)
treefaf96de3d8b841f6dda9f7b0a049c67289862659 /test/cctest
parent95197ed2b052b1fee312cfc127a9f67844dd77b7 (diff)
downloadandroid-node-v8-d3edf2fcde0c37206dc339aab7333212d20aa0f1.tar.gz
android-node-v8-d3edf2fcde0c37206dc339aab7333212d20aa0f1.tar.bz2
android-node-v8-d3edf2fcde0c37206dc339aab7333212d20aa0f1.zip
src: limit foreground tasks draining loop
Foreground tasks that repost themselves can force the draining loop to run indefinitely long without giving other tasks chance to run. This limits the foreground task draining loop to run only the tasks that were in the tasks queue at the beginning of the loop. PR-URL: https://github.com/nodejs/node/pull/19987 Fixes: https://github.com/nodejs/node/issues/19937 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yang Guo <yangguo@chromium.org> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Khaidi Chu <i@2333.moe>
Diffstat (limited to 'test/cctest')
-rw-r--r--test/cctest/test_platform.cc55
1 files changed, 55 insertions, 0 deletions
diff --git a/test/cctest/test_platform.cc b/test/cctest/test_platform.cc
new file mode 100644
index 0000000000..876547480b
--- /dev/null
+++ b/test/cctest/test_platform.cc
@@ -0,0 +1,55 @@
+#include "node_internals.h"
+#include "libplatform/libplatform.h"
+
+#include <string>
+#include "gtest/gtest.h"
+#include "node_test_fixture.h"
+
+// This task increments the given run counter and reposts itself until the
+// repost counter reaches zero.
+class RepostingTask : public v8::Task {
+ public:
+ explicit RepostingTask(int repost_count,
+ int* run_count,
+ v8::Isolate* isolate,
+ node::NodePlatform* platform)
+ : repost_count_(repost_count),
+ run_count_(run_count),
+ isolate_(isolate),
+ platform_(platform) {}
+
+ // v8::Task implementation
+ void Run() final {
+ ++*run_count_;
+ if (repost_count_ > 0) {
+ --repost_count_;
+ platform_->CallOnForegroundThread(isolate_,
+ new RepostingTask(repost_count_, run_count_, isolate_, platform_));
+ }
+ }
+
+ private:
+ int repost_count_;
+ int* run_count_;
+ v8::Isolate* isolate_;
+ node::NodePlatform* platform_;
+};
+
+class PlatformTest : public EnvironmentTestFixture {};
+
+TEST_F(PlatformTest, SkipNewTasksInFlushForegroundTasks) {
+ v8::Isolate::Scope isolate_scope(isolate_);
+ const v8::HandleScope handle_scope(isolate_);
+ const Argv argv;
+ Env env {handle_scope, argv};
+ int run_count = 0;
+ platform->CallOnForegroundThread(
+ isolate_, new RepostingTask(2, &run_count, isolate_, platform.get()));
+ EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
+ EXPECT_EQ(1, run_count);
+ EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
+ EXPECT_EQ(2, run_count);
+ EXPECT_TRUE(platform->FlushForegroundTasks(isolate_));
+ EXPECT_EQ(3, run_count);
+ EXPECT_FALSE(platform->FlushForegroundTasks(isolate_));
+}