check_server_asyncop.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <check.h>
  9. #include <time.h>
  10. #include <signal.h>
  11. #include <stdlib.h>
  12. #include <open62541/plugin/log_stdout.h>
  13. #include <open62541/server.h>
  14. #include "testing_clock.h"
  15. static UA_Server* globalServer;
  16. static UA_Session session;
  17. START_TEST(InternalTestingQueue) {
  18. globalServer->config.asyncOperationTimeout = 2;
  19. globalServer->config.maxAsyncOperationQueueSize = 5;
  20. UA_UInt32 reqId = 1;
  21. const UA_AsyncOperationRequest* pRequest = NULL;
  22. void *pContext = NULL;
  23. UA_AsyncOperationType type;
  24. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch from empty queue");
  25. bool rv = UA_Server_getAsyncOperation(globalServer, &type, &pRequest, &pContext);
  26. ck_assert_int_eq(rv, UA_FALSE);
  27. UA_CallMethodRequest* pRequest1 = UA_CallMethodRequest_new();
  28. UA_CallMethodRequest_init(pRequest1);
  29. UA_NodeId id = UA_NODEID_NUMERIC(1, 62540);
  30. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: create request queue entry");
  31. UA_StatusCode result = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &id, 0, pRequest1);
  32. ck_assert_int_eq(result, UA_STATUSCODE_GOOD);
  33. ck_assert_int_eq(globalServer->asyncMethodManager.nMQCurSize, 1);
  34. const UA_AsyncOperationRequest* pRequestWorker = NULL;
  35. void *pContextWorker = NULL;
  36. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch from queue");
  37. rv = UA_Server_getAsyncOperation(globalServer, &type, &pRequestWorker, &pContextWorker);
  38. ck_assert_int_eq(rv, UA_TRUE);
  39. ck_assert_int_eq(globalServer->asyncMethodManager.nMQCurSize, 0);
  40. UA_AsyncOperationResponse pResponse;
  41. UA_CallMethodResult_init(&pResponse.callMethodResult);
  42. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: set result to response queue");
  43. UA_Server_setAsyncOperationResult(globalServer, &pResponse, pContextWorker);
  44. UA_CallMethodResult_deleteMembers(&pResponse.callMethodResult);
  45. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: fetch result from response queue");
  46. struct AsyncMethodQueueElement* pResponseServer =
  47. UA_Server_GetAsyncMethodResult(&globalServer->asyncMethodManager);
  48. ck_assert_ptr_ne(pResponseServer, NULL);
  49. UA_Server_DeleteMethodQueueElement(globalServer, pResponseServer);
  50. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: testing queue limit (%zu)", globalServer->config.maxAsyncOperationQueueSize);
  51. UA_UInt32 i;
  52. for (i = 0; i < globalServer->config.maxAsyncOperationQueueSize + 1; i++) {
  53. UA_NodeId idTmp = UA_NODEID_NUMERIC(1, 62541);
  54. UA_StatusCode resultTmp = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &idTmp, 0, pRequest1);
  55. if (i < globalServer->config.maxAsyncOperationQueueSize)
  56. ck_assert_int_eq(resultTmp, UA_STATUSCODE_GOOD);
  57. else
  58. ck_assert_int_ne(resultTmp, UA_STATUSCODE_GOOD);
  59. }
  60. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: queue should not be empty");
  61. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  62. ck_assert_int_ne(globalServer->asyncMethodManager.nMQCurSize, 0);
  63. UA_fakeSleep((UA_Int32)(globalServer->config.asyncOperationTimeout + 1) * 1000);
  64. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: empty queue caused by queue timeout (%ds)", (UA_Int32)globalServer->config.asyncOperationTimeout);
  65. /* has to be done twice due to internal queue delete limit */
  66. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  67. UA_Server_CheckQueueIntegrity(globalServer,NULL);
  68. ck_assert_int_eq(globalServer->asyncMethodManager.nMQCurSize, 0);
  69. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking queue: adding one new entry to empty queue");
  70. result = UA_Server_SetNextAsyncMethod(globalServer, reqId++, &id, 0, pRequest1);
  71. ck_assert_int_eq(result, UA_STATUSCODE_GOOD);
  72. ck_assert_int_eq(globalServer->asyncMethodManager.nMQCurSize, 1);
  73. UA_CallMethodRequest_delete(pRequest1);
  74. }
  75. END_TEST
  76. START_TEST(InternalTestingManager) {
  77. UA_Session_init(&session);
  78. session.sessionId = UA_NODEID_NUMERIC(1, 62541);
  79. UA_SecureChannel channel;
  80. UA_SecureChannel_init(&channel);
  81. UA_LOG_INFO(&globalServer->config.logger, UA_LOGCATEGORY_SERVER, "* Checking UA_AsyncOperationManager_createEntry: create CallRequests");
  82. for (UA_Int32 i = 1; i < 7; i++) {
  83. UA_StatusCode result =
  84. UA_AsyncOperationManager_createEntry(&globalServer->asyncMethodManager, globalServer,
  85. &session.sessionId, channel.securityToken.channelId,
  86. i, i, UA_ASYNCOPERATIONTYPE_CALL, 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_AsyncOperationManager_createEntry: empty CallRequest list");
  91. UA_AsyncOperationManager_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->asyncMethodManager.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. }
  131. END_TEST
  132. static void setup(void) {
  133. globalServer = UA_Server_new();
  134. UA_ServerConfig *config = UA_Server_getConfig(globalServer);
  135. UA_ServerConfig_setDefault(config);
  136. }
  137. static void teardown(void) {
  138. UA_Server_delete(globalServer);
  139. }
  140. static Suite* method_async_suite(void) {
  141. /* set up unit test for internal data structures */
  142. Suite *s = suite_create("Async Method");
  143. /* UA_Server_PendingList */
  144. TCase* tc_pending = tcase_create("PendingList");
  145. tcase_add_checked_fixture(tc_pending, setup, NULL);
  146. tcase_add_test(tc_pending, InternalTestingPendingList);
  147. suite_add_tcase(s, tc_pending);
  148. /* UA_AsyncOperationManager */
  149. TCase* tc_manager = tcase_create("AsyncMethodManager");
  150. tcase_add_checked_fixture(tc_manager, NULL, NULL);
  151. tcase_add_test(tc_manager, InternalTestingManager);
  152. suite_add_tcase(s, tc_manager);
  153. /* UA_Server_MethodQueues */
  154. TCase* tc_queue = tcase_create("AsyncMethodQueue");
  155. tcase_add_checked_fixture(tc_queue, NULL, teardown);
  156. tcase_add_test(tc_queue, InternalTestingQueue);
  157. suite_add_tcase(s, tc_queue);
  158. return s;
  159. }
  160. int main(void) {
  161. /* Unit tests for internal data structures for async methods */
  162. int number_failed = 0;
  163. Suite *s = method_async_suite();
  164. SRunner *sr = srunner_create(s);
  165. srunner_set_fork_status(sr, CK_NOFORK);
  166. srunner_run_all(sr, CK_NORMAL);
  167. number_failed += srunner_ntests_failed(sr);
  168. srunner_free(sr);
  169. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  170. }