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