Skip to content

Commit 09d0f27

Browse files
committed
add 1 test for MemcachedServer
1 parent c7a2084 commit 09d0f27

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

tests/memcachedserver.phpt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Memcached::get() with cache callback
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("memcached")) {
6+
die("skip memcached is not loaded\n");
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
include __DIR__ . '/server.inc';
12+
$server = memcached_server_start();
13+
14+
$cache = new Memcached();
15+
$cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
16+
$cache->setOption(Memcached::OPT_COMPRESSION, false);
17+
$cache->addServer('127.0.0.1', 3434);
18+
19+
$cache->add("add_key", "hello", 500);
20+
$cache->append("append_key", "world");
21+
$cache->prepend("prepend_key", "world");
22+
23+
$cache->increment("incr", 2, 1, 500);
24+
$cache->decrement("decr", 2, 1, 500);
25+
26+
$cache->delete("delete_k");
27+
$cache->flush(1);
28+
29+
var_dump($cache->get('get_this'));
30+
31+
$cache->set ('set_key', 'value 1', 100);
32+
$cache->replace ('replace_key', 'value 2', 200);
33+
34+
var_dump($cache->getStats());
35+
36+
$cache->quit();
37+
38+
memcached_server_stop($server);
39+
?>
40+
Done
41+
--EXPECTF--
42+
Listening on 127.0.0.1:3434
43+
Incoming connection from 127.0.0.1:%s
44+
Incoming connection from 127.0.0.1:%s
45+
client_id=[%s]: Add key=[add_key], value=[hello], flags=[0], expiration=[500]
46+
client_id=[%s]: Append key=[append_key], value=[world], cas=[0]
47+
client_id=[%s]: Prepend key=[prepend_key], value=[world], cas=[0]
48+
client_id=[%s]: Incrementing key=[incr], delta=[2], initial=[1], expiration=[500]
49+
client_id=[%s]: Decrementing key=[decr], delta=[2], initial=[1], expiration=[500]
50+
client_id=[%s]: Delete key=[delete_k], cas=[0]
51+
client_id=[%s]: Flush when=[1]
52+
client_id=[%s]: Get key=[get_this]
53+
client_id=[%s]: Noop
54+
string(20) "Hello to you client!"
55+
client_id=[%s]: Set key=[set_key], value=[value 1], flags=[0], expiration=[100], cas=[0]
56+
client_id=[%s]: Replace key=[replace_key], value=[value 2], flags=[0], expiration=[200], cas=[0]
57+
bool(false)
58+
Done

tests/server.inc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
function memcached_server_start($code = 'server.php', $host = "127.0.0.1", $port = 3434) {
3+
$php_executable = getenv('TEST_PHP_EXECUTABLE') ?? PHP_BINARY;
4+
$php_args = getenv('TEST_PHP_ARGS') ?? '';
5+
6+
$descriptorspec = array(
7+
0 => STDIN,
8+
1 => STDOUT,
9+
2 => STDERR,
10+
);
11+
12+
$cmd = "{$php_executable} {$php_args} {$code} {$host}:{$port} ";
13+
if (substr(PHP_OS, 0, 3) == 'WIN') {
14+
$cmd = "{$php_executable} {$php_args} {$code} {$host}:{$port} ";
15+
16+
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, __DIR__, NULL, array("bypass_shell" => true, "suppress_errors" => true));
17+
} else {
18+
$cmd = "exec {$cmd} 2>/dev/null";
19+
20+
$handle = proc_open($cmd, $descriptorspec, $pipes, __DIR__);
21+
}
22+
23+
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
24+
// it might not be listening yet...need to wait until fsockopen() call returns
25+
$error = "Unable to connect to server\n";
26+
for ($i=0; $i < 60; $i++) {
27+
usleep(50000); // 50ms per try
28+
$status = proc_get_status($handle);
29+
$fp = @fsockopen($host, $port);
30+
// Failure, the server is no longer running
31+
if (!($status && $status['running'])) {
32+
$error = "Server is not running\n";
33+
break;
34+
}
35+
// Success, Connected to servers
36+
if ($fp) {
37+
$error = '';
38+
break;
39+
}
40+
}
41+
42+
if ($fp) {
43+
fclose($fp);
44+
}
45+
46+
if ($error) {
47+
echo $error;
48+
proc_terminate($handle);
49+
exit(1);
50+
}
51+
52+
register_shutdown_function(
53+
function($handle) {
54+
proc_terminate($handle);
55+
},
56+
$handle
57+
);
58+
59+
return $handle;
60+
}
61+
62+
function memcached_server_stop($handle) {
63+
$success = FALSE;
64+
if ($handle) {
65+
proc_terminate($handle);
66+
/* Wait for server to shutdown */
67+
for ($i = 0; $i < 60; $i++) {
68+
$status = proc_get_status($handle);
69+
if (!($status && $status['running'])) {
70+
$success = TRUE;
71+
break;
72+
}
73+
usleep(50000);
74+
}
75+
}
76+
return $success;
77+
}
78+

tests/server.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
$server = new MemcachedServer();
4+
5+
$server->on (Memcached::ON_CONNECT,
6+
function ($remote_addr) {
7+
echo "Incoming connection from {$remote_addr}" . PHP_EOL;
8+
return Memcached::RESPONSE_SUCCESS;
9+
});
10+
11+
$server->on (Memcached::ON_ADD,
12+
function ($client_id, $key, $value, $flags, $expiration, &$cas) {
13+
echo "client_id=[$client_id]: Add key=[$key], value=[$value], flags=[$flags], expiration=[$expiration]" . PHP_EOL;
14+
$cas = 15;
15+
return Memcached::RESPONSE_SUCCESS;
16+
});
17+
18+
$server->on (Memcached::ON_APPEND,
19+
function ($client_id, $key, $value, $cas, &$result_cas) {
20+
echo "client_id=[$client_id]: Append key=[$key], value=[$value], cas=[$cas]" . PHP_EOL;
21+
return Memcached::RESPONSE_SUCCESS;
22+
});
23+
24+
$server->on (Memcached::ON_PREPEND,
25+
function ($client_id, $key, $value, $cas, &$result_cas) {
26+
echo "client_id=[$client_id]: Prepend key=[$key], value=[$value], cas=[$cas]" . PHP_EOL;
27+
return Memcached::RESPONSE_SUCCESS;
28+
});
29+
30+
$server->on (Memcached::ON_INCREMENT,
31+
function ($client_id, $key, $delta, $initial, $expiration, &$result, &$result_cas) {
32+
echo "client_id=[$client_id]: Incrementing key=[$key], delta=[$delta], initial=[$initial], expiration=[$expiration]" . PHP_EOL;
33+
return Memcached::RESPONSE_SUCCESS;
34+
});
35+
36+
$server->on (Memcached::ON_DECREMENT,
37+
function ($client_id, $key, $delta, $initial, $expiration, &$result, &$result_cas) {
38+
echo "client_id=[$client_id]: Decrementing key=[$key], delta=[$delta], initial=[$initial], expiration=[$expiration]" . PHP_EOL;
39+
return Memcached::RESPONSE_SUCCESS;
40+
});
41+
42+
$server->on (Memcached::ON_DELETE,
43+
function ($client_id, $key, $cas) {
44+
echo "client_id=[$client_id]: Delete key=[$key], cas=[$cas]" . PHP_EOL;
45+
return Memcached::RESPONSE_SUCCESS;
46+
});
47+
48+
$server->on (Memcached::ON_FLUSH,
49+
function ($client_id, $when) {
50+
echo "client_id=[$client_id]: Flush when=[$when]" . PHP_EOL;
51+
return Memcached::RESPONSE_SUCCESS;
52+
});
53+
54+
$server->on (Memcached::ON_GET,
55+
function ($client_id, $key, &$value, &$flags, &$cas) {
56+
echo "client_id=[$client_id]: Get key=[$key]" . PHP_EOL;
57+
$value = "Hello to you client!";
58+
return Memcached::RESPONSE_SUCCESS;
59+
});
60+
61+
$server->on (Memcached::ON_NOOP,
62+
function ($client_id) {
63+
echo "client_id=[$client_id]: Noop" . PHP_EOL;
64+
return Memcached::RESPONSE_SUCCESS;
65+
});
66+
67+
$server->on (Memcached::ON_REPLACE,
68+
function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) {
69+
echo "client_id=[$client_id]: Replace key=[$key], value=[$value], flags=[$flags], expiration=[$expiration], cas=[$cas]" . PHP_EOL;
70+
return Memcached::RESPONSE_SUCCESS;
71+
});
72+
73+
$server->on (Memcached::ON_SET,
74+
function ($client_id, $key, $value, $flags, $expiration, $cas, &$result_cas) {
75+
echo "client_id=[$client_id]: Set key=[$key], value=[$value], flags=[$flags], expiration=[$expiration], cas=[$cas]" . PHP_EOL;
76+
return Memcached::RESPONSE_SUCCESS;
77+
});
78+
79+
$server->on (Memcached::ON_STAT,
80+
function ($client_id, $key, &$value) {
81+
echo "client_id=[$client_id]: Stat key=[$key]" . PHP_EOL;
82+
$value = "Stat reply";
83+
return Memcached::RESPONSE_SUCCESS;
84+
});
85+
86+
$server->on (Memcached::ON_QUIT,
87+
function ($client_id) {
88+
echo "client_id=[$client_id]: Client quit" . PHP_EOL;
89+
return Memcached::RESPONSE_SUCCESS;
90+
});
91+
92+
$addr = ($_SERVER['argv'][1] ?? "127.0.0.1:3434");
93+
echo "Listening on $addr" . PHP_EOL;
94+
$server->run($addr);

0 commit comments

Comments
 (0)