Skip to content

Commit

Permalink
Tests: Mutex access to server to avoid race condition.
Browse files Browse the repository at this point in the history
Fixes #2330
  • Loading branch information
Pro committed Jan 3, 2019
1 parent 7ec0a2d commit 74ee393
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/server/check_historical_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,35 @@ static UA_HistoryDataGathering *gathering;
#endif
static UA_Boolean running;
static THREAD_HANDLE server_thread;
static MUTEX_HANDLE serverMutex;

static UA_Client *client;
static UA_NodeId parentNodeId;
static UA_NodeId parentReferenceNodeId;
static UA_NodeId outNodeId;

static void serverMutexLock(void) {
ck_assert_uint_eq(MUTEX_LOCK(serverMutex), true);
}

static void serverMutexUnlock(void) {
ck_assert_uint_eq(MUTEX_UNLOCK(serverMutex), true);
}

THREAD_CALLBACK(serverloop)
{
while(running)
while(running) {
serverMutexLock();
UA_Server_run_iterate(server, true);
serverMutexUnlock();
}
return 0;
}

static void
setup(void)
{
ck_assert_uint_eq(MUTEX_INIT(serverMutex), true);
running = true;
config = UA_ServerConfig_new_default();
#ifdef UA_ENABLE_HISTORIZING
Expand Down Expand Up @@ -115,6 +128,9 @@ teardown(void)
#ifdef UA_ENABLE_HISTORIZING
UA_free(gathering);
#endif
if (!MUTEX_DESTROY(serverMutex)) {
fprintf(stderr, "Server mutex was not destroyed correctly.");
}
}

#ifdef UA_ENABLE_HISTORIZING
Expand Down Expand Up @@ -491,20 +507,26 @@ START_TEST(Server_HistorizingStrategyPoll)
setting.maxHistoryDataResponseSize = 100;
setting.pollingInterval = 100;
setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_POLL;
serverMutexLock();
retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
serverMutexUnlock();
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));

// fill the data
UA_DateTime start = UA_DateTime_now();
serverMutexLock();
retval = gathering->startPoll(server, gathering->context, &outNodeId);
serverMutexUnlock();
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(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) {
serverMutexLock();
gathering->stopPoll(server, gathering->context, &outNodeId);
serverMutexUnlock();
}
setUInt32(client, outNodeId, (unsigned int)k);
}
Expand Down Expand Up @@ -557,7 +579,9 @@ START_TEST(Server_HistorizingStrategyValueSet)
setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
setting.maxHistoryDataResponseSize = 100;
setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_VALUESET;
serverMutexLock();
retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
serverMutexUnlock();
ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));

// fill the data
Expand Down Expand Up @@ -607,7 +631,9 @@ START_TEST(Server_HistorizingBackendMemory)
setting.historizingBackend = backend;
setting.maxHistoryDataResponseSize = 1000;
setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
serverMutexLock();
UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
serverMutexUnlock();
ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));

// empty backend should not crash
Expand Down

0 comments on commit 74ee393

Please sign in to comment.