Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 39468e3

Browse files
author
jgil
committed
Add coverage and improving tests
1 parent 258b8bd commit 39468e3

4 files changed

Lines changed: 85 additions & 67 deletions

File tree

src/Connection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public function __construct(ConnectionOptions $options = null)
107107
{
108108
$this->pings = 0;
109109
$this->pubs = 0;
110-
$this->subscriptions = 0;
111110
$this->subscriptions = [];
112111
$this->options = $options;
113112
if (is_null($options)) {
@@ -235,6 +234,11 @@ public function unsubscribe($sid)
235234
{
236235
$msg = 'UNSUB '.$sid;
237236
$this->send($msg);
237+
238+
if (!empty($this->subscriptions[$sid])) {
239+
unset($this->subscriptions[$sid]);
240+
}
241+
238242
}
239243

240244
/**
@@ -259,9 +263,7 @@ private function handleMSG($line)
259263
$parts = explode(' ', $line);
260264
$length = $parts[3];
261265
$sid = $parts[2];
262-
263266
$payload = $this->receive($length);
264-
265267
$func = $this->subscriptions[$sid];
266268
if (is_callable($func)) {
267269
$func($payload);
@@ -298,6 +300,7 @@ public function wait($quantity = 0)
298300
}
299301
}
300302
}
303+
301304
$this->close();
302305

303306
return $this;

tests/Unit/ConnectionTest.php

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Nats\tests\Unit;
34

45
use Nats;
@@ -26,24 +27,20 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
2627
private static $isGnatsd = false;
2728

2829
/**
29-
* Before Class code setup
30-
*
31-
* @return void
30+
* Before Class code setup.
3231
*/
3332
public static function setUpBeforeClass()
3433
{
35-
if (($socket = @fsockopen("localhost", 4222, $err))!==false) {
36-
self::$isGnatsd = true;
34+
if (($socket = @fsockopen('localhost', 4222, $err)) !== false) {
35+
self::$isGnatsd = true;
3736
} else {
3837
self::$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ListeningServerStub.php ');
3938
self::$process->run();
4039
}
4140
}
4241

4342
/**
44-
* After Class code setup
45-
*
46-
* @return void
43+
* After Class code setup.
4744
*/
4845
public static function tearDownAfterClass()
4946
{
@@ -53,26 +50,21 @@ public static function tearDownAfterClass()
5350
}
5451

5552
/**
56-
* setUp test suite
57-
*
58-
* @return void
53+
* setUp test suite.
5954
*/
6055
public function setUp()
6156
{
6257
$options = new ConnectionOptions();
6358
if (!self::$isGnatsd) {
64-
time_nanosleep(1, 700000000);
59+
time_nanosleep(0, 300000000);
6560
$options->port = 4222;
6661
}
6762
$this->c = new Nats\Connection($options);
6863
$this->c->connect();
6964
}
7065

71-
7266
/**
7367
* Test Connection.
74-
*
75-
* @return void
7668
*/
7769
public function testConnection()
7870
{
@@ -86,9 +78,21 @@ public function testConnection()
8678
}
8779

8880
/**
89-
* Test Ping command.
81+
* Test Connection with bad configuration.
9082
*
91-
* @return void
83+
* @expectedException PHPUnit_Framework_Error
84+
*/
85+
public function testConnectionBadStream()
86+
{
87+
$options = new ConnectionOptions();
88+
$options->host = null;
89+
$options->port = null;
90+
$this->c = new Nats\Connection($options);
91+
$this->c->connect();
92+
}
93+
94+
/**
95+
* Test Ping command.
9296
*/
9397
public function testPing()
9498
{
@@ -99,10 +103,9 @@ public function testPing()
99103
$this->c->close();
100104
}
101105

106+
102107
/**
103108
* Test Publish command.
104-
*
105-
* @return void
106109
*/
107110
public function testPublish()
108111
{
@@ -114,10 +117,9 @@ public function testPublish()
114117
$this->c->close();
115118
}
116119

120+
117121
/**
118122
* Test Reconnect command.
119-
*
120-
* @return void
121123
*/
122124
public function testReconnect()
123125
{
@@ -130,27 +132,50 @@ public function testReconnect()
130132

131133
/**
132134
* Test Subscription command.
133-
*
134-
* @return void
135135
*/
136136
public function testSubscription()
137137
{
138+
138139
$callback = function ($message) {
139140
$this->assertNotNull($message);
140141
$this->assertEquals($message, 'bar');
141142
};
143+
$sid = $this->c->subscribe('foo', $callback);
142144

143-
$this->c->subscribe('foo', $callback);
144145
$this->assertGreaterThan(0, $this->c->subscriptionsCount());
146+
145147
$subscriptions = $this->c->getSubscriptions();
146148
$this->assertInternalType('array', $subscriptions);
147149

148150
$this->c->publish('foo', 'bar');
149151
$this->assertEquals(1, $this->c->pubsCount());
150152

151-
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
153+
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php '.$sid);
152154
$process->run();
153-
// time_nanosleep(1, 0);
155+
154156
$this->c->wait(1);
155157
}
158+
159+
/**
160+
* Test Unsubscription command.
161+
*/
162+
public function testUnSubscription()
163+
{
164+
$callback = function ($message) {
165+
$this->assertNotNull($message);
166+
$this->assertEquals($message, 'bar');
167+
};
168+
169+
$sid = $this->c->subscribe('foo', $callback);
170+
171+
$this->assertGreaterThan(0, $this->c->subscriptionsCount());
172+
$subscriptions = $this->c->getSubscriptions();
173+
$this->assertInternalType('array', $subscriptions);
174+
175+
$this->c->publish('foo', 'bar');
176+
$this->assertEquals(1, $this->c->pubsCount());
177+
178+
$this->c->unsubscribe($sid);
179+
$this->assertEquals(0, $this->c->subscriptionsCount());
180+
}
156181
}

tests/Util/ClientServerStub.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,29 @@ class ClientServerStub
3333
*/
3434
public function __construct()
3535
{
36+
$address = "tcp://localhost:4222";
37+
$this->sock = stream_socket_client($address, $errno, $errstr, STREAM_CLIENT_CONNECT);
38+
/*
3639
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
3740
socket_connect($this->sock, 'localhost', 4222);
41+
*/
3842
}
3943

4044
/**
4145
* Sends a PING command
4246
*
4347
* @return void
4448
*/
45-
public function write()
49+
public function write($msg)
4650
{
47-
socket_write($this->sock, "PING");
48-
49-
}
50-
51-
/**
52-
* Close the connection
53-
*
54-
* @return void
55-
*/
56-
public function close()
57-
{
58-
socket_close($this->sock);
51+
fwrite($this->sock, $msg, strlen($msg));
5952
}
6053
}
6154

6255
$client = new ClientServerStub();
63-
time_nanosleep(4, 0);
64-
65-
$client->write();
56+
$msg = "PING";
57+
if (!empty($argv[1])) {
58+
$msg = trim($argv[1]);
59+
}
6660

67-
$client->close();
61+
$client->write($msg);

tests/Util/ListeningServerStub.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'vendor/autoload.php';
66

77
/**
8-
* Class ListeningServerStub
8+
* Class ListeningServerStub.
99
*/
1010
class ListeningServerStub
1111
{
@@ -37,22 +37,15 @@ class ListeningServerStub
3737
public function __construct()
3838
{
3939
try {
40-
if (($this->sock = socket_create_listen(4222)) === false) {
41-
echo socket_strerror(socket_last_error());
42-
} else {
43-
echo "Socket created\n";
44-
}
45-
socket_getsockname($this->sock, $this->addr, $this->port);
46-
40+
$address = "tcp://localhost:4222";
41+
$this->sock = stream_socket_server($address, $errno, $errstr);
4742
} catch (\Exception $e) {
4843
throw $e;
4944
}
5045
}
5146

5247
/**
5348
* Close socket.
54-
*
55-
* @return void
5649
*/
5750
public function close()
5851
{
@@ -71,23 +64,26 @@ public function getSock()
7164
}
7265

7366
$server = new ListeningServerStub();
74-
$time=25;
67+
$time = 15;
7568

7669
while ($time>0) {
77-
time_nanosleep(1, 100000);
78-
$clientSocket = socket_accept($server->getSock());
70+
time_nanosleep(1, 0);
71+
$clientSocket = stream_socket_accept($server->getSock());
7972

8073
if (!is_null($clientSocket)) {
81-
$lll = socket_read($clientSocket, 100000);
82-
$line = "MSG OK 55966a4463383 10";
83-
$line = "PING";
84-
socket_write($clientSocket, $line);
74+
$lll = trim(fgets($clientSocket));
75+
76+
$line = "MSG OK $lll 10";
77+
if (strpos($lll, 'CONNECT') === false) {
78+
fwrite($clientSocket, $line, strlen($line));
79+
} else {
80+
fwrite($clientSocket, "PING", strlen("PING"));
81+
}
82+
8583
} else {
86-
$line = "PING";
87-
socket_write($server->getSock(), $line);
88-
time_nanosleep(1, 20000);
84+
$line = 'PING';
85+
// fwrite($server->getSock(), $line, strlen($line));
8986
continue;
90-
9187
}
9288
$time--;
9389
}

0 commit comments

Comments
 (0)