~brianaker/libmemcached/1164440

929.1.185 by Brian Aker
Merge in all of libtest updates.
1
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2
 * 
3
 *  Libmemcached 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 <config.h>
39
40
#include <libtest/test.hpp>
41
#include <climits>
42
43
using namespace libtest;
44
45
#include <libmemcached/memcached.h>
46
#include <tests/debug.h>
929.1.194 by Brian Aker
Fix it so socket is built just on main tests.
47
#include <tests/print.h>
929.1.185 by Brian Aker
Merge in all of libtest updates.
48
49
/* Dump each server's keys */
50
static memcached_return_t print_keys_callback(const memcached_st *,
51
                                              const char *key,
52
                                              size_t key_length,
53
                                              void *)
54
{
55
56
  Out << "\t" << key << " (" << key_length << ")";
57
  
58
59
  return MEMCACHED_SUCCESS;
60
}
61
62
static memcached_return_t server_wrapper_for_dump_callback(const memcached_st *,
63
                                                           memcached_server_instance_st server,
64
                                                           void *)
65
{
66
  memcached_st *memc= memcached_create(NULL);
67
68
  if (server->type == MEMCACHED_CONNECTION_UNIX_SOCKET)
69
  {
70
    if (memcached_failed(memcached_server_add_unix_socket(memc, memcached_server_name(server))))
71
    {
72
      return MEMCACHED_FAILURE;
73
    }
74
  }
75
  else
76
  {
77
    if (memcached_failed(memcached_server_add(memc, memcached_server_name(server), memcached_server_port(server))))
78
    {
79
      return MEMCACHED_FAILURE;
80
    }
81
  }
82
83
  memcached_dump_fn callbacks[1];
84
85
  callbacks[0]= &print_keys_callback;
86
87
  Out << memcached_server_name(server) << ":" << memcached_server_port(server);
88
89
  if (memcached_failed(memcached_dump(memc, callbacks, NULL, 1)))
90
  {
91
    return MEMCACHED_FAILURE;
92
  }
93
94
  memcached_free(memc);
95
96
  return MEMCACHED_SUCCESS;
97
}
98
99
929.1.246 by Brian Aker
Update for OSX Lion build.
100
test_return_t confirm_keys_exist(memcached_st *memc, const char * const *keys, const size_t number_of_keys, bool key_matches_value, bool require_all)
929.1.185 by Brian Aker
Merge in all of libtest updates.
101
{
102
  for (size_t x= 0; x < number_of_keys; ++x)
103
  {
104
    memcached_return_t rc;
105
    size_t value_length;
106
    char *value= memcached_get(memc,
107
                               test_string_make_from_cstr(keys[x]), // Keys
108
                               &value_length,
109
                               0, &rc);
929.1.246 by Brian Aker
Update for OSX Lion build.
110
    if (require_all)
111
    {
112
      test_true_got(value, keys[x]);
113
      if (key_matches_value)
114
      {
115
        test_strcmp(keys[x], value);
116
      }
117
    }
118
    else if (memcached_success(rc))
119
    {
120
      test_warn_hint(value, keys[x]);
121
      if (value and key_matches_value)
122
      {
123
        test_strcmp(keys[x], value);
124
      }
125
    }
126
127
    if (value)
128
    {
129
      free(value);
130
    }
929.1.185 by Brian Aker
Merge in all of libtest updates.
131
  }
132
133
  return TEST_SUCCESS;
134
}
135
136
test_return_t confirm_keys_dont_exist(memcached_st *memc, const char * const *keys, const size_t number_of_keys)
137
{
138
  for (size_t x= 0; x < number_of_keys; ++x)
139
  {
140
    memcached_return_t rc;
141
    size_t value_length;
142
    char *value= memcached_get(memc,
143
                               test_string_make_from_cstr(keys[x]), // Keys
144
                               &value_length,
145
                               0, &rc);
146
    test_false(value);
147
    test_compare(MEMCACHED_NOTFOUND, rc);
148
  }
149
150
  return TEST_SUCCESS;
151
}
152
153
154
test_return_t print_keys_by_server(memcached_st *memc)
155
{
156
  memcached_server_fn callback[]= { server_wrapper_for_dump_callback };
157
  test_compare(MEMCACHED_SUCCESS, memcached_server_cursor(memc, callback, NULL, test_array_length(callback)));
158
159
  return TEST_SUCCESS;
160
}
161
162
static memcached_return_t callback_dump_counter(const memcached_st *ptr,
163
                                                const char *key,
164
                                                size_t key_length,
165
                                                void *context)
166
{
167
  (void)ptr; (void)key; (void)key_length;
168
  size_t *counter= (size_t *)context;
169
170
  *counter= *counter + 1;
171
172
  return MEMCACHED_SUCCESS;
173
}
174
175
size_t confirm_key_count(memcached_st *memc)
176
{
177
  memcached_st *clone= memcached_clone(NULL, memc);
178
  if (memcached_failed(memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, false)))
179
  {
180
    memcached_free(clone);
181
    return ULONG_MAX;
182
  }
183
184
  memcached_dump_fn callbacks[1];
185
186
  callbacks[0]= &callback_dump_counter;
187
188
  size_t count= 0;
189
  if (memcached_failed(memcached_dump(clone, callbacks, (void *)&count, 1)))
190
  {
191
    memcached_free(clone);
192
    return ULONG_MAX;
193
  }
194
195
  memcached_free(clone);
196
  return count;
197
}
929.1.194 by Brian Aker
Fix it so socket is built just on main tests.
198
199
void print_servers(memcached_st *memc)
200
{
201
  memcached_server_fn callbacks[1];
202
  callbacks[0]= server_print_callback;
203
  memcached_server_cursor(memc, callbacks, NULL,  1);
204
}