-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLoopTest.php
179 lines (152 loc) · 4.67 KB
/
LoopTest.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php declare(strict_types=1);
/**
* Loop test.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/MIT MIT
*/
namespace danog\Loop\Test;
use danog\Loop\Loop;
use danog\Loop\Test\Interfaces\BasicInterface;
use danog\Loop\Test\Traits\Basic;
use danog\Loop\Test\Traits\BasicException;
use Revolt\EventLoop;
use RuntimeException;
use function Amp\delay;
class LoopTest extends Fixtures
{
/**
* Execute pre-start assertions.
*/
private function assertPreStart(BasicInterface&Loop $loop): void
{
$this->assertEquals(self::LOOP_NAME, "$loop");
$this->assertFalse($loop->isRunning());
$this->assertFalse($loop->ran());
$this->assertFalse($loop->inited());
$this->assertEquals(0, $loop->startCounter());
$this->assertEquals(0, $loop->endCounter());
}
/**
* Execute after-start assertions.
*/
private function assertAfterStart(BasicInterface&Loop $loop, int $prevRun = 0): void
{
self::waitTick();
$this->assertTrue($loop->inited());
if ($prevRun === 0) {
$this->assertFalse($loop->ran());
} else {
$this->assertTrue($loop->ran());
}
$this->assertTrue($loop->isRunning());
$this->assertEquals($prevRun+1, $loop->startCounter());
$this->assertEquals($prevRun, $loop->endCounter());
$this->assertFalse($loop->start());
$this->assertFalse($loop->isPaused());
}
/**
* Execute final assertions.
*/
private function assertFinal(BasicInterface&Loop $loop, int $count = 1): void
{
$this->assertTrue($loop->ran());
$this->assertFalse($loop->isRunning());
$this->assertTrue($loop->inited());
$this->assertEquals($count, $loop->startCounter());
$this->assertEquals($count, $loop->endCounter());
}
/**
* Test basic loop.
*/
public function testLoop(): void
{
$loop = new class() extends Loop implements BasicInterface {
use Basic;
};
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
delay(0.110);
$this->assertFinal($loop);
}
/**
* Test basic loop.
*/
public function testLoopStopFromOutside(): void
{
$loop = new class() extends Loop implements BasicInterface {
use Basic;
/**
* Loop implementation.
*/
public function loop(): ?float
{
$this->inited = true;
delay(0.1);
$this->ran = true;
return 1000.0;
}
};
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
$this->assertTrue($loop->stop());
delay(0.110);
$this->assertFinal($loop);
}
/**
* Test basic loop.
*/
public function testLoopStopFromOutsideRestart(): void
{
$loop = new class() extends Loop implements BasicInterface {
use Basic;
/**
* Loop implementation.
*/
public function loop(): ?float
{
$this->inited = true;
delay(0.1);
$this->ran = true;
return 1000.0;
}
};
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
$this->assertAfterStart($loop);
EventLoop::queue(function () use ($loop): void {
$this->assertTrue($loop->stop());
});
self::waitTick();
$this->assertTrue($loop->start());
$this->assertAfterStart($loop, 1);
$this->assertTrue($loop->stop());
delay(0.110);
$this->assertFinal($loop, 2);
}
/**
* Test basic exception in loop.
*/
public function testException(): void
{
$loop = new class() extends Loop implements BasicInterface {
use BasicException;
};
$e_thrown = null;
EventLoop::setErrorHandler(function (\RuntimeException $e) use (&$e_thrown): void {
$e_thrown = $e;
});
$this->assertPreStart($loop);
$this->assertTrue($loop->start());
self::waitTick();
$this->assertFalse($loop->isRunning());
$this->assertTrue($loop->inited());
$this->assertEquals(1, $loop->startCounter());
$this->assertEquals(1, $loop->endCounter());
$this->assertInstanceOf(RuntimeException::class, $e_thrown);
EventLoop::setErrorHandler(null);
}
}