execute.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include <libtest/test.h>
  38. #include <cassert>
  39. #include <cstring>
  40. #include <libgearman/gearman.h>
  41. #include <string>
  42. #include <tests/execute.h>
  43. #ifndef __INTEL_COMPILER
  44. #pragma GCC diagnostic ignored "-Wold-style-cast"
  45. #endif
  46. test_return_t gearman_execute_test(void *object)
  47. {
  48. gearman_client_st *client= (gearman_client_st *)object;
  49. const char *worker_function= (const char *)gearman_client_context(client);
  50. assert(worker_function);
  51. gearman_task_st *task;
  52. gearman_argument_t value= gearman_argument_make(gearman_literal_param("test load"));
  53. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), NULL, 0, NULL, &value), gearman_client_error(client));
  54. test_compare(gearman_literal_param_size("test load"), gearman_result_size(gearman_task_result(task)));
  55. gearman_task_free(task);
  56. return TEST_SUCCESS;
  57. }
  58. test_return_t gearman_execute_fail_test(void *object)
  59. {
  60. gearman_client_st *client= (gearman_client_st *)object;
  61. const char *worker_function= (const char *)gearman_client_context(client);
  62. assert(worker_function);
  63. gearman_task_st *task;
  64. gearman_argument_t value= gearman_argument_make(gearman_literal_param("fail"));
  65. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), NULL, 0, NULL, &value), gearman_client_error(client));
  66. test_compare_got(GEARMAN_WORK_FAIL, gearman_task_error(task), gearman_strerror(gearman_task_error(task)));
  67. gearman_task_free(task);
  68. return TEST_SUCCESS;
  69. }
  70. test_return_t gearman_execute_timeout_test(void *object)
  71. {
  72. gearman_client_st *client= (gearman_client_st *)object;
  73. const char *worker_function= (const char *)gearman_client_context(client);
  74. assert(worker_function);
  75. test_truth(client);
  76. gearman_client_set_timeout(client, 4);
  77. // We should fail since the the timeout is small and the function should
  78. // not exist.
  79. gearman_task_st *task;
  80. gearman_argument_t value= gearman_argument_make(gearman_literal_param("test load"));
  81. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), NULL, 0, NULL, &value), gearman_client_error(client));
  82. gearman_task_free(task);
  83. return TEST_SUCCESS;
  84. }
  85. test_return_t gearman_execute_epoch_test(void *object)
  86. {
  87. gearman_client_st *client= (gearman_client_st *)object;
  88. const char *worker_function= (const char *)gearman_client_context(client);
  89. assert(worker_function);
  90. gearman_work_t workload= gearman_work_epoch(time(NULL) +5, GEARMAN_JOB_PRIORITY_NORMAL);
  91. gearman_task_st *task;
  92. gearman_argument_t value= gearman_argument_make(gearman_literal_param("test load"));
  93. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), NULL, 0, &workload, &value), gearman_client_error(client));
  94. test_truth(task);
  95. test_truth(gearman_task_job_handle(task));
  96. gearman_task_free(task);
  97. return TEST_SUCCESS;
  98. }
  99. test_return_t gearman_execute_bg_test(void *object)
  100. {
  101. gearman_client_st *client= (gearman_client_st *)object;
  102. const char *worker_function= (const char *)gearman_client_context(client);
  103. assert(worker_function);
  104. gearman_work_t workload= gearman_work_background(GEARMAN_JOB_PRIORITY_NORMAL);
  105. gearman_task_st *task;
  106. gearman_argument_t value= gearman_argument_make(gearman_literal_param("test load"));
  107. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), gearman_literal_param("my id"), &workload, &value), gearman_client_error(client));
  108. // Lets make sure we have a task
  109. test_truth(task);
  110. test_truth(gearman_task_job_handle(task));
  111. test_true_got(gearman_success(gearman_client_run_tasks(client)), gearman_client_error(client));
  112. gearman_task_free(task);
  113. return TEST_SUCCESS;
  114. }
  115. test_return_t gearman_execute_multile_bg_test(void *object)
  116. {
  117. gearman_client_st *client= (gearman_client_st *)object;
  118. const char *worker_function= (const char *)gearman_client_context(client);
  119. assert(worker_function);
  120. for (uint32_t x= 0; x < 4; /* No reason for number */ x++)
  121. {
  122. gearman_work_t workload= gearman_work_background(GEARMAN_JOB_PRIORITY_NORMAL);
  123. gearman_task_st *task;
  124. gearman_argument_t value= gearman_argument_make(gearman_literal_param("test load"));
  125. test_true_got(task= gearman_execute(client, gearman_c_str_param(worker_function), NULL, 0, &workload, &value), gearman_client_error(client));
  126. // Lets make sure we have a task
  127. test_truth(task);
  128. test_truth(gearman_task_job_handle(task));
  129. }
  130. gearman_return_t rc;
  131. test_true_got(gearman_success(rc= gearman_client_run_tasks(client)), gearman_strerror(rc));
  132. gearman_client_task_free_all(client);
  133. return TEST_SUCCESS;
  134. }