1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libtest/test.hpp>
44 #include <libmemcached-1.0/memcached.h>
45 #include "tests/libmemcached-1.0/memcached_get.h"
46 #include "tests/libmemcached-1.0/setup_and_teardowns.h"
48 test_return_t
get_test(memcached_st
*memc
)
50 uint64_t query_id
= memcached_query_id(memc
);
51 memcached_return_t rc
= memcached_delete(memc
,
52 test_literal_param(__func__
),
54 test_true_hint(rc
== MEMCACHED_BUFFERED
or rc
== MEMCACHED_NOTFOUND
, memcached_last_error_message(memc
));
55 test_compare(query_id
+1, memcached_query_id(memc
));
59 char *string
= memcached_get(memc
,
60 test_literal_param(__func__
),
61 &string_length
, &flags
, &rc
);
63 test_compare_got(MEMCACHED_NOTFOUND
, rc
, memcached_last_error_message(memc
));
64 test_false(string_length
);
70 test_return_t
get_test2(memcached_st
*memc
)
72 const char *value
= "when we sanitize";
74 uint64_t query_id
= memcached_query_id(memc
);
75 test_compare(return_value_based_on_buffering(memc
),
77 test_literal_param(__func__
),
79 time_t(0), uint32_t(0)));
80 test_compare(query_id
+1, memcached_query_id(memc
));
82 query_id
= memcached_query_id(memc
);
87 memcached_return_t rc
;
88 char *string
= memcached_get(memc
,
89 test_literal_param(__func__
),
90 &string_length
, &flags
, &rc
);
91 test_compare(query_id
+1, memcached_query_id(memc
));
93 test_compare_got(MEMCACHED_SUCCESS
, rc
, memcached_strerror(NULL
, rc
));
94 test_compare_got(MEMCACHED_SUCCESS
, memcached_last_error(memc
), memcached_last_error_message(memc
));
96 test_compare(strlen(value
), string_length
);
97 test_memcmp(string
, value
, string_length
);
104 test_return_t
get_test3(memcached_st
*memc
)
106 size_t value_length
= 8191;
108 libtest::vchar_t value
;
109 value
.reserve(value_length
);
110 for (uint32_t x
= 0; x
< value_length
; x
++)
112 value
.push_back(char(x
% 127));
115 test_compare_hint(return_value_based_on_buffering(memc
),
117 test_literal_param(__func__
),
118 &value
[0], value
.size(),
119 time_t(0), uint32_t(0)),
120 memcached_last_error_message(memc
));
122 size_t string_length
;
124 memcached_return_t rc
;
125 char *string
= memcached_get(memc
,
126 test_literal_param(__func__
),
127 &string_length
, &flags
, &rc
);
129 test_compare(MEMCACHED_SUCCESS
, rc
);
131 test_compare(value
.size(), string_length
);
132 test_memcmp(string
, &value
[0], string_length
);
139 test_return_t
get_test4(memcached_st
*memc
)
141 size_t value_length
= 8191;
143 libtest::vchar_t value
;
144 value
.reserve(value_length
);
145 for (uint32_t x
= 0; x
< value_length
; x
++)
147 value
.push_back(char(x
% 127));
150 test_compare_hint(return_value_based_on_buffering(memc
),
152 test_literal_param(__func__
),
153 &value
[0], value
.size(),
154 time_t(0), uint32_t(0)),
155 memcached_last_error_message(memc
));
157 for (uint32_t x
= 0; x
< 10; x
++)
160 size_t string_length
;
161 memcached_return_t rc
;
162 char *string
= memcached_get(memc
,
163 test_literal_param(__func__
),
164 &string_length
, &flags
, &rc
);
166 test_compare(MEMCACHED_SUCCESS
, rc
);
168 test_compare(value
.size(), string_length
);
169 test_memcmp(string
, &value
[0], string_length
);
177 * This test verifies that memcached_read_one_response doesn't try to
178 * dereference a NIL-pointer if you issue a multi-get and don't read out all
179 * responses before you execute a storage command.
181 test_return_t
get_test5(memcached_st
*memc
)
184 ** Request the same key twice, to ensure that we hash to the same server
185 ** (so that we have multiple response values queued up) ;-)
187 const char *keys
[]= { "key", "key" };
188 size_t lengths
[]= { 3, 3 };
192 test_compare_hint(return_value_based_on_buffering(memc
),
193 memcached_set(memc
, keys
[0], lengths
[0],
195 time_t(0), uint32_t(0)),
196 memcached_last_error_message(memc
));
197 test_compare(MEMCACHED_SUCCESS
, memcached_mget(memc
, keys
, lengths
, test_array_length(keys
)));
199 memcached_result_st results_obj
;
200 memcached_result_st
*results
= memcached_result_create(memc
, &results_obj
);
203 memcached_return_t rc
;
204 results
= memcached_fetch_result(memc
, &results_obj
, &rc
);
207 memcached_result_free(&results_obj
);
209 /* Don't read out the second result, but issue a set instead.. */
210 test_compare(MEMCACHED_SUCCESS
, memcached_set(memc
, keys
[0], lengths
[0], keys
[0], lengths
[0], 0, 0));
212 char *val
= memcached_get_by_key(memc
, keys
[0], lengths
[0], "yek", 3,
215 test_compare(MEMCACHED_NOTFOUND
, rc
);
216 val
= memcached_get(memc
, keys
[0], lengths
[0], &rlen
, &flags
, &rc
);
218 test_compare(MEMCACHED_SUCCESS
, rc
);