check_server_asyncop.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /* This example is just to see how fast we can process messages. The server does
  4. not open a TCP port. */
  5. #include <open62541/server_config_default.h>
  6. #include "server/ua_services.h"
  7. #include "ua_server_internal.h"
  8. #include "ua_server_methodqueue.h"
  9. #include <check.h>
  10. #include <time.h>
  11. #include <signal.h>
  12. #include <stdlib.h>
  13. #include <open62541/plugin/log_stdout.h>
  14. #include <open62541/server.h>
  15. #include "testing_clock.h"
  16. static UA_Server* globalServer;
  17. static UA_Session session;
  18. START_TEST(InternalTestingQueue) {
  19. globalServer->config.asyncOperationTimeout = 2;
  20. globalServer->config.maxAsyncOperationQueueSize = 5;
  21. UA_UInt32 reqId = 1;
  22. const UA_AsyncOperationRequest* pRequest = NULL;
  23. void *pContext = NULL;
  24. UA_AsyncOperationType type;
  25. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch from empty queue");
  26. bool rv = UA_Server_getAsyncOperation(globalServer, &type, &pRequest, &pContext);
  27. ck_assert_int_eq(rv, UA_FALSE);
  28. UA_CallMethodRequest* pRequest1 = UA_CallMethodRequest_new();
  29. UA_CallMethodRequest_init(pRequest1);
  30. UA_NodeId id = UA_NODEID_NUMERIC(1, 62540);
  31. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: create request queue entry");
  32. UA_StatusCode result = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &id, 0, pRequest1);
  33. ck_assert_int_eq(result, UA_STATUSCODE_GOOD);
  34. ck_assert_int_eq(globalServer->nMQCurSize, 1);
  35. const UA_AsyncOperationRequest* pRequestWorker = NULL;
  36. void *pContextWorker = NULL;
  37. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch from queue");
  38. rv = UA_Server_getAsyncOperation(globalServer, &type, &pRequestWorker, &pContextWorker);
  39. ck_assert_int_eq(rv, UA_TRUE);
  40. ck_assert_int_eq(globalServer->nMQCurSize, 0);
  41. UA_AsyncOperationResponse pResponse;
  42. UA_CallMethodResult_init(&pResponse.callMethodResult);
  43. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: set result to response queue");
  44. UA_Server_setAsyncOperationResult(globalServer, &pResponse, pContextWorker);
  45. UA_CallMethodResult_deleteMembers(&pResponse.callMethodResult);
  46. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch result from response queue");
  47. struct AsyncMethodQueueElement* pResponseServer = NULL;
  48. rv = UA_Server_GetAsyncMethodResult(globalServer, &pResponseServer);
  49. ck_assert_int_eq(rv, UA_TRUE);
  50. UA_Server_DeleteMethodQueueElement(globalServer, pResponseServer);
  51. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: testing queue limit (%zu)", globalServer->config.maxAsyncOperationQueueSize);
  52. UA_UInt32 i;
  53. for (i = 0; i < globalServer->config.maxAsyncOperationQueueSize + 1; i++) {
  54. UA_NodeId idTmp = UA_NODEID_NUMERIC(1, 62541);
  55. UA_StatusCode resultTmp = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &idTmp, 0, pRequest1);
  56. if (i < globalServer->config.maxAsyncOperationQueueSize)
  57. ck_assert_int_eq(resultTmp, UA_STATUSCODE_GOOD);
  58. else
  59. ck_assert_int_ne(resultTmp, UA_STATUSCODE_GOOD);
  60. }
  61. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: queue should not be empty");
  62. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  63. ck_assert_int_ne(globalServer->nMQCurSize, 0);
  64. UA_fakeSleep((UA_Int32)(globalServer->config.asyncOperationTimeout + 1) * 1000);
  65. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: empty queue caused by queue timeout (%ds)", (UA_Int32)globalServer->config.asyncOperationTimeout);
  66. /* has to be done twice due to internal queue delete limit */
  67. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  68. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  69. ck_assert_int_eq(globalServer->nMQCurSize, 0);
  70. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: adding one new entry to empty queue");
  71. result = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &id, 0, pRequest1);
  72. ck_assert_int_eq(result, UA_STATUSCODE_GOOD);
  73. ck_assert_int_eq(globalServer->nMQCurSize, 1);
  74. UA_CallMethodRequest_delete(pRequest1);
  75. }
  76. END_TEST
  77. START_TEST(InternalTestingManager) {
  78. UA_Session_init(&session);
  79. session.sessionId = UA_NODEID_NUMERIC(1, 62541);
  80. UA_SecureChannel channel;
  81. UA_SecureChannel_init(&channel);
  82. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking UA_AsyncMethodManager_createEntry: create CallRequests");
  83. UA_DataType dataType = UA_TYPES[UA_TYPES_CALLMETHODREQUEST];
  84. for (UA_Int32 i = 1; i < 7; i++) {
  85. UA_StatusCode result = UA_AsyncMethodManager_createEntry(&globalServer->asyncMethodManager, &session.sessionId,
  86. channel.securityToken.channelId, i, i, &dataType, 1);
  87. ck_assert_int_eq(result, UA_STATUSCODE_GOOD);
  88. }
  89. UA_fakeSleep(121000);
  90. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking UA_AsyncMethodManager_createEntry: empty CallRequest list");
  91. UA_AsyncMethodManager_checkTimeouts(globalServer, &globalServer->asyncMethodManager);
  92. ck_assert_int_eq(globalServer->asyncMethodManager.currentCount, 0);
  93. }
  94. END_TEST
  95. START_TEST(InternalTestingPendingList) {
  96. globalServer->config.asyncOperationTimeout = 2;
  97. globalServer->config.maxAsyncOperationQueueSize = 5;
  98. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList");
  99. struct AsyncMethodQueueElement* elem1 = (struct AsyncMethodQueueElement*)UA_calloc(1, sizeof(struct AsyncMethodQueueElement));
  100. struct AsyncMethodQueueElement* elem2 = (struct AsyncMethodQueueElement*)UA_calloc(1, sizeof(struct AsyncMethodQueueElement));
  101. struct AsyncMethodQueueElement* elem3 = (struct AsyncMethodQueueElement*)UA_calloc(1, sizeof(struct AsyncMethodQueueElement));
  102. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: Adding 3 elements");
  103. UA_Server_AddPendingMethodCall(globalServer, elem1);
  104. UA_Server_AddPendingMethodCall(globalServer, elem2);
  105. UA_Server_AddPendingMethodCall(globalServer, elem3);
  106. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: check if element is found");
  107. UA_Boolean bFound = UA_Server_IsPendingMethodCall(globalServer, elem2);
  108. if (!bFound) {
  109. ck_assert_int_eq(bFound, UA_TRUE);
  110. UA_Server_RmvPendingMethodCall(globalServer, elem2);
  111. }
  112. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: remove remaining elements");
  113. UA_Server_RmvPendingMethodCall(globalServer, elem1);
  114. UA_Server_RmvPendingMethodCall(globalServer, elem3);
  115. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: check if removed element is NOT found");
  116. bFound = UA_Server_IsPendingMethodCall(globalServer, elem1);
  117. if (!bFound) {
  118. ck_assert_int_eq(bFound, UA_FALSE);
  119. UA_Server_RmvPendingMethodCall(globalServer, elem1);
  120. }
  121. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: queue integrity");
  122. UA_Server_AddPendingMethodCall(globalServer, elem1);
  123. UA_Server_AddPendingMethodCall(globalServer, elem2);
  124. UA_Server_AddPendingMethodCall(globalServer, elem3);
  125. UA_fakeSleep((UA_Int32)(globalServer->config.asyncOperationTimeout + 1) * 1000);
  126. UA_Server_CheckQueueIntegrity(globalServer, NULL);
  127. ck_assert_ptr_eq(globalServer->ua_method_pending_list.sqh_first,NULL);
  128. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking PendingList: global removal/delete");
  129. UA_Server_AddPendingMethodCall(globalServer, elem1);
  130. /* remove all entries and delete queues */
  131. UA_Server_MethodQueues_delete(globalServer);
  132. /* re-init queues */
  133. UA_Server_MethodQueues_init(globalServer);
  134. }
  135. END_TEST
  136. static void setup(void) {
  137. globalServer = UA_Server_new();
  138. UA_ServerConfig *config = UA_Server_getConfig(globalServer);
  139. UA_ServerConfig_setDefault(config);
  140. }
  141. static void teardown(void) {
  142. UA_Server_delete(globalServer);
  143. }
  144. static Suite* method_async_suite(void) {
  145. /* set up unit test for internal data structures */
  146. Suite *s = suite_create("Async Method");
  147. /* UA_Server_PendingList */
  148. TCase* tc_pending = tcase_create("PendingList");
  149. tcase_add_checked_fixture(tc_pending, setup, NULL);
  150. tcase_add_test(tc_pending, InternalTestingPendingList);
  151. suite_add_tcase(s, tc_pending);
  152. /* UA_AsyncMethodManager */
  153. TCase* tc_manager = tcase_create("AsyncMethodManager");
  154. tcase_add_checked_fixture(tc_manager, NULL, NULL);
  155. tcase_add_test(tc_manager, InternalTestingManager);
  156. suite_add_tcase(s, tc_manager);
  157. /* UA_Server_MethodQueues */
  158. TCase* tc_queue = tcase_create("AsyncMethodQueue");
  159. tcase_add_checked_fixture(tc_queue, NULL, teardown);
  160. tcase_add_test(tc_queue, InternalTestingQueue);
  161. suite_add_tcase(s, tc_queue);
  162. return s;
  163. }
  164. int main(void) {
  165. /* Unit tests for internal data structures for async methods */
  166. int number_failed = 0;
  167. Suite *s = method_async_suite();
  168. SRunner *sr = srunner_create(s);
  169. srunner_set_fork_status(sr, CK_NOFORK);
  170. srunner_run_all(sr, CK_NORMAL);
  171. number_failed += srunner_ntests_failed(sr);
  172. srunner_free(sr);
  173. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  174. }