check_server_jobs.c 2.5 KB

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