Skip to content

Commit 6ace07d

Browse files
committed
Tests for default behavior
1 parent 794dc33 commit 6ace07d

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

php_memcached.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ PHP_INI_BEGIN()
341341
MEMC_INI_ENTRY("compression_threshold", "2000", OnUpdateLong, compression_threshold)
342342
MEMC_INI_ENTRY("serializer", SERIALIZER_DEFAULT_NAME, OnUpdateSerializer, serializer_name)
343343
MEMC_INI_ENTRY("store_retry_count", "2", OnUpdateLong, store_retry_count)
344+
345+
MEMC_INI_ENTRY("default_consistent_hash", "0", OnUpdateBool, default_behavior.consistent_hash_enabled)
346+
MEMC_INI_ENTRY("default_binary_protocol", "0", OnUpdateBool, default_behavior.binary_protocol_enabled)
347+
MEMC_INI_ENTRY("default_connect_timeout", "0", OnUpdateLongGEZero, default_behavior.connect_timeout)
348+
344349
PHP_INI_END()
345350
/* }}} */
346351

@@ -1190,7 +1195,8 @@ static PHP_METHOD(Memcached, __construct)
11901195
}
11911196

11921197
if (!intern->memc) {
1193-
// TODO: handle allocation fail
1198+
php_error_docref(NULL, E_ERROR, "Failed to allocate memory for memcached structure");
1199+
/* never reached */
11941200
}
11951201

11961202
memc_user_data = pecalloc (1, sizeof(*memc_user_data), is_persistent);
@@ -1203,6 +1209,38 @@ static PHP_METHOD(Memcached, __construct)
12031209

12041210
memcached_set_user_data(intern->memc, memc_user_data);
12051211

1212+
/* Set default behaviors */
1213+
{
1214+
#ifdef mikko_0
1215+
fprintf (stderr, "consistent_hash_enabled=%d binary_protocol_enabled=%d connect_timeout=%ld\n",
1216+
MEMC_G(default_behavior.consistent_hash_enabled), MEMC_G(default_behavior.binary_protocol_enabled), MEMC_G(default_behavior.connect_timeout));
1217+
#endif
1218+
1219+
memcached_return rc;
1220+
1221+
if (MEMC_G(default_behavior.consistent_hash_enabled)) {
1222+
1223+
rc = memcached_behavior_set(intern->memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT);
1224+
if (rc != MEMCACHED_SUCCESS) {
1225+
php_error_docref(NULL, E_WARNING, "Failed to turn on consistent hash: %s", memcached_strerror(intern->memc, rc));
1226+
}
1227+
}
1228+
1229+
if (MEMC_G(default_behavior.binary_protocol_enabled)) {
1230+
rc = memcached_behavior_set(intern->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
1231+
if (rc != MEMCACHED_SUCCESS) {
1232+
php_error_docref(NULL, E_WARNING, "Failed to turn on binary protocol: %s", memcached_strerror(intern->memc, rc));
1233+
}
1234+
}
1235+
1236+
if (MEMC_G(default_behavior.connect_timeout)) {
1237+
rc = memcached_behavior_set(intern->memc, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, MEMC_G(default_behavior.connect_timeout));
1238+
if (rc != MEMCACHED_SUCCESS) {
1239+
php_error_docref(NULL, E_WARNING, "Failed to set connect timeout: %s", memcached_strerror(intern->memc, rc));
1240+
}
1241+
}
1242+
}
1243+
12061244
if (fci.size) {
12071245
if (!s_invoke_new_instance_cb(getThis(), &fci, &fci_cache, persistent_id) || EG(exception)) {
12081246
/* error calling or exception thrown from callback */
@@ -2263,7 +2301,7 @@ PHP_METHOD(Memcached, addServer)
22632301
}
22642302

22652303
MEMC_METHOD_FETCH_OBJECT;
2266-
intern->rescode = MEMCACHED_SUCCESS;
2304+
s_memc_set_status(intern, MEMCACHED_SUCCESS, 0);
22672305

22682306
#if defined(LIBMEMCACHED_VERSION_HEX) && LIBMEMCACHED_VERSION_HEX < 0x01000002
22692307
if (host->val[0] == '/') { /* unix domain socket */
@@ -2712,7 +2750,7 @@ static PHP_METHOD(Memcached, flush)
27122750
}
27132751

27142752
MEMC_METHOD_FETCH_OBJECT;
2715-
intern->rescode = MEMCACHED_SUCCESS;
2753+
s_memc_set_status(intern, MEMCACHED_SUCCESS, 0);
27162754

27172755
status = memcached_flush(intern->memc, delay);
27182756
if (s_memc_status_handle_result_code(intern, status) == FAILURE) {
@@ -4100,6 +4138,11 @@ PHP_GINIT_FUNCTION(php_memcached)
41004138

41014139
php_memcached_globals->memc.sasl_initialised = 0;
41024140
php_memcached_globals->no_effect = 0;
4141+
4142+
/* Defaults for certain options */
4143+
php_memcached_globals->memc.default_behavior.consistent_hash_enabled = 0;
4144+
php_memcached_globals->memc.default_behavior.binary_protocol_enabled = 0;
4145+
php_memcached_globals->memc.default_behavior.connect_timeout = 0;
41034146
}
41044147

41054148
zend_module_entry memcached_module_entry = {

php_memcached_private.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
150150
zend_long lock_retries;
151151
zend_long lock_expiration;
152152

153-
zend_bool compression_enabled;
154153
zend_bool binary_protocol_enabled;
155154
zend_bool consistent_hash_enabled;
156155

@@ -183,6 +182,14 @@ ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
183182
/* Whether we have initialised sasl for this process */
184183
zend_bool sasl_initialised;
185184

185+
struct {
186+
187+
zend_bool consistent_hash_enabled;
188+
zend_bool binary_protocol_enabled;
189+
zend_long connect_timeout;
190+
191+
} default_behavior;
192+
186193
} memc;
187194

188195
/* For deprecated values */

tests/default_behavior.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Default behaviors
3+
--SKIPIF--
4+
<?php if (!extension_loaded("memcached")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$m = new Memcached();
9+
var_dump ($m->getOption(Memcached::OPT_DISTRIBUTION) == Memcached::DISTRIBUTION_MODULA);
10+
var_dump ($m->getOption(Memcached::OPT_BINARY_PROTOCOL) == false);
11+
var_dump ($m->getOption(Memcached::OPT_CONNECT_TIMEOUT) != 0);
12+
13+
ini_set('memcached.default_consistent_hash', true);
14+
ini_set('memcached.default_binary_protocol', true);
15+
ini_set('memcached.default_connect_timeout', 1212);
16+
17+
$m = new Memcached();
18+
var_dump ($m->getOption(Memcached::OPT_DISTRIBUTION) == Memcached::DISTRIBUTION_CONSISTENT);
19+
var_dump ($m->getOption(Memcached::OPT_BINARY_PROTOCOL) == true);
20+
var_dump ($m->getOption(Memcached::OPT_CONNECT_TIMEOUT) == 1212);
21+
22+
echo "OK";
23+
24+
?>
25+
--EXPECT--
26+
bool(true)
27+
bool(true)
28+
bool(true)
29+
bool(true)
30+
bool(true)
31+
bool(true)
32+
OK

0 commit comments

Comments
 (0)