|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: danchukas |
|
5
|
|
|
* Date: 2017-07-15 01:03 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace DanchukAS\DenyMultiplyRunTest; |
|
9
|
|
|
|
|
10
|
|
|
use DanchukAS\DenyMultiplyRun\DenyMultiplyRun; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
class ExistRightPidFileTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
private static $lastError; |
|
17
|
|
|
|
|
18
|
|
|
private static $existFileName; |
|
19
|
|
|
|
|
20
|
|
|
function setUp() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
self::$existFileName = tempnam(sys_get_temp_dir(), 'vo_'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
function tearDown() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
28
|
|
|
@unlink(self::$existFileName); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
function testEmptyFile() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
self::waitError(); |
|
34
|
|
|
DenyMultiplyRun::setPidFile(self::$existFileName); |
|
35
|
|
|
$wait_error = "pid-file exist, but file empty. pid-file updated with pid this process: %i"; |
|
36
|
|
|
self::catchError($wait_error); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private static function waitError() |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** @noinspection PhpUnusedParameterInspection */ |
|
43
|
|
|
set_error_handler(function (int $messageType, string $messageText) { |
|
44
|
|
|
self::$lastError = $messageText; |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
self::$lastError = null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* перевіряє чи помилка відбулась, і саме та яка очікувалась. |
|
52
|
|
|
* @param string $message |
|
53
|
|
|
*/ |
|
54
|
|
|
private static function catchError(string $message) |
|
55
|
|
|
{ |
|
56
|
|
|
restore_error_handler(); |
|
57
|
|
|
self::assertStringMatchesFormat("$message", self::$lastError); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @expectedException \DanchukAS\DenyMultiplyRun\Exception\ProcessExisted |
|
62
|
|
|
*/ |
|
63
|
|
|
function testExistedPid() |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
$file_name = self::$existFileName; |
|
67
|
|
|
file_put_contents($file_name, getmypid()); |
|
68
|
|
|
|
|
69
|
|
|
DenyMultiplyRun::setPidFile($file_name); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
function testNoExistedPid() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
|
|
75
|
|
|
$no_exist_pid = 1; |
|
76
|
|
|
while (++$no_exist_pid < PHP_INT_MAX) { |
|
77
|
|
|
if (false === posix_kill($no_exist_pid, 0) |
|
78
|
|
|
&& 3 === posix_get_last_error() |
|
79
|
|
|
) { |
|
80
|
|
|
break; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
file_put_contents(self::$existFileName, $no_exist_pid); |
|
84
|
|
|
|
|
85
|
|
|
self::waitError(); |
|
86
|
|
|
DenyMultiplyRun::setPidFile(self::$existFileName); |
|
87
|
|
|
$wait_error = "pid-file exist, but process with contained ID(%i) in it is not exist. " |
|
88
|
|
|
. "pid-file updated with pid this process: %i"; |
|
89
|
|
|
self::catchError($wait_error); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function testLockedFile() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$file_resource = fopen(self::$existFileName, "r+"); |
|
95
|
|
|
flock($file_resource,LOCK_EX); |
|
96
|
|
|
|
|
97
|
|
|
self::expectException("DanchukAS\DenyMultiplyRun\Exception\LockFileFail"); |
|
|
|
|
|
|
98
|
|
|
DenyMultiplyRun::setPidFile(self::$existFileName); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private, and only raise it toprotectedif a sub-class needs to have access, orpublicif an external class needs access.