BootTest.php
1.3 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
<?php
namespace AukeySwrpcTests;
use PHPUnit\Framework\TestCase;
/**
* Class BootTest
*
* @author wuzhc
* @internal
*/
abstract class BootTest extends TestCase
{
const PID_FILE = __DIR__ . '/AukeySwrpc.pid';
const SERVER_LOG = __DIR__ . '/AukeySwrpc.log';
const SERVER_SCRIPT = __DIR__ . '/server.sh';
public static function setUpBeforeClass(): void
{
// fwrite(STDOUT, 'Starting rpc server...' . PHP_EOL);
$cmd = 'nohup ' . self::SERVER_SCRIPT . ' > ' . self::SERVER_LOG . ' 2>&1 &';
shell_exec($cmd);
sleep(5);
self::assertFileExists(self::PID_FILE, 'Run rpc server failed: ' . $cmd . '');
$pid = file_get_contents(self::PID_FILE);
self::assertNotEmpty($pid, 'Failed to start the rpc server.');
$res = shell_exec('ps aux | grep ' . $pid . ' | wc -l');
self::assertGreaterThanOrEqual(1, intval($res), 'Failed to start the rpc server.');
// fwrite(STDOUT, 'Rpc server started successfully.' . PHP_EOL);
}
public static function tearDownAfterClass(): void
{
if (\file_exists(self::PID_FILE)) {
$pid = file_get_contents(self::PID_FILE);
\shell_exec('kill -15 ' . $pid);
if (\file_exists(self::PID_FILE)) {
\unlink(self::PID_FILE);
}
\sleep(1);
}
}
}