Skip to content

Commit 45f673c

Browse files
committed
Fix tests.
1 parent 9ed21cc commit 45f673c

File tree

6 files changed

+259
-7
lines changed

6 files changed

+259
-7
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
require_once dirname(dirname(__DIR__)).'/bootstrap.php';
3+
4+
class TcpServer
5+
{
6+
/**
7+
* @var \swoole_server
8+
*/
9+
public $swooleServer;
10+
11+
public function __construct()
12+
{
13+
$this->swooleServer = new \swoole_server('127.0.0.1', 9001, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
14+
$this->swooleServer->set([
15+
// "buffer_output_size" => 1024 * 1024 * 1024, // 输出限制
16+
17+
'log_file' => TEST_LOG_FILE,
18+
'max_connection' => 10240,
19+
'pipe_buffer_size' => 1024 * 1024 * 1024,
20+
21+
'dispatch_mode' => 3,
22+
'open_tcp_nodelay' => 1,
23+
'open_cpu_affinity' => 1,
24+
'daemonize' => 0,
25+
'reactor_num' => 1,
26+
'worker_num' => 2,
27+
'max_request' => 100000,
28+
]);
29+
}
30+
31+
public function start()
32+
{
33+
$this->swooleServer->on('start', [$this, 'onStart']);
34+
$this->swooleServer->on('shutdown', [$this, 'onShutdown']);
35+
36+
$this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
37+
$this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
38+
$this->swooleServer->on('workerError', [$this, 'onWorkerError']);
39+
40+
$this->swooleServer->on('connect', [$this, 'onConnect']);
41+
$this->swooleServer->on('receive', [$this, 'onReceive']);
42+
43+
$this->swooleServer->on('close', [$this, 'onClose']);
44+
45+
$this->swooleServer->start();
46+
}
47+
48+
public function onConnect()
49+
{
50+
// print("connecting ......");
51+
}
52+
53+
public function onClose()
54+
{
55+
// print("closing .....");
56+
}
57+
58+
public function onStart(swoole_server $swooleServer)
59+
{
60+
// print("swoole_server starting .....");
61+
}
62+
63+
public function onShutdown(swoole_server $swooleServer)
64+
{
65+
// print("swoole_server shutdown .....");
66+
}
67+
68+
public function onWorkerStart(swoole_server $swooleServer, $workerId)
69+
{
70+
// print("worker #$workerId starting .....");
71+
}
72+
73+
public function onWorkerStop(swoole_server $swooleServer, $workerId)
74+
{
75+
// print("worker #$workerId stopping ....");
76+
}
77+
78+
public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
79+
{
80+
// print("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
81+
}
82+
83+
public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
84+
{
85+
static $i;
86+
if ($i > 20000)
87+
{
88+
$swooleServer->close($fd);
89+
$swooleServer->shutdown();
90+
@unlink(__DIR__ . '/swoole.log');
91+
}
92+
else
93+
{
94+
$swooleServer->send($fd, $data);
95+
}
96+
$i++;
97+
}
98+
}
99+
100+
(new TcpServer())->start();

tests/include/lib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/composer.lock
22
/vendor/
3+
!/misc/**/*

tests/include/lib/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"autoload": {
66
"classmap": [
7-
"misc/WebsocketClient.php"
7+
"misc"
88
]
99
}
1010
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Swoole\Bug\Redis;
4+
5+
Class SQLPool
6+
{
7+
private static $instance;
8+
9+
public static function init()
10+
{
11+
self::$instance = new self;
12+
}
13+
14+
/**
15+
* @return \SplQueue
16+
*/
17+
public static function i(string $name): \SplQueue
18+
{
19+
return self::$instance->$name ?? (self::$instance->$name = new \SplQueue);
20+
}
21+
22+
public static function release()
23+
{
24+
self::$instance = null;
25+
}
26+
27+
}
28+
29+
Class Redis
30+
{
31+
public $is_in_pool = false;
32+
33+
public $name;
34+
35+
/**
36+
* @var \Swoole\Coroutine\Redis
37+
*/
38+
public $client;
39+
40+
public static function i(array $options)
41+
{
42+
$name = 'redis_' . $options['host'] . ':' . $options['port'];
43+
$pool = SQLPool::i($name);
44+
/**@var $redis Redis */
45+
if (count($pool) > 0 && ($redis = $pool->shift()) && $redis->isConnect()) {
46+
//满足 1.会话池里有空闲连接 2.返回了一个非空连接 3.Redis没有超时时间
47+
$redis->is_in_pool = false;
48+
return $redis;
49+
}
50+
return new self($name, $options);
51+
}
52+
53+
public static function main(): self
54+
{
55+
return self::i([
56+
'host' => REDIS_SERVER_HOST,
57+
'port' => REDIS_SERVER_PORT
58+
]);
59+
}
60+
61+
private function __construct(string $name, array $options)
62+
{
63+
$this->name = $name;
64+
$this->client = new \Swoole\Coroutine\Redis();
65+
if (!$this->client->connect($options['host'], $options['port'])) {
66+
new DBConnectException('[Redis: ' . $this->client->errCode . '] ' . $this->client->errMsg,
67+
$this->client->errCode);
68+
}
69+
}
70+
71+
public function isConnect(): bool
72+
{
73+
return $this->client->connected ?? false;
74+
}
75+
76+
public function __call(string $name, $params)
77+
{
78+
if ($this->is_in_pool) {
79+
throw new \BadMethodCallException('this redis client is in pool!');
80+
}
81+
$ret = call_user_func_array([$this->client, $name], $params);
82+
$this->revert();
83+
return $ret;
84+
}
85+
86+
public function revert()
87+
{
88+
SQLPool::i($this->name)->push($this);
89+
$this->is_in_pool = true;
90+
}
91+
92+
public function __destruct()
93+
{
94+
$this->client->close();
95+
}
96+
97+
}
98+
99+
class Lock
100+
{
101+
const EXPIRES = 5;
102+
103+
private $random;
104+
private $expires;
105+
private $keyMap = [];
106+
107+
public static function i(): self
108+
{
109+
return new self;
110+
}
111+
112+
public function __construct()
113+
{
114+
$this->random = substr(md5(microtime()), 0, 8);
115+
}
116+
117+
public function lock(string $key, int $expires = self::EXPIRES): bool
118+
{
119+
$this->expires = $expires;
120+
$ret = Redis::main()->set($key, $this->random, ['nx', 'ex' => $this->expires]);
121+
if ($ret) {
122+
$this->keyMap[$key] = microtime(true);
123+
}
124+
return !!$ret;
125+
}
126+
127+
public function unlock(string $key = null)
128+
{
129+
if ($key) {
130+
// unlock one
131+
if ($this->keyMap[$key] ?? false) {
132+
if ($this->keyMap[$key] < microtime(true) - $this->expires) {
133+
return; // have already expired
134+
} else {
135+
@Redis::main()->del($key);
136+
}
137+
unset($this->keyMap[$key]);
138+
}
139+
} else {
140+
// unlock all
141+
foreach ($this->keyMap as $key => $expires) {
142+
$this->unlock($key);
143+
}
144+
}
145+
}
146+
147+
public function __destruct()
148+
{
149+
$this->unlock();
150+
}
151+
152+
}

tests/swoole_http_server/callback_with_internal_function.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ $pm->run();
3131
object(Swoole\Http\Request)#%d (%d) {
3232
["fd"]=>
3333
int(1)
34-
%s"header"]=>
34+
%A"header"]=>
3535
array(3) {
3636
["host"]=>
3737
string(%d) "%s"
@@ -85,4 +85,4 @@ object(Swoole\Http\Response)#%d (%d) {
8585
NULL
8686
["trailer"]=>
8787
NULL
88-
}
88+
}

tests/swoole_redis_coro/bug_redislock.phpt renamed to tests/swoole_redis_coro/bug_lock.phpt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ require __DIR__ . '/../include/skipif.inc'; ?>
66
--FILE--
77
<?php
88
require __DIR__ . '/../include/bootstrap.php';
9-
require __DIR__ . '/../include/api/swoole_redis_coro/RedisLock.php';
109

11-
use RedisLockBug\RedisLock;
12-
use RedisLockBug\SQLPool;
10+
use Swoole\Bug\Redis\Lock;
11+
use Swoole\Bug\Redis\SQLPool;
1312

1413
SQLPool::init();
1514

1615
go(function () {
17-
$redis_lock = RedisLock::i();
16+
$redis_lock = Lock::i();
1817
for ($i = 3; $i--;) {
1918
echo "LOCK\n";
2019
if (!$redis_lock->lock('SWOOLE_TEST_LOCK')) {

0 commit comments

Comments
 (0)