Skip to content

Commit

Permalink
Merge pull request #61 from tidal/develop
Browse files Browse the repository at this point in the history
maintenance
  • Loading branch information
tidal authored Jul 25, 2017
2 parents 246c84d + cf38ca7 commit 5d508ca
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
17 changes: 14 additions & 3 deletions examples/crossbar_session/monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 10 additions & 3 deletions src/Tidal/WampWatch/SessionMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -62,7 +64,7 @@ public function __construct(ClientSession $session)
*
* @param $sessionId
*
* @return PromiseAdapter
* @return PromiseInterface
*/
public function getSessionInfo($sessionId)
{
Expand All @@ -72,8 +74,8 @@ function ($res) {

return $res;
},
function ($error) {
$this->emit('error', [$error]);
function (ErrorMessage $error) use ($sessionId) {
$this->emit('error', [$error, $sessionId]);
}
);
}
Expand Down Expand Up @@ -250,4 +252,9 @@ protected function removeOwnSessionId(array $sessionsIds)

return $sessionsIds;
}

public static function create(ClientSession $session)
{
return new self($session);
}
}
9 changes: 9 additions & 0 deletions tests/integration/crossbar/CrosssbarSessionMonitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function test_onstart()

$this->connection->open();

// wait for connection to be established
sleep(1);

$this->assertTrue(is_array($sessionIds));
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/integration/crossbar/CrosssbarSubscriptionMonitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function test_onstart()

$this->connection->open();

// wait for connection to be established
sleep(1);

$this->validateSubscriptionInfo($this->initialSubscriptionInfo);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/SessionMonitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Tidal\WampWatch\Stub\ClientSessionStub;
use PHPUnit_Framework_TestCase;
use stdClass;
use Thruway\Message;

/**
* @author Timo Michna <[email protected]>
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5d508ca

Please sign in to comment.