Skip to content

Commit

Permalink
more tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Izumi-kun committed Nov 9, 2017
1 parent 5adbcdd commit 41097b7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ public function trigger()
{
$filePath = $this->_filePath;
$tries = 4;

$lastError = null;
$state = null;
set_error_handler(function () use (&$lastError) {
$lastError = func_get_arg(1);
});
while ($tries > 0) {
$tries--;

Expand Down Expand Up @@ -128,11 +132,17 @@ public function trigger()

if (touch($filePath, $state)) {
$this->_state = $state;
return $state;
$lastError = null;
break;
}
}
restore_error_handler();

if ($lastError === null) {
return $state;
}

Yii::warning("Unable to trigger event '{$this->_key}'", __METHOD__);
Yii::warning("Unable to trigger event '{$this->_key}': {$lastError}", __METHOD__);

return null;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use izumi\longpoll\Event;
use Symfony\Component\Process\PhpProcess;
use Yii;
use yii\helpers\FileHelper;

class EventTest extends EventTestCase
{
Expand Down Expand Up @@ -49,6 +50,14 @@ public function testTriggerWithFileLock()
$this->assertNotNull($result);
}

public function testTriggerWithRemovedDir()
{
$event = new Event(['key' => 'rmDirTest']);
FileHelper::removeDirectory(Yii::getAlias('@runtime/events'));
$result = $event->trigger();
$this->assertNull($result);
}

/**
* @depends testStaticTriggerByKey
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use izumi\longpoll\widgets\LongPoll;
use Symfony\Component\Process\Process;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\Url;
use yii\httpclient\Client;
use yii\httpclient\CurlTransport;
Expand Down Expand Up @@ -114,6 +116,13 @@ public function testAddEvent()
$this->assertArrayHasKey('addTest2', $server->eventCollection->getEvents());
}

public function testAddEventInvalidState()
{
$server = new Server();
$this->expectException(InvalidParamException::class);
$server->addEvent('addTest2', '123');
}

public function sendDataProvider()
{
return [
Expand Down Expand Up @@ -160,6 +169,13 @@ public function testSend($delay)
$this->expectOutputString($expectedResponse);
}

public function testSendWithoutEvents()
{
$server = new Server();
$this->expectException(InvalidConfigException::class);
$server->send();
}

/**
* @depends testSend
*/
Expand Down

0 comments on commit 41097b7

Please sign in to comment.