check_server_jobs.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <open62541/server_config_default.h>
  5. #include "server/ua_server_internal.h"
  6. #include <check.h>
  7. #include "testing_clock.h"
  8. UA_Server *server = NULL;
  9. UA_Boolean *executed;
  10. static void setup(void) {
  11. server = UA_Server_new();
  12. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  13. UA_Server_run_startup(server);
  14. }
  15. static void teardown(void) {
  16. UA_Server_run_shutdown(server);
  17. UA_Server_delete(server);
  18. }
  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_fakeSleep(15);
  30. UA_Server_run_iterate(server, false);
  31. /* Wait a bit longer until the workers have picked up the dispatched callback */
  32. UA_realSleep(100);
  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_fakeSleep(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. }