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 
38 #include <gear_config.h>
39 #include <libtest/test.hpp>
40 
41 #include <libgearman-1.0/gearman.h>
42 
43 #include "tests/workers/v1/echo_or_react_chunk.h"
44 
45 
echo_or_react_chunk_worker(gearman_job_st * job,void *,size_t * result_size,gearman_return_t * ret_ptr)46 void *echo_or_react_chunk_worker(gearman_job_st *job, void *,
47                                  size_t *result_size, gearman_return_t *ret_ptr)
48 {
49   const char *workload;
50   workload= (const char *)gearman_job_workload(job);
51   size_t workload_size= gearman_job_workload_size(job);
52 
53   bool fail= false;
54   if (workload_size == test_literal_param_size("fail") and (not memcmp(workload, test_literal_param("fail"))))
55   {
56     fail= true;
57   }
58   else if (workload_size == test_literal_param_size("exception") and (not memcmp(workload, test_literal_param("exception"))))
59   {
60     gearman_return_t rc= gearman_job_send_exception(job, test_literal_param("test exception"));
61     if (gearman_failed(rc))
62     {
63       *ret_ptr= GEARMAN_WORK_FAIL;
64       return NULL;
65     }
66   }
67   else if (workload_size == test_literal_param_size("warning") and (not memcmp(workload, test_literal_param("warning"))))
68   {
69     gearman_return_t rc= gearman_job_send_warning(job, test_literal_param("test warning"));
70     if (gearman_failed(rc))
71     {
72       *ret_ptr= GEARMAN_WORK_FAIL;
73       return NULL;
74     }
75   }
76 
77   for (size_t x= 0; x < workload_size; x++)
78   {
79     // Chunk
80     {
81       *ret_ptr= gearman_job_send_data(job, &workload[x], 1);
82       if (*ret_ptr != GEARMAN_SUCCESS)
83       {
84         return NULL;
85       }
86     }
87 
88     // report status
89     {
90       *ret_ptr= gearman_job_send_status(job, (uint32_t)x,
91                                         (uint32_t)workload_size);
92       assert(gearman_success(*ret_ptr));
93       if (gearman_failed(*ret_ptr))
94       {
95         return NULL;
96       }
97 
98       if (fail)
99       {
100         *ret_ptr= GEARMAN_WORK_FAIL;
101         return NULL;
102       }
103     }
104   }
105 
106   *ret_ptr= GEARMAN_SUCCESS;
107   *result_size= 0;
108 
109   return NULL;
110 }
111 
112