basic.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <config.h>
  38. #include <cstring>
  39. #include <libgearman/gearman.h>
  40. #include <tests/basic.h>
  41. #include <tests/context.h>
  42. #pragma GCC diagnostic ignored "-Wold-style-cast"
  43. /* Counter test for worker */
  44. static void *counter_function(gearman_job_st *job __attribute__((unused)),
  45. void *context, size_t *result_size,
  46. gearman_return_t *ret_ptr __attribute__((unused)))
  47. {
  48. uint32_t *counter= (uint32_t *)context;
  49. *result_size= 0;
  50. *counter= *counter + 1;
  51. return NULL;
  52. }
  53. test_return_t echo_test(void *object)
  54. {
  55. gearman_client_st client, *client_ptr;
  56. gearman_return_t rc;
  57. const char *value= "This is my echo test";
  58. size_t value_length= sizeof("This is my echo test") -1;
  59. (void)object;
  60. client_ptr= gearman_client_create(&client);
  61. test_truth(client_ptr);
  62. rc= gearman_client_echo(&client, (uint8_t *)value, value_length);
  63. test_truth(rc == GEARMAN_SUCCESS);
  64. gearman_client_free(&client);
  65. return TEST_SUCCESS;
  66. }
  67. test_return_t queue_clean(void *object)
  68. {
  69. Context *test= (Context *)object;
  70. gearman_worker_st *worker= test->worker;
  71. uint32_t counter= 0;
  72. gearman_return_t rc;
  73. gearman_worker_set_timeout(worker, 200);
  74. rc= gearman_worker_add_function(worker, "queue_test", 5, counter_function, &counter);
  75. test_truth(rc == GEARMAN_SUCCESS);
  76. // Clean out any jobs that might still be in the queue from failed tests.
  77. while (1)
  78. {
  79. rc= gearman_worker_work(worker);
  80. if (rc != GEARMAN_SUCCESS)
  81. break;
  82. }
  83. return TEST_SUCCESS;
  84. }
  85. test_return_t queue_add(void *object)
  86. {
  87. Context *test= (Context *)object;
  88. gearman_client_st client, *client_ptr;
  89. char job_handle[GEARMAN_JOB_HANDLE_SIZE];
  90. uint8_t *value= (uint8_t *)"background_test";
  91. size_t value_length= strlen("background_test");
  92. gearman_return_t rc;
  93. test->run_worker= false;
  94. client_ptr= gearman_client_create(&client);
  95. test_truth(client_ptr);
  96. rc= gearman_client_add_server(&client, NULL, test->port());
  97. test_truth(rc == GEARMAN_SUCCESS);
  98. rc= gearman_client_echo(&client, (uint8_t *)value, value_length);
  99. test_truth(rc == GEARMAN_SUCCESS);
  100. rc= gearman_client_do_background(&client, "queue_test", NULL, value,
  101. value_length, job_handle);
  102. test_truth(rc == GEARMAN_SUCCESS);
  103. gearman_client_free(&client);
  104. test->run_worker= true;
  105. return TEST_SUCCESS;
  106. }
  107. #include <iostream>
  108. test_return_t queue_worker(void *object)
  109. {
  110. Context *test= (Context *)object;
  111. gearman_worker_st *worker= test->worker;
  112. uint32_t counter= 0;
  113. gearman_return_t rc;
  114. if (! test->run_worker)
  115. return TEST_FAILURE;
  116. rc= gearman_worker_add_function(worker, "queue_test", 5, counter_function, &counter);
  117. test_truth(rc == GEARMAN_SUCCESS);
  118. gearman_worker_set_timeout(worker, 5);
  119. std::cerr << "got here " << std::endl;
  120. rc= gearman_worker_work(worker);
  121. test_truth(rc == GEARMAN_SUCCESS or rc == GEARMAN_TIMEOUT);
  122. std::cerr << "gearman_worker_work() " << std::endl;
  123. if (rc == GEARMAN_SUCCESS)
  124. {
  125. test_truth (counter != 0);
  126. }
  127. return TEST_SUCCESS;
  128. }