check_server_jobs.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_server_internal.h"
  6. #include "ua_config_standard.h"
  7. #include "check.h"
  8. #include "testing_clock.h"
  9. UA_Server *server = NULL;
  10. static void setup(void) {
  11. server = UA_Server_new(UA_ServerConfig_standard);
  12. UA_Server_run_startup(server);
  13. }
  14. static void teardown(void) {
  15. UA_Server_run_shutdown(server);
  16. UA_Server_delete(server);
  17. }
  18. UA_Boolean *executed;
  19. static void
  20. dummyCallback(UA_Server *serverPtr, void *data) {
  21. *executed = true;
  22. }
  23. START_TEST(Server_addRemoveRepeatedCallback) {
  24. executed = UA_Boolean_new();
  25. /* The callback is added to the main queue only upon the next run_iterate */
  26. UA_UInt64 id;
  27. UA_Server_addRepeatedCallback(server, dummyCallback, NULL, 10, &id);
  28. /* Wait until the callback has surely timed out */
  29. UA_sleep(15);
  30. UA_Server_run_iterate(server, false);
  31. /* Wait a bit longer until the workers have picked up the dispatched callback */
  32. UA_sleep(15);
  33. ck_assert_uint_eq(*executed, true);
  34. UA_Server_removeRepeatedCallback(server, id);
  35. UA_Boolean_delete(executed);
  36. }
  37. END_TEST
  38. UA_UInt64 *cbId;
  39. static void
  40. removeItselfCallback(UA_Server *serverPtr, void *data) {
  41. UA_Server_removeRepeatedCallback(serverPtr, *cbId);
  42. }
  43. START_TEST(Server_repeatedCallbackRemoveItself) {
  44. cbId = UA_UInt64_new();
  45. UA_Server_addRepeatedCallback(server, removeItselfCallback, NULL, 10, cbId);
  46. UA_sleep(15);
  47. UA_Server_run_iterate(server, false);
  48. UA_UInt64_delete(cbId);
  49. }
  50. END_TEST
  51. static Suite* testSuite_Client(void) {
  52. Suite *s = suite_create("Server Callbacks");
  53. TCase *tc_server = tcase_create("Server Repeated Callbacks");
  54. tcase_add_checked_fixture(tc_server, setup, teardown);
  55. tcase_add_test(tc_server, Server_addRemoveRepeatedCallback);
  56. tcase_add_test(tc_server, Server_repeatedCallbackRemoveItself);
  57. suite_add_tcase(s, tc_server);
  58. return s;
  59. }
  60. int main(void) {
  61. Suite *s = testSuite_Client();
  62. SRunner *sr = srunner_create(s);
  63. srunner_set_fork_status(sr, CK_NOFORK);
  64. srunner_run_all(sr,CK_NORMAL);
  65. int number_failed = srunner_ntests_failed(sr);
  66. srunner_free(sr);
  67. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  68. }