Skip to content

Commit 54d524b

Browse files
committed
Fix tests
1 parent e3cbe83 commit 54d524b

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

lib/Loop.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace danog\Loop;
1212

13+
use AssertionError;
1314
use Revolt\EventLoop;
1415
use Stringable;
1516

@@ -65,8 +66,9 @@ public function start(): bool
6566
return false;
6667
}
6768
$this->running = true;
68-
$this->paused = true;
69-
\assert($this->resume());
69+
if (!$this->resume()) {
70+
throw new AssertionError("Could not resume!");
71+
}
7072
$this->startedLoop();
7173
return true;
7274
}
@@ -101,8 +103,12 @@ abstract protected function loop(): ?float;
101103
private bool $paused = true;
102104
private function loopInternal(): void
103105
{
104-
\assert($this->running);
105-
\assert($this->paused);
106+
if (!$this->running) {
107+
throw new AssertionError("Already running!");
108+
}
109+
if (!$this->paused) {
110+
throw new AssertionError("Already paused!");
111+
}
106112
$this->paused = false;
107113
try {
108114
$timeout = $this->loop();
@@ -125,7 +131,9 @@ private function loopInternal(): void
125131
$this->reportPause(0.0);
126132
} else {
127133
if (!$this->resumeImmediate) {
128-
\assert($this->resumeTimer === null);
134+
if ($this->resumeTimer !== null) {
135+
throw new AssertionError("Already have a resume timer!");
136+
}
129137
$this->resumeTimer = EventLoop::delay($timeout, function (): void {
130138
$this->resumeTimer = null;
131139
$this->loopInternal();
@@ -138,8 +146,13 @@ private function loopInternal(): void
138146
private function exitedLoopInternal(): void
139147
{
140148
$this->running = false;
141-
\assert($this->resumeTimer === null);
142-
\assert($this->resumeImmediate === null);
149+
$this->paused = true;
150+
if ($this->resumeTimer !== null) {
151+
throw new AssertionError("Already have a resume timer!");
152+
}
153+
if ($this->resumeTimer !== null) {
154+
throw new AssertionError("Already have a resume immediate timer!");
155+
}
143156
$this->exitedLoop();
144157
}
145158
/**

test/GenericTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
149149
$this->assertEquals($expectedRunCount, $runCount);
150150
$this->assertEquals(1, $loop->getPauseCount());
151151
$this->assertEquals(0, $loop->getLastPause());
152+
$this->assertTrue($loop->isPaused());
152153

153154
$pauseTime = 0.1;
154155
$this->assertTrue($loop->resume());
@@ -159,13 +160,15 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
159160
$this->assertEquals($expectedRunCount, $runCount);
160161
$this->assertEquals(2, $loop->getPauseCount());
161162
$this->assertEquals(0.1, $loop->getLastPause());
163+
$this->assertTrue($loop->isPaused());
162164

163165
delay(0.048);
164166
$this->fixtureStarted($loop);
165167

166168
$this->assertEquals($expectedRunCount, $runCount);
167169
$this->assertEquals(2, $loop->getPauseCount());
168170
$this->assertEquals(0.1, $loop->getLastPause());
171+
$this->assertTrue($loop->isPaused());
169172

170173
delay(0.060);
171174
$this->fixtureStarted($loop);
@@ -174,6 +177,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
174177
$this->assertEquals($expectedRunCount, $runCount);
175178
$this->assertEquals(3, $loop->getPauseCount());
176179
$this->assertEquals(0.1, $loop->getLastPause());
180+
$this->assertTrue($loop->isPaused());
177181

178182
$this->assertTrue($loop->resume());
179183
self::waitTick();
@@ -182,6 +186,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
182186
$this->assertEquals($expectedRunCount, $runCount);
183187
$this->assertEquals(4, $loop->getPauseCount());
184188
$this->assertEquals(0.1, $loop->getLastPause());
189+
$this->assertTrue($loop->isPaused());
185190

186191
if ($stopSig) {
187192
$this->assertTrue($loop->stop());
@@ -194,6 +199,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
194199
$this->assertEquals($expectedRunCount, $runCount);
195200
$this->assertEquals(4, $loop->getPauseCount());
196201
$this->assertEquals(0.1, $loop->getLastPause());
202+
$this->assertTrue($loop->isPaused());
197203

198204
$this->assertEquals(1, $loop->startCounter());
199205
$this->assertEquals(1, $loop->endCounter());
@@ -212,6 +218,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
212218
$this->assertEquals($expectedRunCount, $runCount);
213219
$this->assertEquals(5, $loop->getPauseCount());
214220
$this->assertEquals(0.0, $loop->getLastPause());
221+
$this->assertTrue($loop->isPaused());
215222

216223
if ($stopSig) {
217224
$this->assertTrue($loop->stop());
@@ -224,6 +231,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
224231
$this->assertEquals($expectedRunCount, $runCount);
225232
$this->assertEquals(5, $loop->getPauseCount());
226233
$this->assertEquals(0.0, $loop->getLastPause());
234+
$this->assertTrue($loop->isPaused());
227235

228236
$this->assertEquals(2, $loop->startCounter());
229237
$this->assertEquals(2, $loop->endCounter());
@@ -241,6 +249,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
241249
$this->assertEquals($expectedRunCount, $runCount);
242250
$this->assertEquals(5, $loop->getPauseCount());
243251
$this->assertEquals(0.0, $loop->getLastPause());
252+
$this->assertTrue($loop->isPaused());
244253

245254
$this->assertEquals(3, $loop->startCounter());
246255
$this->assertEquals(3, $loop->endCounter());
@@ -258,6 +267,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
258267
$this->assertEquals($expectedRunCount, $runCount);
259268
$this->assertEquals(5, $loop->getPauseCount());
260269
$this->assertEquals(0.0, $loop->getLastPause());
270+
$this->assertTrue($loop->isPaused());
261271

262272
$this->assertEquals(4, $loop->startCounter());
263273
$this->assertEquals(4, $loop->endCounter());

test/LoopTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function assertAfterStart(BasicInterface&Loop $loop, bool $running = t
5858
$this->assertEquals($running ? 0 : 1, $loop->endCounter());
5959

6060
$this->assertEquals($running, !$loop->start());
61+
$this->assertEquals($running, !$loop->isPaused());
6162
}
6263
/**
6364
* Execute final assertions.
@@ -140,5 +141,7 @@ public function testException(): void
140141
$this->assertEquals(1, $loop->endCounter());
141142

142143
$this->assertInstanceOf(RuntimeException::class, $e_thrown);
144+
145+
EventLoop::setErrorHandler(null);
143146
}
144147
}

test/PeriodicTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,27 @@ private function fixtureAssertions(callable $closure, int &$runCount, bool &$ret
141141
$this->fixtureStarted($loop);
142142
self::waitTick();
143143

144+
$this->assertTrue($loop->isPaused());
144145
$this->assertEquals(1, $runCount);
145146

146147
$this->assertEquals($loop, $l);
147148

148149
delay(0.048);
149150
$this->fixtureStarted($loop);
150151

152+
$this->assertTrue($loop->isPaused());
151153
$this->assertEquals(1, $runCount);
152154

153155
delay(0.060);
154156
$this->fixtureStarted($loop);
155157

158+
$this->assertTrue($loop->isPaused());
156159
$this->assertEquals(2, $runCount);
157160

158161
$this->assertTrue($loop->resume());
159162
self::waitTick();
160163

164+
$this->assertTrue($loop->isPaused());
161165
$this->assertEquals(3, $runCount);
162166

163167
if ($stopSig) {
@@ -169,6 +173,7 @@ private function fixtureAssertions(callable $closure, int &$runCount, bool &$ret
169173
self::waitTick();
170174
$this->assertEquals($stopSig ? 3 : 4, $runCount);
171175

176+
$this->assertTrue($loop->isPaused());
172177
$this->assertFalse($loop->isRunning());
173178

174179
$this->assertEquals(1, $loop->startCounter());

0 commit comments

Comments
 (0)