123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- *
- * Gearmand client and server library.
- *
- * Copyright (C) 2010-2013 Data Differential, http://datadifferential.com/
- * Copyright (C) 2008 Brian Aker, Eric Day
- * 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 "gear_config.h"
- #include <libtest/test.hpp>
- using namespace libtest;
- #include <cassert>
- #include <cerrno>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <libgearman-1.0/gearman.h>
- #include <libtest/test.hpp>
- #include "libgearman/interface/task.hpp"
- #include "libgearman/client.hpp"
- using namespace org::gearmand;
- #include <tests/start_worker.h>
- #define DEFAULT_WORKER_NAME "burnin"
- static gearman_return_t worker_fn(gearman_job_st*, void*)
- {
- return GEARMAN_SUCCESS;
- }
- struct client_test_st {
- libgearman::Client _client;
- worker_handle_st *handle;
- client_test_st():
- _client(libtest::default_port()),
- handle(NULL)
- {
- gearman_function_t func_arg= gearman_function_create(worker_fn);
- handle= test_worker_start(libtest::default_port(), NULL, DEFAULT_WORKER_NAME, func_arg, NULL, gearman_worker_options_t());
- }
- ~client_test_st()
- {
- delete handle;
- }
- gearman_client_st* client()
- {
- return &_client;
- }
- };
- struct client_context_st {
- int latch;
- size_t min_size;
- size_t max_size;
- size_t num_tasks;
- size_t count;
- char *blob;
- client_context_st():
- latch(0),
- min_size(1024),
- max_size(1024 *2),
- num_tasks(20),
- count(2000),
- blob(NULL)
- { }
- ~client_context_st()
- {
- if (blob)
- {
- free(blob);
- }
- }
- };
- #ifndef __INTEL_COMPILER
- #pragma GCC diagnostic ignored "-Wold-style-cast"
- #endif
- static client_test_st *test_client_context= NULL;
- static test_return_t burnin_TEST(void*)
- {
- gearman_client_st *client= test_client_context->client();
- fatal_assert(client);
- client_context_st *context= (client_context_st *)gearman_client_context(client);
- fatal_assert(context);
- // This sketchy, don't do this in your own code.
- test_true(context->num_tasks > 0);
- std::vector<gearman_task_st> tasks;
- try {
- tasks.resize(context->num_tasks);
- }
- catch (...)
- { }
- ASSERT_EQ(tasks.size(), context->num_tasks);
- ASSERT_EQ(gearman_client_echo(client, test_literal_param("echo_test")), GEARMAN_SUCCESS);
- do
- {
- for (uint32_t x= 0; x < context->num_tasks; x++)
- {
- size_t blob_size= 0;
- if (context->min_size == context->max_size)
- {
- blob_size= context->max_size;
- }
- else
- {
- blob_size= (size_t)rand();
- if (context->max_size > RAND_MAX)
- {
- blob_size*= (size_t)(rand() + 1);
- }
- blob_size= (blob_size % (context->max_size - context->min_size)) + context->min_size;
- }
- gearman_task_st *task_ptr;
- gearman_return_t ret;
- if (context->latch)
- {
- task_ptr= gearman_client_add_task_background(client, &(tasks[x]),
- NULL, DEFAULT_WORKER_NAME, NULL,
- (void *)context->blob, blob_size, &ret);
- }
- else
- {
- task_ptr= gearman_client_add_task(client, &(tasks[x]), NULL,
- DEFAULT_WORKER_NAME, NULL, (void *)context->blob, blob_size,
- &ret);
- }
- ASSERT_EQ(ret, GEARMAN_SUCCESS);
- test_truth(task_ptr);
- }
- gearman_return_t ret= gearman_client_run_tasks(client);
- for (uint32_t x= 0; x < context->num_tasks; x++)
- {
- ASSERT_EQ(GEARMAN_TASK_STATE_FINISHED, tasks[x].impl()->state);
- ASSERT_EQ(GEARMAN_SUCCESS, tasks[x].impl()->result_rc);
- }
- test_zero(client->impl()->new_tasks);
- ASSERT_EQ(ret, GEARMAN_SUCCESS);
- for (uint32_t x= 0; x < context->num_tasks; x++)
- {
- gearman_task_free(&(tasks[x]));
- }
- } while (context->count--);
- context->latch++;
- return TEST_SUCCESS;
- }
- static test_return_t burnin_setup(void*)
- {
- test_client_context= new client_test_st;
- client_context_st *context= new client_context_st;
- context->blob= (char *)malloc(context->max_size);
- test_true(context->blob);
- memset(context->blob, 'x', context->max_size);
- gearman_client_set_context(test_client_context->client(), context);
- return TEST_SUCCESS;
- }
- static test_return_t burnin_cleanup(void*)
- {
- client_context_st *context= (struct client_context_st *)gearman_client_context(test_client_context->client());
- delete context;
- delete test_client_context;
- test_client_context= NULL;
- return TEST_SUCCESS;
- }
- /*********************** World functions **************************************/
- static void *world_create(server_startup_st& servers, test_return_t& error)
- {
- if (server_startup(servers, "gearmand", libtest::default_port(), NULL) == false)
- {
- error= TEST_SKIPPED;
- return NULL;
- }
- worker_handles_st *handle= new worker_handles_st;
- if (handle == NULL)
- {
- error= TEST_FAILURE;
- return NULL;
- }
- return handle;
- }
- static bool world_destroy(void *object)
- {
- worker_handles_st *handles= (worker_handles_st *)object;
- delete handles;
- return TEST_SUCCESS;
- }
- test_st burnin_TESTS[] ={
- {"burnin", 0, burnin_TEST },
- {0, 0, 0}
- };
- collection_st collection[] ={
- {"burnin", burnin_setup, burnin_cleanup, burnin_TESTS },
- {0, 0, 0, 0}
- };
- void get_world(libtest::Framework *world)
- {
- world->collections(collection);
- world->create(world_create);
- world->destroy(world_destroy);
- }
|