Skip to content

Commit 50731e5

Browse files
author
jgil
committed
Passing test without nats
1 parent 3a827d8 commit 50731e5

File tree

4 files changed

+49
-35
lines changed

4 files changed

+49
-35
lines changed

phpunit.xml.dist

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false">
12+
1213
<testsuites>
1314
<testsuite name="PHPNats Test Suite">
14-
<directory suffix=".php">./tests/</directory>
15+
<directory suffix=".php">./tests/Unit/</directory>
1516
</testsuite>
1617
</testsuites>
1718
<logging>

tests/Unit/ConnectionTest.php

+7-20
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
use Nats;
1919
use Cocur\BackgroundProcess\BackgroundProcess;
2020

21-
22-
2321
/**
24-
* Class TestConnection.
22+
* Class ConnectionTest.
2523
*
2624
* @category Class
2725
*
@@ -36,35 +34,24 @@ class ConnectionTest extends \PHPUnit_Framework_TestCase
3634

3735
private static $_process;
3836

39-
private static $_port;
40-
4137
public static function setUpBeforeClass()
4238
{
43-
44-
self::$_process = new BackgroundProcess('/usr/bin/php tests/Unit/ListeningServerStub.php &');
39+
self::$_process = new BackgroundProcess('/usr/bin/php ./tests/Util/ListeningServerStub.php ');
4540
self::$_process->run();
46-
4741
}
4842

4943
public static function tearDownAfterClass()
5044
{
5145
self::$_process->stop();
52-
//socket_close(self::$_server);
5346
}
5447

5548
public function setUp()
5649
{
50+
time_nanosleep(0, 100000000);
5751
$this->_c = new Nats\Connection('localhost', 55555);
5852
$this->_c->connect();
5953
}
6054

61-
/**
62-
* Test Dummy.
63-
*/
64-
public function testDummy()
65-
{
66-
$this->assertTrue(true);
67-
}
6855

6956
/**
7057
* Test Connection.
@@ -97,6 +84,7 @@ public function testPing()
9784
*/
9885
public function testPublish()
9986
{
87+
$this->_c->ping();
10088
$this->_c->publish('foo', 'bar');
10189
$count = $this->_c->pubsCount();
10290
$this->assertInternalType('int', $count);
@@ -132,10 +120,9 @@ public function testSubscription()
132120

133121
$this->_c->publish('foo', 'bar');
134122
$this->assertEquals(1, $this->_c->pubsCount());
135-
// $this->_c->wait(1);
136-
}
123+
$process = new BackgroundProcess('/usr/bin/php ./tests/Util/ClientServerStub.php ');
124+
$process->run();
137125

138-
public function testWait()
139-
{
126+
$this->_c->wait(1);
140127
}
141128
}

tests/Unit/ClientServerStub.php renamed to tests/Util/ClientServerStub.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
namespace Nats\tests\Unit;
3+
namespace Nats\tests\Util;
44

55
require 'vendor/autoload.php';
6-
use Cocur\BackgroundProcess\BackgroundProcess;
76

87

98
class ClientServerStub
@@ -24,7 +23,7 @@ public function __construct()
2423

2524
public function write()
2625
{
27-
socket_write($this->sock, "PUBLISH");
26+
socket_write($this->sock, "PING");
2827

2928
}
3029

@@ -36,7 +35,7 @@ public function close()
3635
public function read()
3736
{
3837
// Read the input from the client &#8211; 1024 bytes
39-
$input = socket_read($client, 1024);
38+
$input = socket_read($this->sock, 1024);
4039

4140
return $input;
4241
}
@@ -58,6 +57,10 @@ public function getSock()
5857
}
5958

6059
$client = new ClientServerStub();
60+
sleep(1);
6161
$client->write();
6262

63+
// echo $client->read(100);
64+
6365
$client->close();
66+

tests/Unit/ListeningServerStub.php renamed to tests/Util/ListeningServerStub.php

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3-
namespace Nats\tests\Unit;
3+
namespace Nats\tests\Util;
44

55
require 'vendor/autoload.php';
66

77

8+
use Nats\tests\Util\ClientServerStub;
9+
810
class ListeningServerStub
911
{
1012
protected $client;
@@ -23,18 +25,16 @@ public function __construct()
2325
} else {
2426
echo "Socket created\n";
2527
}
26-
2728
socket_getsockname($this->sock, $this->addr, $this->port);
29+
time_nanosleep(0, 100);
30+
2831
} catch (\Exception $e) {
2932
echo $e->getMessage();
3033
}
3134
}
3235

3336
public function listen()
3437
{
35-
36-
// Bind the socket to an address/port
37-
3838
// Start listening for connections
3939
socket_listen($this->sock);
4040

@@ -72,15 +72,38 @@ public function getSock()
7272
}
7373

7474
$server = new ListeningServerStub();
75+
$time=8;
76+
77+
/*
78+
$client = new ClientServerStub();
79+
*/
80+
7581

76-
while (true) {
82+
while ($time>0) {
83+
time_nanosleep(1, 100);
7784
$clientSocket = socket_accept($server->getSock());
78-
if (false == $clientSocket) {
85+
var_dump($clientSocket);
86+
87+
88+
file_put_contents("/tmp/a.txt", "aaaa\n", FILE_APPEND);
89+
if(!is_null($clientSocket)) {
90+
$lll = socket_read($clientSocket, 100000);
91+
$line = "MSG OK 55966a4463383 10";
92+
$line = "PING";
93+
socket_write($clientSocket, $line);
94+
} else {
95+
$client->write();
96+
$line = "PING";
97+
echo $line;
98+
file_put_contents("/tmp/a.txt", $line."\n", FILE_APPEND);
99+
socket_write($server->getSock(), $line);
100+
// echo $client->write(100);
101+
time_nanosleep(0, 20000);
79102
continue;
103+
80104
}
81-
var_dump($clientSocket);
105+
$time--;
82106
}
83107

108+
$client->close();
84109
$server->close();
85-
// echo "Start listening...";
86-
// $server->listen();

0 commit comments

Comments
 (0)