1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/test/LibeventReactorTest.php

278 lines
8.1 KiB
PHP
Raw Normal View History

2013-08-05 16:05:08 -04:00
<?php
2014-03-06 09:52:31 -05:00
namespace Alert;
2013-08-05 16:05:08 -04:00
2014-03-06 09:52:31 -05:00
class LibeventReactorTest extends \PHPUnit_Framework_TestCase {
2013-08-05 16:05:08 -04:00
private function skipIfMissingExtLibevent() {
if (!extension_loaded('libevent')) {
$this->markTestSkipped(
2014-03-06 09:52:31 -05:00
'ext/libevent extension not loaded'
2013-08-05 16:05:08 -04:00
);
}
}
2014-03-06 09:52:31 -05:00
public function testEnablingWatcherAllowsSubsequentInvocation() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$watcherId = $reactor->once(function() use (&$testIncrement) {
$testIncrement++;
}, $delay = 0);
$reactor->disable($watcherId);
$reactor->once(function() use ($reactor) {
$reactor->stop();
}, $delay = 0.01);
$reactor->run();
$this->assertEquals(0, $testIncrement);
$reactor->enable($watcherId);
$reactor->once(function() use ($reactor) {
$reactor->stop();
}, $delay = 0.01);
$reactor->run();
2013-08-05 16:05:08 -04:00
$this->assertEquals(1, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testDisablingWatcherPreventsSubsequentInvocation() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$watcherId = $reactor->once(function() use (&$testIncrement) {
$testIncrement++;
}, $delay = 0);
$reactor->disable($watcherId);
$reactor->once(function() use ($reactor) {
$reactor->stop();
}, $delay = 0.01);
$reactor->run();
$this->assertEquals(0, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testUnresolvedEventsAreReenabledOnRunFollowingPreviousStop() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$reactor->once(function() use (&$testIncrement, $reactor) {
$testIncrement++;
$reactor->stop();
}, $delay = 0.1);
$reactor->immediately(function() use ($reactor) {
$reactor->stop();
});
$reactor->run();
$this->assertEquals(0, $testIncrement);
usleep(150000);
$reactor->run();
$this->assertEquals(1, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testImmediateExecution() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$reactor->immediately(function() use (&$testIncrement) {
$testIncrement++;
});
$reactor->tick();
$this->assertEquals(1, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testTickExecutesReadyEvents() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$reactor->once(function() use (&$testIncrement) {
$testIncrement++;
}, $delay = 0);
$reactor->tick();
$this->assertEquals(1, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testRunExecutesEventsUntilExplicitlyStopped() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$testIncrement = 0;
$reactor->repeat(function() use (&$testIncrement, $reactor) {
if ($testIncrement < 10) {
$testIncrement++;
} else {
$reactor->stop();
}
}, $delay = 0.001);
2013-08-05 16:05:08 -04:00
$reactor->run();
$this->assertEquals(10, $testIncrement);
}
2014-03-06 09:52:31 -05:00
public function testOnceReturnsEventWatcher() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$firstWatcherId = (PHP_INT_MAX * -1) + 1;
2013-08-05 16:05:08 -04:00
$watcherId = $reactor->once(function(){}, $delay = 0);
$this->assertSame($firstWatcherId, $watcherId);
2013-08-05 16:05:08 -04:00
$watcherId = $reactor->once(function(){}, $delay = 0);
$this->assertSame($firstWatcherId + 1, $watcherId);
2013-08-05 16:05:08 -04:00
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage test
*/
2014-03-06 09:52:31 -05:00
public function testReactorAllowsExceptionToBubbleUpDuringTick() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$reactor->once(function(){ throw new RuntimeException('test'); }, $delay = 0);
$reactor->tick();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage test
*/
2014-03-06 09:52:31 -05:00
public function testReactorAllowsExceptionToBubbleUpDuringRun() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$reactor->once(function(){ throw new RuntimeException('test'); }, $delay = 0);
$reactor->run();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage test
*/
2014-03-06 09:52:31 -05:00
public function testReactorAllowsExceptionToBubbleUpFromRepeatingAlarmDuringRun() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$reactor->repeat(function(){ throw new RuntimeException('test'); }, $interval = 0);
$reactor->run();
}
2014-03-06 09:52:31 -05:00
public function testRepeatReturnsEventWatcher() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$firstWatcherId = (PHP_INT_MAX * -1) + 1;
2013-08-05 16:05:08 -04:00
$watcherId = $reactor->repeat(function(){}, $interval = 1);
$this->assertSame($firstWatcherId, $watcherId);
2013-08-05 16:05:08 -04:00
}
2014-03-06 09:52:31 -05:00
public function testCancelRemovesWatcher() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$watcherId = $reactor->once(function(){
$this->fail('Watcher was not cancelled as expected');
}, $delay = 0.001);
$reactor->once(function() use ($reactor, $watcherId) { $reactor->cancel($watcherId); }, $delay = 0);
$reactor->once(function() use ($reactor) { $reactor->stop(); }, $delay = 0.002);
$reactor->run();
}
2014-03-06 09:52:31 -05:00
public function testOnWritableWatcher() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$flag = FALSE;
$reactor->onWritable(STDOUT, function() use ($reactor, &$flag) {
$flag = TRUE;
$reactor->stop();
});
$reactor->once(function() use ($reactor) { $reactor->stop(); }, 0.05);
$reactor->run();
$this->assertTrue($flag);
}
2014-03-06 09:52:31 -05:00
public function testInitiallyDisabledWriteWatcher() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$increment = 0;
$reactor->onWritable(STDOUT, function() use (&$increment) { $increment++; }, $isEnabled = FALSE);
$reactor->once(function() use ($reactor) { $reactor->stop(); }, 0.05);
$reactor->run();
$this->assertSame(0, $increment);
}
2014-03-06 09:52:31 -05:00
public function testInitiallyDisabledWriteWatcherIsTriggeredOnceEnabled() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$increment = 0;
$watcherId = $reactor->onWritable(STDOUT, function() use (&$increment) {
$increment++;
}, $isEnabled = FALSE);
$reactor->once(function() use ($reactor, $watcherId) {
$reactor->enable($watcherId);
}, 0.01);
$reactor->once(function() use ($reactor) { $reactor->stop(); }, 0.1);
$reactor->run();
$this->assertTrue($increment > 0);
}
/**
* @expectedException RuntimeException
*/
2014-03-06 09:52:31 -05:00
public function testStreamWatcherDoesntSwallowExceptions() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor;
$reactor->onWritable(STDOUT, function() { throw new RuntimeException; });
$reactor->once(function() use ($reactor) { $reactor->stop(); }, 0.05);
$reactor->run();
}
2014-03-06 09:52:31 -05:00
public function testGarbageCollection() {
2013-08-05 16:05:08 -04:00
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor();
$reactor->once(function() use ($reactor) { $reactor->stop(); }, 0.1);
$reactor->run();
}
2014-03-06 09:52:31 -05:00
public function testAfterForkReinitializesWatchers() {
$this->skipIfMissingExtLibevent();
$reactor = new LibeventReactor();
$reactor->onWritable(STDOUT, function() use ($reactor) { $reactor->stop(); });
$reactor->beforeFork();
$reactor->afterFork();
2013-08-05 16:05:08 -04:00
$reactor->run();
}
}