Skip to content

Commit

Permalink
Add ability to set callback on socket and signal events
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Aug 12, 2015
1 parent 958681d commit bf5a680
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Loop/Events/Signal.php
Expand Up @@ -57,6 +57,14 @@ public function __invoke()
$this->call();
}

/**
* {@inheritdoc}
*/
public function setCallback(callable $callback)
{
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Loop/Events/SignalInterface.php
Expand Up @@ -21,6 +21,11 @@ public function call();
*/
public function __invoke();

/**
* @param callable $callback
*/
public function setCallback(callable $callback);

/**
* Enables listening for the signal.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Loop/Events/SocketEvent.php
Expand Up @@ -67,6 +67,14 @@ public function __invoke($expired)
$this->call($expired);
}

/**
* {@inheritdoc}
*/
public function setCallback(callable $callback)
{
$this->callback = $callback;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Loop/Events/SocketEventInterface.php
Expand Up @@ -28,6 +28,11 @@ public function call($expired);
*/
public function __invoke($expired);

/**
* @param callable $callback
*/
public function setCallback(callable $callback);

/**
* Listens for data or the ability to write.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Loop/Events/SignalTest.php
Expand Up @@ -69,6 +69,25 @@ public function testInvoke()
$signal();
}

/**
* @depends testCall
*/
public function testSetCallback()
{
$signo = 1;

$callback = $this->createCallback(2);
$callback->method('__invoke')
->with($this->identicalTo($signo));

$signal = $this->createSignal($signo, $this->createCallback(0));

$signal->setCallback($callback);

$signal->call();
$signal->call();
}

public function testEnable()
{
$signal = $this->createSignal(1, $this->createCallback(0));
Expand Down
19 changes: 19 additions & 0 deletions tests/Loop/Events/SocketEventTest.php
Expand Up @@ -95,6 +95,25 @@ public function testListen()

$event->listen();
}

/**
* @depends testCall
*/
public function testSetCallback()
{
list($socket) = $this->createSockets();

$callback = $this->createCallback(2);
$callback->method('__invoke')
->with($this->identicalTo($socket), $this->identicalTo(false));

$event = $this->createSocketEvent($socket, $this->createCallback(0));

$event->setCallback($callback);

$event->call(false);
$event->call(false);
}

/**
* @depends testListen
Expand Down

0 comments on commit bf5a680

Please sign in to comment.