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

Commit 8be1089

Browse files
author
jgil
committed
Revert "Add coverage and improving tests"
This reverts commit 39468e3.
1 parent 39468e3 commit 8be1089

4 files changed

Lines changed: 67 additions & 85 deletions

File tree

src/Connection.php

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

244240
/**
@@ -263,7 +259,9 @@ private function handleMSG($line)
263259
$parts = explode(' ', $line);
264260
$length = $parts[3];
265261
$sid = $parts[2];
262+
266263
$payload = $this->receive($length);
264+
267265
$func = $this->subscriptions[$sid];
268266
if (is_callable($func)) {
269267
$func($payload);
@@ -300,7 +298,6 @@ public function wait($quantity = 0)
300298
}
301299
}
302300
}
303-
304301
$this->close();
305302

306303
return $this;

tests/Unit/ConnectionTest.php

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

54
use Nats;
@@ -27,20 +26,24 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
2726
private static $isGnatsd = false;
2827

2928
/**
30-
* Before Class code setup.
29+
* Before Class code setup
30+
*
31+
* @return void
3132
*/
3233
public static function setUpBeforeClass()
3334
{
34-
if (($socket = @fsockopen('localhost', 4222, $err)) !== false) {
35-
self::$isGnatsd = true;
35+
if (($socket = @fsockopen("localhost", 4222, $err))!==false) {
36+
self::$isGnatsd = true;
3637
} else {
3738
self::$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ListeningServerStub.php ');
3839
self::$process->run();
3940
}
4041
}
4142

4243
/**
43-
* After Class code setup.
44+
* After Class code setup
45+
*
46+
* @return void
4447
*/
4548
public static function tearDownAfterClass()
4649
{
@@ -50,21 +53,26 @@ public static function tearDownAfterClass()
5053
}
5154

5255
/**
53-
* setUp test suite.
56+
* setUp test suite
57+
*
58+
* @return void
5459
*/
5560
public function setUp()
5661
{
5762
$options = new ConnectionOptions();
5863
if (!self::$isGnatsd) {
59-
time_nanosleep(0, 300000000);
64+
time_nanosleep(1, 700000000);
6065
$options->port = 4222;
6166
}
6267
$this->c = new Nats\Connection($options);
6368
$this->c->connect();
6469
}
6570

71+
6672
/**
6773
* Test Connection.
74+
*
75+
* @return void
6876
*/
6977
public function testConnection()
7078
{
@@ -77,22 +85,10 @@ public function testConnection()
7785
$this->assertFalse($this->c->isConnected());
7886
}
7987

80-
/**
81-
* Test Connection with bad configuration.
82-
*
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-
9488
/**
9589
* Test Ping command.
90+
*
91+
* @return void
9692
*/
9793
public function testPing()
9894
{
@@ -103,9 +99,10 @@ public function testPing()
10399
$this->c->close();
104100
}
105101

106-
107102
/**
108103
* Test Publish command.
104+
*
105+
* @return void
109106
*/
110107
public function testPublish()
111108
{
@@ -117,9 +114,10 @@ public function testPublish()
117114
$this->c->close();
118115
}
119116

120-
121117
/**
122118
* Test Reconnect command.
119+
*
120+
* @return void
123121
*/
124122
public function testReconnect()
125123
{
@@ -132,50 +130,27 @@ public function testReconnect()
132130

133131
/**
134132
* Test Subscription command.
133+
*
134+
* @return void
135135
*/
136136
public function testSubscription()
137137
{
138-
139138
$callback = function ($message) {
140139
$this->assertNotNull($message);
141140
$this->assertEquals($message, 'bar');
142141
};
143-
$sid = $this->c->subscribe('foo', $callback);
144142

143+
$this->c->subscribe('foo', $callback);
145144
$this->assertGreaterThan(0, $this->c->subscriptionsCount());
146-
147145
$subscriptions = $this->c->getSubscriptions();
148146
$this->assertInternalType('array', $subscriptions);
149147

150148
$this->c->publish('foo', 'bar');
151149
$this->assertEquals(1, $this->c->pubsCount());
152150

153-
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php '.$sid);
151+
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
154152
$process->run();
155-
153+
// time_nanosleep(1, 0);
156154
$this->c->wait(1);
157155
}
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-
}
181156
}

tests/Util/ClientServerStub.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,35 @@ 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-
/*
3936
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
4037
socket_connect($this->sock, 'localhost', 4222);
41-
*/
4238
}
4339

4440
/**
4541
* Sends a PING command
4642
*
4743
* @return void
4844
*/
49-
public function write($msg)
45+
public function write()
5046
{
51-
fwrite($this->sock, $msg, strlen($msg));
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);
5259
}
5360
}
5461

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

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

tests/Util/ListeningServerStub.php

Lines changed: 21 additions & 17 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,15 +37,22 @@ class ListeningServerStub
3737
public function __construct()
3838
{
3939
try {
40-
$address = "tcp://localhost:4222";
41-
$this->sock = stream_socket_server($address, $errno, $errstr);
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+
4247
} catch (\Exception $e) {
4348
throw $e;
4449
}
4550
}
4651

4752
/**
4853
* Close socket.
54+
*
55+
* @return void
4956
*/
5057
public function close()
5158
{
@@ -64,26 +71,23 @@ public function getSock()
6471
}
6572

6673
$server = new ListeningServerStub();
67-
$time = 15;
74+
$time=25;
6875

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

7380
if (!is_null($clientSocket)) {
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-
81+
$lll = socket_read($clientSocket, 100000);
82+
$line = "MSG OK 55966a4463383 10";
83+
$line = "PING";
84+
socket_write($clientSocket, $line);
8385
} else {
84-
$line = 'PING';
85-
// fwrite($server->getSock(), $line, strlen($line));
86+
$line = "PING";
87+
socket_write($server->getSock(), $line);
88+
time_nanosleep(1, 20000);
8689
continue;
90+
8791
}
8892
$time--;
8993
}

0 commit comments

Comments
 (0)