check_server_jobs.c 2.5 KB

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