Browse Source

Move out the worker we use from basic such that all tests can use it.

Brian Aker 12 years ago
parent
commit
8cf6d86e9d

+ 11 - 48
tests/basic.cc

@@ -50,48 +50,11 @@ using namespace libtest;
 #include <tests/context.h>
 #include <tests/start_worker.h>
 
+#include "tests/workers/v2/called.h"
+
 #ifndef __INTEL_COMPILER
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 #endif
-pthread_mutex_t counter_lock= PTHREAD_MUTEX_INITIALIZER;
-
-struct counter_st
-{
-  int32_t _count;
-  pthread_mutex_t _lock;
-
-  counter_st() :
-    _count(0)
-  {
-    pthread_mutex_init(&_lock, NULL);
-  }
-
-  void increment()
-  {
-    pthread_mutex_lock(&_lock);
-    _count++;
-    pthread_mutex_unlock(&_lock);
-  }
-
-  int32_t count()
-  {
-    int32_t tmp;
-    pthread_mutex_lock(&_lock);
-    tmp= _count;
-    pthread_mutex_unlock(&_lock);
-
-    return tmp;
-  }
-};
-
-static gearman_return_t counter_fn(gearman_job_st *, void *object)
-{
-  counter_st *count= (counter_st*)object;
-  assert(count);
-  count->increment();
-
-  return GEARMAN_SUCCESS;
-}
 
 test_return_t client_echo_fail_test(void *object)
 {
@@ -153,12 +116,12 @@ test_return_t queue_clean(void *object)
 
   gearman_worker_set_timeout(worker, 3000);
 
-  counter_st counter;
-  gearman_function_t counter_function= gearman_function_create(counter_fn);
+  Called called;
+  gearman_function_t counter_function= gearman_function_create(called_worker);
   test_compare(gearman_worker_define_function(worker,
                                               test->worker_function_name(), strlen(test->worker_function_name()),
                                               counter_function,
-                                              5, &counter), GEARMAN_SUCCESS);
+                                              5, &called), GEARMAN_SUCCESS);
 
   // Clean out any jobs that might still be in the queue from failed tests.
   while (GEARMAN_SUCCESS == gearman_worker_work(worker)) {};
@@ -220,8 +183,8 @@ test_return_t queue_worker(void *object)
 
   test_true(test->run_worker);
 
-  counter_st counter;
-  gearman_function_t counter_function= gearman_function_create(counter_fn);
+  Called counter;
+  gearman_function_t counter_function= gearman_function_create(called_worker);
   test_compare(gearman_worker_define_function(worker,
                                               test->worker_function_name(), strlen(test->worker_function_name()),
                                               counter_function,
@@ -267,14 +230,14 @@ test_return_t lp_734663(void *object)
 
   struct worker_handle_st *worker_handle[NUMBER_OF_WORKERS];
 
-  counter_st counter;
-  gearman_function_t counter_function_fn= gearman_function_create(counter_fn);
+  Called called;
+  gearman_function_t counter_function_fn= gearman_function_create(called_worker);
   for (uint32_t x= 0; x < NUMBER_OF_WORKERS; x++)
   {
-    worker_handle[x]= test_worker_start(test->port(), NULL, worker_function_name, counter_function_fn, &counter, gearman_worker_options_t());
+    worker_handle[x]= test_worker_start(test->port(), NULL, worker_function_name, counter_function_fn, &called, gearman_worker_options_t());
   }
 
-  while (counter.count() < NUMBER_OF_JOBS)
+  while (called.count() < NUMBER_OF_JOBS)
   {
     libtest::dream(0, static_cast<long>(gearman_timeout(client) *1000));
   }

+ 1 - 0
tests/include.am

@@ -39,6 +39,7 @@ tests_libstartworker_la_LIBADD+= $(BOOST_THREAD_LIBS)
 tests_libstartworker_la_LIBADD+= libtest/libtest.la
 tests_libstartworker_la_LIBADD+= libgearman/libgearman.la
 tests_libstartworker_la_SOURCES+= tests/start_worker.cc
+tests_libstartworker_la_SOURCES+= tests/workers/v2/called.cc
 tests_libstartworker_la_SOURCES+= util/instance.cc
 tests_libstartworker_la_SOURCES+= util/operation.cc
 

+ 1 - 21
tests/libgearman-1.0/client_test.cc

@@ -1353,27 +1353,6 @@ static void *world_create(server_startup_st& servers, test_return_t& error)
 
   test->add_server(NULL, libtest::default_port());
 
-  // Namespace versions of the above
-
-  // Long function name
-
-#if 0
-  gearman_function_t increment_reset_worker_fn= gearman_function_create_v1(increment_reset_worker);
-  for (uint32_t x= 0; x < 10; x++)
-  {
-    test->push(test_worker_start(libtest::default_port(), 
-                                 NULL,
-                                 "increment_reset_worker", increment_reset_worker_fn, 
-                                 NULL, gearman_worker_options_t()));
-  }
-#endif
-
-  // Count worker
-#if 0
-  gearman_function_t count_worker_fn= gearman_function_create(count_worker);
-  test->push(test_worker_start(libtest::default_port(), NULL, "count", count_worker_fn, NULL, gearman_worker_options_t()));
-#endif
-
   error= TEST_SUCCESS;
 
   return (void *)test;
@@ -1440,6 +1419,7 @@ test_st loop_TESTS[] ={
 test_st coalescence_TESTS[] ={
   {"basic coalescence", 0, coalescence_TEST },
   {"coalescence by data", 0, coalescence_by_data_TEST },
+  {"coalescence by data fail", 0, coalescence_by_data_FAIL_TEST },
   {0, 0, 0}
 };
 

+ 70 - 0
tests/libgearman-1.0/unique.cc

@@ -215,6 +215,76 @@ test_return_t coalescence_by_data_TEST(void *object)
   return TEST_SUCCESS;
 }
 
+test_return_t coalescence_by_data_FAIL_TEST(void *object)
+{
+  gearman_client_st *client_one= (gearman_client_st *)object;
+  test_true(client_one);
+
+  Client client_two(client_one);
+
+  const char* unique_handle= "-";
+
+  gearman_function_t sleep_return_random_worker_FN= gearman_function_create(sleep_return_random_worker);
+  std::auto_ptr<worker_handle_st> handle(test_worker_start(libtest::default_port(),
+                                                           NULL,
+                                                           __func__,
+                                                           sleep_return_random_worker_FN,
+                                                           NULL,
+                                                           gearman_worker_options_t(),
+                                                           0)); // timeout
+
+  // First task
+  gearman_return_t ret;
+  gearman_task_st *first_task= gearman_client_add_task(client_one,
+                                                       NULL, // preallocated task
+                                                       NULL, // context 
+                                                       __func__, // function
+                                                       unique_handle, // unique
+                                                       NULL, 0, // workload
+                                                       &ret);
+  test_compare(GEARMAN_SUCCESS, ret);
+  test_truth(first_task);
+  test_true(gearman_task_unique(first_task));
+  test_compare(strlen(unique_handle), strlen(gearman_task_unique(first_task)));
+ 
+  // Second task
+  gearman_task_st *second_task= gearman_client_add_task(&client_two,
+                                                        NULL, // preallocated task
+                                                        NULL, // context 
+                                                        __func__, // function
+                                                        unique_handle, // unique
+                                                        test_literal_param("mine"), // workload
+                                                        &ret);
+  test_compare(GEARMAN_SUCCESS, ret);
+  test_truth(second_task);
+  test_true(gearman_task_unique(second_task));
+  test_compare(strlen(unique_handle), strlen(gearman_task_unique(second_task)));
+  
+  test_strcmp(gearman_task_unique(first_task), gearman_task_unique(second_task));
+
+  do {
+    ret= gearman_client_run_tasks(client_one);
+    gearman_client_run_tasks(&client_two);
+  } while (gearman_continue(ret));
+
+  do {
+    ret= gearman_client_run_tasks(&client_two);
+  } while (gearman_continue(ret));
+
+  gearman_result_st* first_result= gearman_task_result(first_task);
+  gearman_result_st* second_result= gearman_task_result(second_task);
+
+  test_compare(GEARMAN_SUCCESS, gearman_task_return(first_task));
+  test_compare(GEARMAN_SUCCESS, gearman_task_return(second_task));
+
+  test_compare(gearman_result_value(first_result), gearman_result_value(second_result));
+
+  gearman_task_free(first_task);
+  gearman_task_free(second_task);
+
+  return TEST_SUCCESS;
+}
+
 test_return_t unique_compare_test(void *object)
 {
   gearman_return_t rc;

+ 1 - 0
tests/unique.h

@@ -44,5 +44,6 @@ test_return_t unique_SETUP(void *);
 test_return_t unique_compare_test(void *);
 test_return_t coalescence_TEST(void*);
 test_return_t coalescence_by_data_TEST(void*);
+test_return_t coalescence_by_data_FAIL_TEST(void*);
 test_return_t gearman_client_unique_status_TEST(void*);
 test_return_t gearman_client_unique_status_NOT_FOUND_TEST(void *object);

+ 1 - 0
tests/workers/include.am

@@ -13,6 +13,7 @@ noinst_HEADERS+= tests/workers/v1/echo_or_react_chunk.h
 noinst_HEADERS+= tests/workers/v1/increment_reset.h
 noinst_HEADERS+= tests/workers/v1/unique.h
 
+noinst_HEADERS+= tests/workers/v2/called.h
 noinst_HEADERS+= tests/workers/v2/count.h
 noinst_HEADERS+= tests/workers/v2/echo_or_react.h
 noinst_HEADERS+= tests/workers/v2/echo_or_react_chunk.h

+ 53 - 0
tests/workers/v2/called.cc

@@ -0,0 +1,53 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <config.h>
+
+#include <libtest/test.hpp>
+
+#include <libgearman-1.0/gearman.h>
+
+#include "tests/workers/v2/called.h"
+
+gearman_return_t called_worker(gearman_job_st *, void *object)
+{
+  Called *count= (Called*)object;
+  assert(count);
+  count->increment();
+
+  return GEARMAN_SUCCESS;
+}

+ 80 - 0
tests/workers/v2/called.h

@@ -0,0 +1,80 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#pragma once
+
+#include <pthread.h>
+
+class Called
+{
+public:
+  Called() :
+    _count(0)
+  {
+    pthread_mutex_init(&_lock, NULL);
+  }
+
+  ~Called()
+  {
+    pthread_mutex_destroy(&_lock);
+  }
+
+  void increment()
+  {
+    pthread_mutex_lock(&_lock);
+    _count++;
+    pthread_mutex_unlock(&_lock);
+  }
+
+  int32_t count()
+  {
+    int32_t tmp;
+    pthread_mutex_lock(&_lock);
+    tmp= _count;
+    pthread_mutex_unlock(&_lock);
+
+    return tmp;
+  }
+
+private:
+  int32_t _count;
+  pthread_mutex_t _lock;
+
+};
+
+LIBTEST_API
+gearman_return_t called_worker(gearman_job_st *job, void *context);