Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / js / src / jsapi-tests / testThreadingConditionVariable.cpp
blob1bff19437210fecf7d248524408b95f995cac7a5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jsapi-tests/tests.h"
9 #include "threading/ConditionVariable.h"
10 #include "threading/Thread.h"
11 #include "vm/MutexIDs.h"
13 struct TestState {
14 js::Mutex mutex MOZ_UNANNOTATED;
15 js::ConditionVariable condition;
16 bool flag;
17 js::Thread testThread;
19 explicit TestState(bool createThread = true)
20 : mutex(js::mutexid::TestMutex), flag(false) {
21 if (createThread) {
22 MOZ_RELEASE_ASSERT(testThread.init(setFlag, this));
26 static void setFlag(TestState* state) {
27 js::UniqueLock<js::Mutex> lock(state->mutex);
28 state->flag = true;
29 state->condition.notify_one();
32 void join() { testThread.join(); }
35 BEGIN_TEST(testThreadingConditionVariable) {
36 auto state = mozilla::MakeUnique<TestState>();
38 js::UniqueLock<js::Mutex> lock(state->mutex);
39 while (!state->flag) {
40 state->condition.wait(lock);
43 state->join();
45 CHECK(state->flag);
47 return true;
49 END_TEST(testThreadingConditionVariable)
51 BEGIN_TEST(testThreadingConditionVariablePredicate) {
52 auto state = mozilla::MakeUnique<TestState>();
54 js::UniqueLock<js::Mutex> lock(state->mutex);
55 state->condition.wait(lock, [&state]() { return state->flag; });
57 state->join();
59 CHECK(state->flag);
61 return true;
63 END_TEST(testThreadingConditionVariablePredicate)
65 BEGIN_TEST(testThreadingConditionVariableUntilOkay) {
66 auto state = mozilla::MakeUnique<TestState>();
68 js::UniqueLock<js::Mutex> lock(state->mutex);
69 while (!state->flag) {
70 auto to =
71 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromSeconds(600);
72 js::CVStatus res = state->condition.wait_until(lock, to);
73 CHECK(res == js::CVStatus::NoTimeout);
76 state->join();
78 CHECK(state->flag);
80 return true;
82 END_TEST(testThreadingConditionVariableUntilOkay)
84 BEGIN_TEST(testThreadingConditionVariableUntilTimeout) {
85 auto state = mozilla::MakeUnique<TestState>(false);
87 js::UniqueLock<js::Mutex> lock(state->mutex);
88 while (!state->flag) {
89 auto to = mozilla::TimeStamp::Now() +
90 mozilla::TimeDuration::FromMilliseconds(10);
91 js::CVStatus res = state->condition.wait_until(lock, to);
92 if (res == js::CVStatus::Timeout) {
93 break;
97 CHECK(!state->flag);
99 // Timeout in the past should return with timeout immediately.
101 js::UniqueLock<js::Mutex> lock(state->mutex);
102 auto to =
103 mozilla::TimeStamp::Now() - mozilla::TimeDuration::FromMilliseconds(10);
104 js::CVStatus res = state->condition.wait_until(lock, to);
105 CHECK(res == js::CVStatus::Timeout);
108 return true;
110 END_TEST(testThreadingConditionVariableUntilTimeout)
112 BEGIN_TEST(testThreadingConditionVariableUntilOkayPredicate) {
113 auto state = mozilla::MakeUnique<TestState>();
115 js::UniqueLock<js::Mutex> lock(state->mutex);
116 auto to =
117 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromSeconds(600);
118 bool res = state->condition.wait_until(lock, to,
119 [&state]() { return state->flag; });
120 CHECK(res);
122 state->join();
124 CHECK(state->flag);
126 return true;
128 END_TEST(testThreadingConditionVariableUntilOkayPredicate)
130 BEGIN_TEST(testThreadingConditionVariableUntilTimeoutPredicate) {
131 auto state = mozilla::MakeUnique<TestState>(false);
133 js::UniqueLock<js::Mutex> lock(state->mutex);
134 auto to =
135 mozilla::TimeStamp::Now() + mozilla::TimeDuration::FromMilliseconds(10);
136 bool res = state->condition.wait_until(lock, to,
137 [&state]() { return state->flag; });
138 CHECK(!res);
140 CHECK(!state->flag);
142 return true;
144 END_TEST(testThreadingConditionVariableUntilTimeoutPredicate)
146 BEGIN_TEST(testThreadingConditionVariableForOkay) {
147 auto state = mozilla::MakeUnique<TestState>();
149 js::UniqueLock<js::Mutex> lock(state->mutex);
150 while (!state->flag) {
151 auto duration = mozilla::TimeDuration::FromSeconds(600);
152 js::CVStatus res = state->condition.wait_for(lock, duration);
153 CHECK(res == js::CVStatus::NoTimeout);
156 state->join();
158 CHECK(state->flag);
160 return true;
162 END_TEST(testThreadingConditionVariableForOkay)
164 BEGIN_TEST(testThreadingConditionVariableForTimeout) {
165 auto state = mozilla::MakeUnique<TestState>(false);
167 js::UniqueLock<js::Mutex> lock(state->mutex);
168 while (!state->flag) {
169 auto duration = mozilla::TimeDuration::FromMilliseconds(10);
170 js::CVStatus res = state->condition.wait_for(lock, duration);
171 if (res == js::CVStatus::Timeout) {
172 break;
176 CHECK(!state->flag);
178 // Timeout in the past should return with timeout immediately.
180 js::UniqueLock<js::Mutex> lock(state->mutex);
181 auto duration = mozilla::TimeDuration::FromMilliseconds(-10);
182 js::CVStatus res = state->condition.wait_for(lock, duration);
183 CHECK(res == js::CVStatus::Timeout);
186 return true;
188 END_TEST(testThreadingConditionVariableForTimeout)
190 BEGIN_TEST(testThreadingConditionVariableForOkayPredicate) {
191 auto state = mozilla::MakeUnique<TestState>();
193 js::UniqueLock<js::Mutex> lock(state->mutex);
194 auto duration = mozilla::TimeDuration::FromSeconds(600);
195 bool res = state->condition.wait_for(lock, duration,
196 [&state]() { return state->flag; });
197 CHECK(res);
199 state->join();
201 CHECK(state->flag);
203 return true;
205 END_TEST(testThreadingConditionVariableForOkayPredicate)
207 BEGIN_TEST(testThreadingConditionVariableForTimeoutPredicate) {
208 auto state = mozilla::MakeUnique<TestState>(false);
210 js::UniqueLock<js::Mutex> lock(state->mutex);
211 auto duration = mozilla::TimeDuration::FromMilliseconds(10);
212 bool res = state->condition.wait_for(lock, duration,
213 [&state]() { return state->flag; });
214 CHECK(!res);
216 CHECK(!state->flag);
218 return true;
220 END_TEST(testThreadingConditionVariableForTimeoutPredicate)