forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for RuntimeScheduler::now
Summary: changelog: [internal] Add unit tests for `RuntimeScheduler::now` Reviewed By: JoshuaGross Differential Revision: D27763852 fbshipit-source-id: 1edec8e8338e9d9798b95c55d07114be05f555b8
- Loading branch information
1 parent
eb49f95
commit e3a4de8
Showing
5 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
ReactCommon/react/renderer/runtimescheduler/tests/StubClock.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |