Skip to content

Commit fd18225

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Add unit tests for RuntimeScheduler::executeNowOnTheSameThread
Summary: Changelog: [internal] Reviewed By: philIip Differential Revision: D33062204 fbshipit-source-id: f37375400fea645f5540b85c3c1ef6343e64be2e
1 parent 6a046fb commit fd18225

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,64 @@ TEST_F(RuntimeSchedulerTest, handlingError) {
494494
EXPECT_EQ(stubErrorUtils_->getReportFatalCallCount(), 1);
495495
}
496496

497+
TEST_F(RuntimeSchedulerTest, basicSameThreadExecution) {
498+
bool didRunSynchronousTask = false;
499+
std::thread t1([this, &didRunSynchronousTask]() {
500+
runtimeScheduler_->executeNowOnTheSameThread(
501+
[this, &didRunSynchronousTask](jsi::Runtime &rt) {
502+
EXPECT_TRUE(runtimeScheduler_->getIsSynchronous());
503+
didRunSynchronousTask = true;
504+
});
505+
EXPECT_FALSE(runtimeScheduler_->getIsSynchronous());
506+
});
507+
508+
auto hasTask = stubQueue_->waitForTask(1ms);
509+
510+
EXPECT_TRUE(hasTask);
511+
EXPECT_FALSE(didRunSynchronousTask);
512+
EXPECT_EQ(stubQueue_->size(), 1);
513+
514+
stubQueue_->tick();
515+
516+
t1.join();
517+
518+
EXPECT_TRUE(didRunSynchronousTask);
519+
}
520+
521+
TEST_F(RuntimeSchedulerTest, sameThreadTaskCreatesImmediatePriorityTask) {
522+
bool didRunSynchronousTask = false;
523+
bool didRunSubsequentTask = false;
524+
std::thread t1([this, &didRunSynchronousTask, &didRunSubsequentTask]() {
525+
runtimeScheduler_->executeNowOnTheSameThread(
526+
[this, &didRunSynchronousTask, &didRunSubsequentTask](
527+
jsi::Runtime &rt) {
528+
didRunSynchronousTask = true;
529+
530+
auto callback = createHostFunctionFromLambda(
531+
[&didRunSubsequentTask](bool didUserCallbackTimeout) {
532+
didRunSubsequentTask = true;
533+
EXPECT_FALSE(didUserCallbackTimeout);
534+
return jsi::Value::undefined();
535+
});
536+
537+
runtimeScheduler_->scheduleTask(
538+
SchedulerPriority::ImmediatePriority, std::move(callback));
539+
});
540+
});
541+
542+
auto hasTask = stubQueue_->waitForTask(1ms);
543+
544+
EXPECT_TRUE(hasTask);
545+
EXPECT_FALSE(didRunSynchronousTask);
546+
EXPECT_FALSE(didRunSubsequentTask);
547+
EXPECT_EQ(stubQueue_->size(), 1);
548+
549+
stubQueue_->tick();
550+
551+
t1.join();
552+
553+
EXPECT_TRUE(didRunSynchronousTask);
554+
EXPECT_TRUE(didRunSubsequentTask);
555+
}
556+
497557
} // namespace facebook::react

ReactCommon/react/renderer/runtimescheduler/tests/StubQueue.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,48 @@
1212
class StubQueue {
1313
public:
1414
void runOnQueue(std::function<void()> &&func) {
15-
callbackQueue_.push(func);
15+
{
16+
std::lock_guard<std::mutex> lock(mutex_);
17+
callbackQueue_.push(func);
18+
}
19+
20+
signal_.notify_one();
1621
}
1722

1823
void flush() {
19-
while (!callbackQueue_.empty()) {
24+
while (size() > 0) {
2025
tick();
2126
}
2227
}
2328

2429
void tick() {
25-
if (!callbackQueue_.empty()) {
26-
auto callback = callbackQueue_.front();
30+
std::function<void()> callback;
31+
{
32+
std::lock_guard<std::mutex> lock(mutex_);
33+
if (!callbackQueue_.empty()) {
34+
callback = callbackQueue_.front();
35+
callbackQueue_.pop();
36+
}
37+
}
38+
39+
if (callback) {
2740
callback();
28-
callbackQueue_.pop();
2941
}
3042
}
3143

32-
int size() {
44+
size_t size() const {
45+
std::lock_guard<std::mutex> lock(mutex_);
3346
return callbackQueue_.size();
3447
}
3548

49+
bool waitForTask(std::chrono::duration<double> timeout) const {
50+
std::unique_lock<std::mutex> lock(mutex_);
51+
return signal_.wait_for(
52+
lock, timeout, [this]() { return !callbackQueue_.empty(); });
53+
}
54+
3655
private:
56+
mutable std::condition_variable signal_;
57+
mutable std::mutex mutex_;
3758
std::queue<std::function<void()>> callbackQueue_;
3859
};

0 commit comments

Comments
 (0)