-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "TimeSource" class #1153
Merged
Merged
Add a "TimeSource" class #1153
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
391f2ef
Add a "TimeSource" class so that time specific methods can be substit…
andreilitvin fca866b
Add export of unit test artifacts for esp32 qemu tests
andreilitvin f522673
Merge branch 'master' into 01_add_time_source
andreilitvin b98dafc
Fix initializer for unit test
andreilitvin d41bf39
Merge branch 'master' into 01_add_time_source
andreilitvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @brief defines a generic time source interface that uses a real clock | ||
* at runtime but can be substituted by a test one for unit tests. | ||
*/ | ||
#ifndef TIME_SOURCE_H_ | ||
#define TIME_SOURCE_H_ | ||
|
||
#include <system/SystemClock.h> | ||
|
||
namespace chip { | ||
namespace Time { | ||
|
||
enum class Source | ||
{ | ||
kSystem, // System time source | ||
kTest, // Test time source | ||
}; | ||
|
||
/** | ||
* Defines a generic time source within a system. System time and test times | ||
* are available. | ||
*/ | ||
template <Source kSource> | ||
class TimeSource | ||
{ | ||
public: | ||
/** | ||
* Returns a monotonically increasing time in milliseconds since an arbitrary, platform-defined | ||
* epoch. | ||
* | ||
* Maintains requirements for the System::Platform::Layer clock implementation: | ||
* | ||
* - Return a value that is ever-increasing (i.e. never * wraps) between reboots of the system. | ||
* - The underlying time source is required to tick continuously during any system sleep modes | ||
* such that the values do not entail a restart upon wake. | ||
* - This function is expected to be thread-safe on any platform that employs threading. | ||
*/ | ||
uint64_t GetCurrentMonotonicTimeMs(); | ||
}; | ||
|
||
/** | ||
* A system time source, based on the system platform layer. | ||
*/ | ||
template <> | ||
class TimeSource<Source::kSystem> | ||
{ | ||
public: | ||
uint64_t GetCurrentMonotonicTimeMs() { return System::Platform::Layer::GetClock_MonotonicMS(); } | ||
}; | ||
|
||
/** | ||
* A test time source. Allows setting the current time. | ||
*/ | ||
template <> | ||
class TimeSource<Source::kTest> | ||
{ | ||
public: | ||
uint64_t GetCurrentMonotonicTimeMs() { return mCurrentTimeMs; } | ||
|
||
void SetCurrentMonotonicTimeMs(uint64_t value) | ||
{ | ||
VerifyOrDie(value >= mCurrentTimeMs); // required contract | ||
mCurrentTimeMs = value; | ||
} | ||
|
||
private: | ||
uint64_t mCurrentTimeMs = 0; | ||
}; | ||
|
||
} // namespace Time | ||
} // namespace chip | ||
|
||
#endif // TIME_SOURCE_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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This is a unit test suite for <tt>chip::Time::TimeSource</tt>. Tests mainly | ||
* the ability to compile and use the test implementation of the time source. | ||
*/ | ||
|
||
#ifndef __STDC_LIMIT_MACROS | ||
#define __STDC_LIMIT_MACROS | ||
#endif | ||
// config | ||
#include <system/SystemConfig.h> | ||
|
||
// module header | ||
#include "TestSystemLayer.h" | ||
|
||
#include <nlunit-test.h> | ||
#include <support/CodeUtils.h> | ||
#include <support/ErrorStr.h> | ||
#include <support/TestUtils.h> | ||
#include <system/TimeSource.h> | ||
|
||
namespace { | ||
|
||
void TestTimeSourceSetAndGet(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
|
||
chip::Time::TimeSource<chip::Time::Source::kTest> source; | ||
|
||
NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 0); | ||
|
||
source.SetCurrentMonotonicTimeMs(1234); | ||
NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 1234); | ||
} | ||
|
||
void SystemTimeSourceGet(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
|
||
chip::Time::TimeSource<chip::Time::Source::kSystem> source; | ||
|
||
uint64_t oldValue = source.GetCurrentMonotonicTimeMs(); | ||
|
||
// a basic monotonic test. This is likely to take less than 1ms, so the | ||
// actual test value lies mostly in ensuring things compile. | ||
for (int i = 0; i < 100; i++) | ||
{ | ||
uint64_t newValue = source.GetCurrentMonotonicTimeMs(); | ||
NL_TEST_ASSERT(inSuite, newValue >= oldValue); | ||
oldValue = newValue; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
/** | ||
* Test Suite. It lists all the test functions. | ||
*/ | ||
// clang-format off | ||
static const nlTest sTests[] = | ||
{ | ||
NL_TEST_DEF("TimeSource<Test>::SetAndGet", TestTimeSourceSetAndGet), | ||
NL_TEST_DEF("TimeSource<System>::SetAndGet", SystemTimeSourceGet), | ||
NL_TEST_SENTINEL() | ||
}; | ||
// clang-format on | ||
|
||
int TestTimeSource(void) | ||
{ | ||
nlTestSuite theSuite = { | ||
"chip-timesource", &sTests[0], NULL /* setup */, NULL /* teardown */ | ||
}; | ||
|
||
// Run test suit againt one context. | ||
nlTestRunner(&theSuite, NULL /* context */); | ||
|
||
return (nlTestRunnerStats(&theSuite)); | ||
} | ||
|
||
static void __attribute__((constructor)) TestTimeSourceCtor(void) | ||
{ | ||
VerifyOrDie(chip::RegisterUnitTests(&TestTimeSource) == CHIP_NO_ERROR); | ||
} |
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,36 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file implements a standalone/native program executable | ||
* test driver for the CHIP system layer library timer unit | ||
* tests. | ||
* | ||
*/ | ||
|
||
#include "TestSystemLayer.h" | ||
|
||
#include <nlunit-test.h> | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
// Generate machine-readable, comma-separated value (CSV) output. | ||
nlTestSetOutputStyle(OUTPUT_CSV); | ||
|
||
return TestTimeSource(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't there be a companion Set... for the kSystem that's a no-op?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of that, however it does not seem like the platform layer has a usable set time and I did not want to deceive users.
I could have a CHIP_ERROR return that returns error on system and success on test ... but then I wonder why bother if compiler can already catch correct vs incorrect usages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LMK if you think a CHIP_ERROR Set* method would be useful in all instances and I can add it. As it stands, I am leaning towards compile time error rather than runtime, but I agree that this may confuse some compilers (I was surprised it built myself .... but hey, I;ll take it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm generally for "fly what you test and test what you fly", so this seems a bit akimbo to that to begin with; however, I think, in this case, a compile-time error is probably better than a run-time error.