runner.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #pragma once
  39. #include "tests/libgearman-1.0/client_test.h"
  40. class GearmandRunner : public Runner {
  41. private:
  42. typedef test_return_t (*libgearman_test_prepost_callback_fn)(client_test_st *);
  43. typedef test_return_t (*libgearman_test_callback_fn)(gearman_client_st *);
  44. public:
  45. test_return_t run(test_callback_fn* func, void *object)
  46. {
  47. return _run(libgearman_test_callback_fn(func), (client_test_st*)object);
  48. }
  49. test_return_t pre(test_callback_fn* func, void *object)
  50. {
  51. return _setup(libgearman_test_prepost_callback_fn(func), (client_test_st*)object);
  52. }
  53. test_return_t post(test_callback_fn* func, void *object)
  54. {
  55. return _teardown(libgearman_test_prepost_callback_fn(func), (client_test_st*)object);
  56. }
  57. private:
  58. test_return_t _setup(libgearman_test_prepost_callback_fn func, client_test_st *container)
  59. {
  60. if (func)
  61. {
  62. return func(container);
  63. }
  64. return TEST_SUCCESS;
  65. }
  66. test_return_t _teardown(libgearman_test_prepost_callback_fn func, client_test_st *container)
  67. {
  68. if (func)
  69. {
  70. return func(container);
  71. }
  72. container->clear();
  73. return TEST_SUCCESS;
  74. }
  75. test_return_t _run(libgearman_test_callback_fn func, client_test_st *container)
  76. {
  77. if (func)
  78. {
  79. test_return_t rc;
  80. {
  81. org::gearmand::libgearman::Client client(container->client());
  82. gearman_client_set_context(&client, (void *)container->worker_name());
  83. if (container->session_namespace())
  84. {
  85. gearman_client_set_namespace(&client, container->session_namespace(),strlen(container->session_namespace()));
  86. }
  87. rc= func(&client);
  88. if (rc == TEST_SUCCESS)
  89. {
  90. #if defined(DEBUG) && DEBUG
  91. test_warn(gearman_client_has_tasks(&client) == false, "client has uncompleted tasks");
  92. #endif
  93. }
  94. }
  95. return rc;
  96. }
  97. return TEST_SUCCESS;
  98. }
  99. };