check_server_jobs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* The job is added to the main queue only upon the next run_iterate */
  31. UA_Server_addRepeatedJob(server, rj, 10, &id);
  32. UA_Server_run_iterate(server, false);
  33. /* Wait until the job has surely timed out */
  34. usleep(15*1000);
  35. UA_Server_run_iterate(server, false);
  36. /* Wait a bit longer until the workers have picked up the dispatched job */
  37. usleep(15*1000);
  38. ck_assert_uint_eq(*executed, true);
  39. UA_Server_removeRepeatedJob(server, id);
  40. UA_Boolean_delete(executed);
  41. }
  42. END_TEST
  43. UA_Guid *jobId;
  44. static void
  45. removeItselfJob(UA_Server *serverPtr, void *data) {
  46. UA_Server_removeRepeatedJob(serverPtr, *jobId);
  47. }
  48. START_TEST(Server_repeatedJobRemoveItself) {
  49. jobId = UA_Guid_new();
  50. UA_Job rj = (UA_Job){
  51. .type = UA_JOBTYPE_METHODCALL,
  52. .job.methodCall = {.data = NULL, .method = removeItselfJob}
  53. };
  54. UA_Server_addRepeatedJob(server, rj, 10, jobId);
  55. usleep(15*1000);
  56. UA_Server_run_iterate(server, false);
  57. UA_Guid_delete(jobId);
  58. }
  59. END_TEST
  60. static Suite* testSuite_Client(void) {
  61. Suite *s = suite_create("Server Jobs");
  62. TCase *tc_server = tcase_create("Server Repeated Jobs");
  63. tcase_add_checked_fixture(tc_server, setup, teardown);
  64. tcase_add_test(tc_server, Server_addRemoveRepeatedJob);
  65. tcase_add_test(tc_server, Server_repeatedJobRemoveItself);
  66. suite_add_tcase(s, tc_server);
  67. return s;
  68. }
  69. int main(void) {
  70. Suite *s = testSuite_Client();
  71. SRunner *sr = srunner_create(s);
  72. srunner_set_fork_status(sr, CK_NOFORK);
  73. srunner_run_all(sr,CK_NORMAL);
  74. int number_failed = srunner_ntests_failed(sr);
  75. srunner_free(sr);
  76. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  77. }