check_services_subscriptions.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  73. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);
  74. /* Remove the subscriptions */
  75. UA_DeleteSubscriptionsRequest del_request;
  76. UA_DeleteSubscriptionsRequest_init(&del_request);
  77. UA_UInt32 removeIds[2] = {subscriptionId1, subscriptionId2};
  78. del_request.subscriptionIdsSize = 2;
  79. del_request.subscriptionIds = removeIds;
  80. UA_DeleteSubscriptionsResponse del_response;
  81. UA_DeleteSubscriptionsResponse_init(&del_response);
  82. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  83. ck_assert_uint_eq(del_response.resultsSize, 2);
  84. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  85. ck_assert_uint_eq(del_response.results[1], UA_STATUSCODE_GOOD);
  86. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  87. }
  88. END_TEST
  89. static Suite* testSuite_Client(void) {
  90. Suite *s = suite_create("Server Subscription");
  91. TCase *tc_server = tcase_create("Server Subscription Basic");
  92. tcase_add_checked_fixture(tc_server, setup, teardown);
  93. tcase_add_test(tc_server, Server_createSubscription);
  94. tcase_add_test(tc_server, Server_publishCallback);
  95. suite_add_tcase(s, tc_server);
  96. return s;
  97. }
  98. int main(void) {
  99. Suite *s = testSuite_Client();
  100. SRunner *sr = srunner_create(s);
  101. srunner_set_fork_status(sr, CK_NOFORK);
  102. srunner_run_all(sr,CK_NORMAL);
  103. int number_failed = srunner_ntests_failed(sr);
  104. srunner_free(sr);
  105. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  106. }