Skip to content

Commit

Permalink
Add unit tests for RuntimeScheduler::now
Browse files Browse the repository at this point in the history
Summary:
changelog: [internal]

Add unit tests for `RuntimeScheduler::now`

Reviewed By: JoshuaGross

Differential Revision: D27763852

fbshipit-source-id: 1edec8e8338e9d9798b95c55d07114be05f555b8
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Apr 15, 2021
1 parent eb49f95 commit e3a4de8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
11 changes: 8 additions & 3 deletions ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
namespace facebook::react {

RuntimeScheduler::RuntimeScheduler(RuntimeExecutor const &runtimeExecutor)
: runtimeExecutor_(runtimeExecutor) {}
: RuntimeScheduler(runtimeExecutor, RuntimeSchedulerClock::now) {}

RuntimeScheduler::RuntimeScheduler(
RuntimeExecutor const &runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now)
: runtimeExecutor_(runtimeExecutor), now_(now) {}

std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
SchedulerPriority priority,
Expand All @@ -36,8 +41,8 @@ bool RuntimeScheduler::getShouldYield() const {
return shouldYield_;
}

RuntimeSchedulerClock::time_point RuntimeScheduler::now() const {
return RuntimeSchedulerClock::now();
RuntimeSchedulerTimePoint RuntimeScheduler::now() const {
return now_();
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
#include <react/renderer/runtimescheduler/Task.h>
#include <atomic>
#include <chrono>
#include <memory>
#include <queue>

Expand All @@ -20,6 +19,9 @@ namespace facebook::react {
class RuntimeScheduler final {
public:
RuntimeScheduler(RuntimeExecutor const &runtimeExecutor);
RuntimeScheduler(
RuntimeExecutor const &runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now);

std::shared_ptr<Task> scheduleTask(
SchedulerPriority priority,
Expand All @@ -29,7 +31,7 @@ class RuntimeScheduler final {

bool getShouldYield() const;

RuntimeSchedulerClock::time_point now() const;
RuntimeSchedulerTimePoint now() const;

private:
mutable std::priority_queue<
Expand All @@ -39,6 +41,7 @@ class RuntimeScheduler final {
taskQueue_;
RuntimeExecutor const runtimeExecutor_;
std::atomic_bool shouldYield_{false};
std::function<RuntimeSchedulerTimePoint()> now_;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ namespace facebook::react {
*/
using RuntimeSchedulerClock = std::chrono::steady_clock;

using RuntimeSchedulerTimePoint = RuntimeSchedulerClock::time_point;
using RuntimeSchedulerDuration = RuntimeSchedulerClock::duration;

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <gtest/gtest.h>
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include "StubClock.h"

using namespace facebook::react;
using namespace std::chrono_literals;

TEST(RuntimeSchedulerTest, now) {
facebook::jsi::Runtime *runtime;

RuntimeExecutor runtimeExecutor =
[&runtime](
std::function<void(facebook::jsi::Runtime & runtime)> &&callback) {
// TODO: This will crash if executed.
callback(*runtime);
};

auto stubClock = StubClock();
stubClock.setTimePoint(1ms);

auto stubNow = [&stubClock]() -> RuntimeSchedulerTimePoint {
return stubClock.getNow();
};

auto runtimeScheduler = RuntimeScheduler(runtimeExecutor, stubNow);
EXPECT_EQ(runtimeScheduler.now(), RuntimeSchedulerTimePoint(1ms));

stubClock.advanceTimeBy(10ms);

EXPECT_EQ(runtimeScheduler.now(), RuntimeSchedulerTimePoint(11ms));

stubClock.advanceTimeBy(6s);

EXPECT_EQ(runtimeScheduler.now(), RuntimeSchedulerTimePoint(6011ms));
}
41 changes: 41 additions & 0 deletions ReactCommon/react/renderer/runtimescheduler/tests/StubClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
#include <chrono>

namespace facebook::react {

class StubClock {
public:
RuntimeSchedulerTimePoint getNow() const {
return timePoint_;
}

void setTimePoint(RuntimeSchedulerTimePoint timePoint) {
timePoint_ = timePoint;
}

void setTimePoint(RuntimeSchedulerDuration duration) {
timePoint_ = RuntimeSchedulerTimePoint(duration);
}

RuntimeSchedulerTimePoint getTimePoint() {
return timePoint_;
}

void advanceTimeBy(RuntimeSchedulerDuration duration) {
timePoint_ += duration;
}

private:
RuntimeSchedulerTimePoint timePoint_;
};

} // namespace facebook::react

0 comments on commit e3a4de8

Please sign in to comment.