check_services_subscriptions.c 4.6 KB

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