diff --git a/examples/crossbar_session/monitor.php b/examples/crossbar_session/monitor.php index 2825433..5405f35 100644 --- a/examples/crossbar_session/monitor.php +++ b/examples/crossbar_session/monitor.php @@ -29,7 +29,7 @@ ); $connection->on('open', function (ClientSession $session) use ($connection, &$timer) { - $sessionMonitor = new SessionMonitor(new Adapter($session, new PromiseFactory())); + $sessionMonitor = SessionMonitor::create(new Adapter($session, new PromiseFactory())); echo PHP_EOL . "******** SESSION MONITOR STARTED ********" . PHP_EOL; $sessionMonitor->on('start', function ($l) { @@ -42,12 +42,23 @@ print_r($l); }); - $sessionMonitor->on('join', function ($sessionData) { + $sessionMonitor->on('join', function ($sessionData) use ($sessionMonitor) { echo PHP_EOL . "JOIN: {$sessionData->session}" . PHP_EOL; + + $sessionMonitor->getSessionIds()->then(function (array $sessions) { + + echo "SESSIONS : " . count($sessions) . PHP_EOL; + + }); }); - $sessionMonitor->on('leave', function ($sessionId) { + $sessionMonitor->on('leave', function ($sessionId) use ($sessionMonitor) { echo PHP_EOL . "LEAVE: $sessionId" . PHP_EOL; + $sessionMonitor->getSessionIds()->then(function (array $sessions) { + + echo "SESSIONS : " . count($sessions) . PHP_EOL; + + }); }); $sessionMonitor->on('error', function ($l) { diff --git a/examples/thruway_internal_session_monitor/InternalSessionMonitor.php b/examples/thruway_internal_session_monitor/InternalSessionMonitor.php index 62e9b91..595c709 100644 --- a/examples/thruway_internal_session_monitor/InternalSessionMonitor.php +++ b/examples/thruway_internal_session_monitor/InternalSessionMonitor.php @@ -51,9 +51,9 @@ public function onSessionStart($session, $transport) print_r($l); }); - $sessionMonitor->on('error', function ($l) { - echo PHP_EOL . "ERROR: " . PHP_EOL; - print_r($l); + $sessionMonitor->on('error', function ($l, $i = 'default') { + echo PHP_EOL . "ERROR REQUEST $i : " . PHP_EOL; + print_r(func_get_args()); }); $sessionMonitor->start(); diff --git a/src/Tidal/WampWatch/SessionMonitor.php b/src/Tidal/WampWatch/SessionMonitor.php index af99656..44057fe 100644 --- a/src/Tidal/WampWatch/SessionMonitor.php +++ b/src/Tidal/WampWatch/SessionMonitor.php @@ -12,8 +12,10 @@ namespace Tidal\WampWatch; use Evenement\EventEmitterInterface; +use Thruway\Message\ErrorMessage; use Tidal\WampWatch\ClientSessionInterface as ClientSession; use Tidal\WampWatch\Adapter\React\PromiseAdapter; +use Tidal\WampWatch\Async\PromiseInterface; /** * Description of SessionMonitor. @@ -62,7 +64,7 @@ public function __construct(ClientSession $session) * * @param $sessionId * - * @return PromiseAdapter + * @return PromiseInterface */ public function getSessionInfo($sessionId) { @@ -72,8 +74,8 @@ function ($res) { return $res; }, - function ($error) { - $this->emit('error', [$error]); + function (ErrorMessage $error) use ($sessionId) { + $this->emit('error', [$error, $sessionId]); } ); } @@ -250,4 +252,9 @@ protected function removeOwnSessionId(array $sessionsIds) return $sessionsIds; } + + public static function create(ClientSession $session) + { + return new self($session); + } } diff --git a/tests/integration/crossbar/CrosssbarSessionMonitorTest.php b/tests/integration/crossbar/CrosssbarSessionMonitorTest.php index a3b018c..fd31b01 100644 --- a/tests/integration/crossbar/CrosssbarSessionMonitorTest.php +++ b/tests/integration/crossbar/CrosssbarSessionMonitorTest.php @@ -44,6 +44,9 @@ public function test_onstart() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->assertTrue(is_array($sessionIds)); } @@ -79,6 +82,9 @@ public function test_onjoin() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->assertEquals($this->clientSessionId, $this->monitoredSessionId); } @@ -114,6 +120,9 @@ public function test_onleave() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->assertEquals($this->clientSessionId, $this->monitoredSessionId); } } diff --git a/tests/integration/crossbar/CrosssbarSubscriptionMonitorTest.php b/tests/integration/crossbar/CrosssbarSubscriptionMonitorTest.php index 1e74ce0..59366a5 100644 --- a/tests/integration/crossbar/CrosssbarSubscriptionMonitorTest.php +++ b/tests/integration/crossbar/CrosssbarSubscriptionMonitorTest.php @@ -75,6 +75,9 @@ public function test_onstart() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->validateSubscriptionInfo($this->initialSubscriptionInfo); } @@ -101,6 +104,9 @@ public function test_onstart_delivers_current_list() $clientConnection->open(); + // wait for connection to be established + sleep(1); + $this->validateSubscriptionInfo($this->initialSubscriptionInfo); $this->assertArraySubset([$subscriptionId], $this->initialSubscriptionInfo->exact); } @@ -131,6 +137,9 @@ public function test_oncreate() $this->getConnection()->open(); + // wait for connection to be established + sleep(1); + $this->assertInternalType('int', $this->creatorSessionId); $this->assertInstanceOf(\stdClass::class, $this->clientSubscriptionInfo); $this->assertAttributeEquals($this->testTopicName, 'uri', $this->clientSubscriptionInfo); @@ -207,6 +216,9 @@ public function test_onunsubscribe() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->assertInternalType('int', $sessionId); $this->assertInternalType('int', $subscriptionId); } @@ -256,6 +268,9 @@ public function test_ondelete() $this->connection->open(); + // wait for connection to be established + sleep(1); + $this->assertInternalType('int', $sessionId); $this->assertInternalType('int', $subscriptionId); } diff --git a/tests/unit/SessionMonitorTest.php b/tests/unit/SessionMonitorTest.php index 8de0ba1..6c249df 100644 --- a/tests/unit/SessionMonitorTest.php +++ b/tests/unit/SessionMonitorTest.php @@ -17,6 +17,7 @@ use Tidal\WampWatch\Stub\ClientSessionStub; use PHPUnit_Framework_TestCase; use stdClass; +use Thruway\Message; /** * @author Timo Michna @@ -401,12 +402,13 @@ public function test_get_sessioninfo_fail_emits_event() $monitor->getSessionInfo(654); - $sessionInfo = new stdClass(); - $sessionInfo->session = 654; + $errorMessage = $this->getMockBuilder(Message\ErrorMessage::class) + ->disableOriginalConstructor() + ->getMock(); - $stub->failCall(SessionMonitor::SESSION_INFO_TOPIC, $sessionInfo); + $stub->failCall(SessionMonitor::SESSION_INFO_TOPIC, $errorMessage); - $this->assertSame($sessionInfo, $response); + $this->assertSame($errorMessage, $response); } public function test_invalid_sessioninfo_does_not_get_added()