Skip to content

Commit

Permalink
refactor(test): Use the fake clock also for non-monotonic time-source
Browse files Browse the repository at this point in the history
The subscription tests depend on a deterministic clock.
  • Loading branch information
jpfr committed Dec 25, 2023
1 parent 1365c3f commit 054420c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion tests/client/check_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ START_TEST(ClientConfig_Copy){
UA_ClientConfig srcConfig;
memset(&srcConfig, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefault(&srcConfig);
srcConfig.eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
srcConfig.eventLoop->dateTime_now = UA_DateTime_now_fake;
srcConfig.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;

UA_StatusCode retval = UA_ClientConfig_copy(&srcConfig, &dstConfig);
ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
Expand Down
9 changes: 6 additions & 3 deletions tests/server/check_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ registerServer(void) {
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefaultEncryption(&cc, certificate, privateKey, NULL, 0, NULL, 0);
UA_CertificateVerification_AcceptAll(&cc.certificateVerification);
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
cc.eventLoop->dateTime_now = UA_DateTime_now_fake;
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;

*running_register = false;
THREAD_JOIN(server_thread_register);
Expand Down Expand Up @@ -222,7 +223,8 @@ unregisterServer(void) {
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefaultEncryption(&cc, certificate, privateKey, NULL, 0, NULL, 0);
UA_CertificateVerification_AcceptAll(&cc.certificateVerification);
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
cc.eventLoop->dateTime_now = UA_DateTime_now_fake;
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;

*running_register = false;
THREAD_JOIN(server_thread_register);
Expand Down Expand Up @@ -266,7 +268,8 @@ Server_register_semaphore(void) {
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefaultEncryption(&cc, certificate, privateKey, NULL, 0, NULL, 0);
UA_CertificateVerification_AcceptAll(&cc.certificateVerification);
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
cc.eventLoop->dateTime_now = UA_DateTime_now_fake;
cc.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;

*running_register = false;
THREAD_JOIN(server_thread_register);
Expand Down
6 changes: 4 additions & 2 deletions tests/testing-plugins/test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ UA_Server * UA_Server_newForUnitTest(void) {
return NULL;
UA_ServerConfig *config = UA_Server_getConfig(server);
/* Manually set the eventloop clock to the fake clock */
config->eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
config->eventLoop->dateTime_now = UA_DateTime_now_fake;
config->eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
return server;
}

Expand All @@ -27,7 +28,8 @@ UA_Client * UA_Client_newForUnitTest(void) {
return NULL;
UA_ClientConfig *config = UA_Client_getConfig(client);
/* Manually set the eventloop clock to the fake clock */
config->eventLoop->dateTime_nowMonotonic = UA_DateTime_nowMonotonic_fake;
config->eventLoop->dateTime_now = UA_DateTime_now_fake;
config->eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
return client;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testing-plugins/testing_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ UA_realSleep(UA_UInt32 duration) {
#endif
}

UA_DateTime UA_DateTime_nowMonotonic_fake(UA_EventLoop *el) {
UA_DateTime UA_DateTime_now_fake(UA_EventLoop *el) {
return testingClock;
}
2 changes: 1 addition & 1 deletion tests/testing-plugins/testing_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ void UA_fakeSleep(UA_UInt32 duration);
void UA_realSleep(UA_UInt32 duration);

/* To be hooked into the EventLoop for unit tests that need deterministic timings */
UA_DateTime UA_DateTime_nowMonotonic_fake(UA_EventLoop *el);
UA_DateTime UA_DateTime_now_fake(UA_EventLoop *el);

#endif /* TESTING_CLOCK_H_ */

0 comments on commit 054420c

Please sign in to comment.