check_server_jobs.c 2.3 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_standard.h"
  7. #include "check.h"
  8. #include <unistd.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. dummyJob(UA_Server *serverPtr, void *data) {
  21. *executed = true;
  22. }
  23. START_TEST(Server_addRemoveRepeatedJob) {
  24. executed = UA_Boolean_new();
  25. UA_Guid id;
  26. UA_Job rj = (UA_Job){
  27. .type = UA_JOBTYPE_METHODCALL,
  28. .job.methodCall = {.data = NULL, .method = dummyJob}
  29. };
  30. UA_Server_addRepeatedJob(server, rj, 10, &id);
  31. usleep(15*1000);
  32. UA_Server_run_iterate(server, false);
  33. ck_assert_uint_eq(*executed, true);
  34. UA_Server_removeRepeatedJob(server, id);
  35. UA_Boolean_delete(executed);
  36. }
  37. END_TEST
  38. UA_Guid *jobId;
  39. static void
  40. removeItselfJob(UA_Server *serverPtr, void *data) {
  41. UA_Server_removeRepeatedJob(serverPtr, *jobId);
  42. }
  43. START_TEST(Server_repeatedJobRemoveItself) {
  44. jobId = UA_Guid_new();
  45. UA_Job rj = (UA_Job){
  46. .type = UA_JOBTYPE_METHODCALL,
  47. .job.methodCall = {.data = NULL, .method = removeItselfJob}
  48. };
  49. UA_Server_addRepeatedJob(server, rj, 10, jobId);
  50. usleep(15*1000);
  51. UA_Server_run_iterate(server, false);
  52. UA_Guid_delete(jobId);
  53. }
  54. END_TEST
  55. static Suite* testSuite_Client(void) {
  56. Suite *s = suite_create("Server Jobs");
  57. TCase *tc_server = tcase_create("Server Repeated Jobs");
  58. tcase_add_checked_fixture(tc_server, setup, teardown);
  59. tcase_add_test(tc_server, Server_addRemoveRepeatedJob);
  60. tcase_add_test(tc_server, Server_repeatedJobRemoveItself);
  61. suite_add_tcase(s, tc_server);
  62. return s;
  63. }
  64. int main(void) {
  65. Suite *s = testSuite_Client();
  66. SRunner *sr = srunner_create(s);
  67. srunner_set_fork_status(sr, CK_NOFORK);
  68. srunner_run_all(sr,CK_NORMAL);
  69. int number_failed = srunner_ntests_failed(sr);
  70. srunner_free(sr);
  71. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  72. }