summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2017-10-10 15:39:07 +0200
committerKonstantin Käfer <mail@kkaefer.com>2017-10-10 19:34:36 +0200
commit329282fa574670c54f580ddebbc3b9f97161171b (patch)
tree144eb1312e8e605e8225f0e5e2355c9c7e0b6a9c /test
parentb820a3031885e6b2df987dc71d1b4562b7ca25f2 (diff)
downloadqtlocation-mapboxgl-upstream/prioritize-thread-pause-resume.tar.gz
[core] Allow pausing RunLoopsupstream/prioritize-thread-pause-resume
Previously we had the capability to pause on the Thread object, which used regular tasks to pause the RunLoop by blocking it. Instead, we can now pause the entire RunLoop and prevent it from processing items. This means that a pause() call is no longer treated as a regular task. Instead, it will take precedence over scheduled tasks, which means that a pause() call takes effect much more instantly since the RunLoop doesn't process the queue before the pause() task. Having pause() take effect much quicker is useful for situations where stopping the loop quickly is important, e.g. when the application goes to the background on iOS, and we have to stop processing tasks that access the file system. It also reduces the length of the blocking pause() call since the time until the RunLoop is paused is shortened.
Diffstat (limited to 'test')
-rw-r--r--test/util/thread.test.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/util/thread.test.cpp b/test/util/thread.test.cpp
index 76fb5ce3f0..c74dae2ec6 100644
--- a/test/util/thread.test.cpp
+++ b/test/util/thread.test.cpp
@@ -275,3 +275,57 @@ TEST(Thread, PauseResume) {
thread.actor().invoke(&TestWorker::send, [&] { loop.stop(); });
loop.run();
}
+
+TEST(Thread, MultiplePauseResumeCalls) {
+ RunLoop loop;
+
+ Thread<TestWorker> thread("Test");
+
+ // Test if multiple pause calls work
+ thread.pause();
+ thread.pause();
+ thread.resume();
+ thread.resume();
+
+ thread.actor().invoke(&TestWorker::send, [&] { loop.stop(); });
+ loop.run();
+}
+
+TEST(Thread, TestImmediatePause) {
+ using namespace std::chrono_literals;
+
+ RunLoop loop;
+
+ Thread<TestWorker> thread("Test");
+
+ std::promise<void> resume;
+ auto resumed = resume.get_future();
+
+ std::atomic<bool> ending { false };
+
+ thread.pause();
+ thread.actor().invoke(&TestWorker::send, [&] {
+ resume.set_value();
+
+ // Make sure we have some time to process the pause() call.
+ std::this_thread::sleep_for(300ms);
+ });
+
+ // We're scheduling a second action right after, before calling pause. Ensure that it never
+ // gets called.
+ thread.actor().invoke(&TestWorker::send, [&] {
+ EXPECT_TRUE(ending) << "callback called without ending";
+ });
+
+ thread.resume();
+ resumed.get();
+ thread.pause();
+
+ Timer timer;
+ timer.start(600ms, Duration::zero(), [&] {
+ ending = true;
+ loop.stop();
+ });
+
+ loop.run();
+}