check_services_subscriptions.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.*/
  4. #include "ua_server.h"
  5. #include "server/ua_services.h"
  6. #include "server/ua_server_internal.h"
  7. #include "server/ua_subscription.h"
  8. #include "ua_config_standard.h"
  9. #include "check.h"
  10. #include <unistd.h>
  11. UA_Server *server = NULL;
  12. static void setup(void) {
  13. server = UA_Server_new(UA_ServerConfig_standard);
  14. UA_Server_run_startup(server);
  15. }
  16. static void teardown(void) {
  17. UA_Server_run_shutdown(server);
  18. UA_Server_delete(server);
  19. }
  20. START_TEST(Server_createSubscription) {
  21. /* Create a subscription */
  22. UA_CreateSubscriptionRequest request;
  23. UA_CreateSubscriptionRequest_init(&request);
  24. request.publishingEnabled = true;
  25. UA_CreateSubscriptionResponse response;
  26. UA_CreateSubscriptionResponse_init(&response);
  27. Service_CreateSubscription(server, &adminSession, &request, &response);
  28. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  29. UA_UInt32 subscriptionId = response.subscriptionId;
  30. UA_CreateSubscriptionResponse_deleteMembers(&response);
  31. /* Remove the subscription */
  32. UA_DeleteSubscriptionsRequest del_request;
  33. UA_DeleteSubscriptionsRequest_init(&del_request);
  34. del_request.subscriptionIdsSize = 1;
  35. del_request.subscriptionIds = &subscriptionId;
  36. UA_DeleteSubscriptionsResponse del_response;
  37. UA_DeleteSubscriptionsResponse_init(&del_response);
  38. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  39. ck_assert_uint_eq(del_response.resultsSize, 1);
  40. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  41. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  42. }
  43. END_TEST
  44. START_TEST(Server_publishCallback) {
  45. /* Create a subscription */
  46. UA_CreateSubscriptionRequest request;
  47. UA_CreateSubscriptionResponse response;
  48. UA_CreateSubscriptionRequest_init(&request);
  49. request.publishingEnabled = true;
  50. UA_CreateSubscriptionResponse_init(&response);
  51. Service_CreateSubscription(server, &adminSession, &request, &response);
  52. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  53. UA_UInt32 subscriptionId1 = response.subscriptionId;
  54. UA_CreateSubscriptionResponse_deleteMembers(&response);
  55. /* Create a second subscription */
  56. UA_CreateSubscriptionRequest_init(&request);
  57. request.publishingEnabled = true;
  58. UA_CreateSubscriptionResponse_init(&response);
  59. Service_CreateSubscription(server, &adminSession, &request, &response);
  60. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  61. UA_UInt32 subscriptionId2 = response.subscriptionId;
  62. UA_Double publishingInterval = response.revisedPublishingInterval;
  63. ck_assert(publishingInterval > 0.0f);
  64. UA_CreateSubscriptionResponse_deleteMembers(&response);
  65. /* Sleep until the publishing interval times out */
  66. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  67. /* Keepalive is set to max initially */
  68. UA_Subscription *sub;
  69. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  70. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount);
  71. UA_Server_run_iterate(server, false);
  72. #ifdef UA_ENABLE_MULTITHREADING
  73. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  74. UA_Server_run_iterate(server, false);
  75. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  76. #endif
  77. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  78. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);
  79. /* Remove the subscriptions */
  80. UA_DeleteSubscriptionsRequest del_request;
  81. UA_DeleteSubscriptionsRequest_init(&del_request);
  82. UA_UInt32 removeIds[2] = {subscriptionId1, subscriptionId2};
  83. del_request.subscriptionIdsSize = 2;
  84. del_request.subscriptionIds = removeIds;
  85. UA_DeleteSubscriptionsResponse del_response;
  86. UA_DeleteSubscriptionsResponse_init(&del_response);
  87. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  88. ck_assert_uint_eq(del_response.resultsSize, 2);
  89. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  90. ck_assert_uint_eq(del_response.results[1], UA_STATUSCODE_GOOD);
  91. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  92. }
  93. END_TEST
  94. static Suite* testSuite_Client(void) {
  95. Suite *s = suite_create("Server Subscription");
  96. TCase *tc_server = tcase_create("Server Subscription Basic");
  97. tcase_add_checked_fixture(tc_server, setup, teardown);
  98. tcase_add_test(tc_server, Server_createSubscription);
  99. tcase_add_test(tc_server, Server_publishCallback);
  100. suite_add_tcase(s, tc_server);
  101. return s;
  102. }
  103. int main(void) {
  104. Suite *s = testSuite_Client();
  105. SRunner *sr = srunner_create(s);
  106. srunner_set_fork_status(sr, CK_NOFORK);
  107. srunner_run_all(sr,CK_NORMAL);
  108. int number_failed = srunner_ntests_failed(sr);
  109. srunner_free(sr);
  110. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  111. }