Skip to content

Commit

Permalink
fix(tests): Fix use of deterministic clock in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Dec 6, 2023
1 parent 1884112 commit 437bd44
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
3 changes: 2 additions & 1 deletion tests/pubsub/check_pubsub_subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,8 @@ START_TEST(SinglePublishSubscribeHeartbeat) {
* we compare the lastHeartbeatReceived with the static timestamp
* (given by UA_DateTime_nowMonotonic()). If the timestamps are equal,
* the code path was executed and the lastHeartbeatReceived set correctly */
ck_assert(UA_DateTime_nowMonotonic() == dsr->lastHeartbeatReceived);
UA_EventLoop *el = server->config.eventLoop;
ck_assert(el->dateTime_nowMonotonic(el) == dsr->lastHeartbeatReceived);

} END_TEST

Expand Down
62 changes: 35 additions & 27 deletions tests/server/check_server_historical_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ static void teardown(void) {

static UA_StatusCode
setUInt32(UA_Client *thisClient, UA_NodeId node, UA_UInt32 value) {
UA_EventLoop *el = UA_Client_getConfig(thisClient)->eventLoop;
UA_DataValue dv;
UA_DataValue_init(&dv);
UA_Variant_setScalar(&dv.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
dv.hasValue = true;
dv.sourceTimestamp = UA_DateTime_now();
dv.sourceTimestamp = el->dateTime_now(el);
dv.hasSourceTimestamp = true;
return UA_Client_writeValueAttributeEx(thisClient, node, &dv);
}
Expand Down Expand Up @@ -713,8 +714,7 @@ START_TEST(Server_HistorizingUpdateUpdate)
}
END_TEST

START_TEST(Server_HistorizingStrategyUser)
{
START_TEST(Server_HistorizingStrategyUser) {
// set a data backend
UA_HistorizingNodeIdSettings setting;
setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
Expand All @@ -724,7 +724,8 @@ START_TEST(Server_HistorizingStrategyUser)
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));

// fill the data
UA_DateTime start = UA_DateTime_now();
UA_EventLoop *el = UA_Client_getConfig(client)->eventLoop;
UA_DateTime start = el->dateTime_now(el);
UA_DateTime end = start + (10 * UA_DATETIME_SEC);
for (UA_UInt32 i = 0; i < 10; ++i) {
UA_DataValue value;
Expand Down Expand Up @@ -779,8 +780,10 @@ START_TEST(Server_HistorizingStrategyUser)
}
END_TEST

START_TEST(Server_HistorizingStrategyPoll)
{
START_TEST(Server_HistorizingStrategyPoll) {
UA_EventLoop *el = UA_Client_getConfig(client)->eventLoop;
UA_DateTime start = el->dateTime_now(el);

// init to a defined value
UA_StatusCode retval = setUInt32(client, outNodeId, 43);
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
Expand All @@ -795,47 +798,52 @@ START_TEST(Server_HistorizingStrategyPoll)
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));

// fill the data
UA_DateTime start = UA_DateTime_now();
retval = gathering->startPoll(server, gathering->context, &outNodeId);
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);

ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
for (size_t k = 0; k < 10; ++k) {
UA_fakeSleep(50);
UA_realSleep(50);
if (k == 5) {
for(size_t k = 0; k < 10; ++k) {
running = false;
THREAD_JOIN(server_thread);
UA_fakeSleep(setting.pollingInterval);
UA_Server_run_iterate(server, false);
running = true;
THREAD_CREATE(server_thread, serverloop);
if(k == 5) {
gathering->stopPoll(server, gathering->context, &outNodeId);
}
setUInt32(client, outNodeId, (unsigned int)k);
}

ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
UA_DateTime end = UA_DateTime_now();
UA_DateTime end = el->dateTime_now(el);

// request
UA_HistoryReadResponse response;
UA_HistoryReadResponse_init(&response);
requestHistory(start, end, &response, 0, false, NULL);

// test the response
ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
ck_assert_int_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(response.resultsSize, 1);
for (size_t i = 0; i < response.resultsSize; ++i) {
ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
for(size_t i = 0; i < response.resultsSize; ++i) {
ck_assert_int_eq(response.results[i].statusCode, UA_STATUSCODE_GOOD);
ck_assert_uint_eq(response.results[i].historyData.encoding,
UA_EXTENSIONOBJECT_DECODED);
ck_assert_ptr_eq(response.results[i].historyData.content.decoded.type,
&UA_TYPES[UA_TYPES_HISTORYDATA]);
UA_HistoryData *data = (UA_HistoryData *)
response.results[i].historyData.content.decoded.data;
ck_assert(data->dataValuesSize > 1);
for (size_t j = 0; j < data->dataValuesSize; ++j) {
for(size_t j = 0; j < data->dataValuesSize; ++j) {
ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
ck_assert(data->dataValues[j].sourceTimestamp >= start);
ck_assert(data->dataValues[j].sourceTimestamp < end);
ck_assert_uint_eq(data->dataValues[j].hasValue, true);
ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
// first need to be 43
if (j == 0) {
ck_assert(*value == 43);
if(j == 0) {
ck_assert_uint_eq(*value, 43);
} else {
ck_assert(*value < 5);
}
Expand All @@ -846,8 +854,7 @@ START_TEST(Server_HistorizingStrategyPoll)
}
END_TEST

START_TEST(Server_HistorizingStrategyValueSet)
{
START_TEST(Server_HistorizingStrategyValueSet) {
// init to a defined value
UA_StatusCode retval = setUInt32(client, outNodeId, 43);
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
Expand All @@ -862,14 +869,15 @@ START_TEST(Server_HistorizingStrategyValueSet)

// fill the data
UA_fakeSleep(100);
UA_DateTime start = UA_DateTime_now();
UA_EventLoop *el = UA_Client_getConfig(client)->eventLoop;
UA_DateTime start = el->dateTime_now(el);
UA_fakeSleep(100);
for (UA_UInt32 i = 0; i < 10; ++i) {
retval = setUInt32(client, outNodeId, i);
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
UA_fakeSleep(100);
}
UA_DateTime end = UA_DateTime_now();
UA_DateTime end = el->dateTime_now(el);

// request
UA_HistoryReadResponse response;
Expand Down
10 changes: 3 additions & 7 deletions tests/server/check_server_reverseconnect.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static void setup(void) {
numClientCallbackCalled = 0;

server = UA_Server_newForUnitTest();
UA_ServerConfig *sc = UA_Server_getConfig(server);
sc->reverseReconnectInterval = 15000;
ck_assert(server != NULL);

client = UA_Client_newForUnitTest();
Expand Down Expand Up @@ -281,21 +283,15 @@ START_TEST(checkReconnect) {

UA_fakeSleep(UA_Server_getConfig(server)->reverseReconnectInterval + 1000);

UA_EventLoop *serverLoop = UA_Server_getConfig(server)->eventLoop;
UA_DateTime next = serverLoop->run(serverLoop, 1);
UA_fakeSleep((UA_UInt32)((next - UA_DateTime_now()) / UA_DATETIME_MSEC));

for(int j = 0; j < 5; ++j) {
UA_Server_run_iterate(server, true);
UA_Client_run_iterate(client, 1);
next = serverLoop->run(serverLoop, 1);
UA_fakeSleep((UA_UInt32)((next - UA_DateTime_now()) / UA_DATETIME_MSEC));
}

ck_assert_int_eq(numServerCallbackCalled, 9);
ck_assert_int_eq(serverCallbackStates[6], UA_SECURECHANNELSTATE_RHE_SENT);
ck_assert_int_eq(serverCallbackStates[7], UA_SECURECHANNELSTATE_ACK_SENT);
ck_assert_int_eq(serverCallbackStates[8], UA_SECURECHANNELSTATE_OPEN);

}

if(i == 80)
Expand Down

0 comments on commit 437bd44

Please sign in to comment.