check_server_jobs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "ua_server.h"
  2. #include "server/ua_server_internal.h"
  3. #include "ua_config_standard.h"
  4. #include "check.h"
  5. #include <unistd.h>
  6. UA_Server *server = NULL;
  7. static void setup(void) {
  8. server = UA_Server_new(UA_ServerConfig_standard);
  9. UA_Server_run_startup(server);
  10. }
  11. static void teardown(void) {
  12. UA_Server_run_shutdown(server);
  13. UA_Server_delete(server);
  14. }
  15. UA_Boolean *executed;
  16. static void
  17. dummyJob(UA_Server *serverPtr, void *data) {
  18. *executed = true;
  19. }
  20. START_TEST(Server_addRemoveRepeatedJob) {
  21. executed = UA_Boolean_new();
  22. UA_Guid id;
  23. UA_Job rj = (UA_Job){
  24. .type = UA_JOBTYPE_METHODCALL,
  25. .job.methodCall = {.data = NULL, .method = dummyJob}
  26. };
  27. UA_Server_addRepeatedJob(server, rj, 10, &id);
  28. usleep(15*1000);
  29. UA_Server_run_iterate(server, false);
  30. ck_assert_uint_eq(*executed, true);
  31. UA_Server_removeRepeatedJob(server, id);
  32. UA_Boolean_delete(executed);
  33. }
  34. END_TEST
  35. UA_Guid *jobId;
  36. static void
  37. removeItselfJob(UA_Server *serverPtr, void *data) {
  38. UA_Server_removeRepeatedJob(serverPtr, *jobId);
  39. }
  40. START_TEST(Server_repeatedJobRemoveItself) {
  41. jobId = UA_Guid_new();
  42. UA_Job rj = (UA_Job){
  43. .type = UA_JOBTYPE_METHODCALL,
  44. .job.methodCall = {.data = NULL, .method = removeItselfJob}
  45. };
  46. UA_Server_addRepeatedJob(server, rj, 10, jobId);
  47. usleep(15*1000);
  48. UA_Server_run_iterate(server, false);
  49. UA_Guid_delete(jobId);
  50. }
  51. END_TEST
  52. static Suite* testSuite_Client(void) {
  53. Suite *s = suite_create("Server Jobs");
  54. TCase *tc_server = tcase_create("Server Repeated Jobs");
  55. tcase_add_checked_fixture(tc_server, setup, teardown);
  56. tcase_add_test(tc_server, Server_addRemoveRepeatedJob);
  57. tcase_add_test(tc_server, Server_repeatedJobRemoveItself);
  58. suite_add_tcase(s, tc_server);
  59. return s;
  60. }
  61. int main(void) {
  62. Suite *s = testSuite_Client();
  63. SRunner *sr = srunner_create(s);
  64. srunner_set_fork_status(sr, CK_NOFORK);
  65. srunner_run_all(sr,CK_NORMAL);
  66. int number_failed = srunner_ntests_failed(sr);
  67. srunner_free(sr);
  68. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  69. }