-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.php
More file actions
95 lines (78 loc) · 2.43 KB
/
tests.php
File metadata and controls
95 lines (78 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* LockManager tests.
*
* Run this script in console in concurrent mode (many instances in parallel).
* Tests completed successful if `Tests failed` value is 0 in all runs.
*
* (c) Valera Leontyev (feedbee), 2013
* All LockManager code published under BSD 3-Clause License http://choosealicense.com/licenses/bsd-3-clause/
*/
require_once('LockManager.php');
require_once('Driver/DriverInterface.php');
require_once('Driver/Flock.php');
require_once('Driver/Redis.php');
require_once('Driver/Memcached.php');
use LockManager\LockManager;
use LockManager\Driver\Memcached as MemcachedDriver;
use LockManager\Driver\Redis as RedisDriver;
use LockManager\Driver\Flock as FlockDriver;
$opt = getopt('mrfsn');
if (isset($opt['m'])) {
print "LockManager: Memcached back-end test" . PHP_EOL;
$memcached = new \Memcached;
$memcached->addServer('127.0.0.1', 11211);
$backend = new \LockManager\Driver\Memcached($memcached);
}
else if (isset($opt['r'])) {
print "LockManager: Redis back-end test" . PHP_EOL;
$redis = new \Redis;
$redis->connect('127.0.0.1');
$backend = new RedisDriver($redis);
}
else if (isset($opt['f'])) {
print "LockManager: Flock back-end test" . PHP_EOL;
$backend = new FlockDriver;
}
else {
die('Set back-end parameter: -m, -r, or -f' . PHP_EOL);
}
$block = !isset($opt['n']);
print 'Block mode: ' . ($block ? 'BLOCK' : 'NOT BLOCK') . PHP_EOL;
$lockManager = new LockManager($backend);
if (isset($opt['s'])) {
// Run simple test (for debug purpose)
echo 'Before lock' . PHP_EOL;
var_dump($lockManager->lock('test', $block));
echo 'After lock' . PHP_EOL;
sleep(5);
echo 'Before release' . PHP_EOL;;
var_dump($lockManager->release('test'));
echo 'After release' . PHP_EOL;
exit;
}
// Run standard test
print "Test is infinitive and must be interrupter with system termination signal (CTRL+C)" . PHP_EOL;
$sysTmpDir = sys_get_temp_dir();
$testFilePath = "{$sysTmpDir}/lock-test-file";
$fd = fopen($testFilePath, "w+");
$all = $acquired = $failed = 0;
do {
print "Tests launched: {$all}\tLock acquired: {$acquired}\tTests failed: {$failed}\r";
if ($success = $lockManager->lock('test-key', $block)) {
$acquired++;
if (!flock($fd, LOCK_EX)) {
$failed++;
continue;
}
usleep(120000);
flock($fd, LOCK_UN);
$lockManager->release('test-key');
}
usleep(rand(500, 50000));
$all++;
}
while (1);
fclose($fd);
unlink($testFilePath);
// Test is infinitive and must be interrupter with system termination signal (CTRL+C)