Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
{
"name": "Evseev Nikolay",
"email": "[email protected]"
},
{
"name": "Aleksandar Babic",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
},
"autoload": {
"psr-4": { "Cassandra\\": "src/" }
},

"autoload-dev" : {
"psr-4": { "Cassandra\\": "tests/"}
}
}
5 changes: 5 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require __DIR__.'/vendor/autoload.php';

date_default_timezone_set('UTC');
24 changes: 24 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="phpunit.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="all">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
103 changes: 88 additions & 15 deletions src/Connection/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,106 @@ public function __construct($options) {
}

/**
*
* Create connection and return socket resource
*
* @throws Exception
* @return resource
*/
public function getConnection() {
if (!empty($this->socket)) return $this->socket;

$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if ($this->socket === false)
throw new Exception(socket_strerror(socket_last_error()));

socket_set_option($this->socket, getprotobyname('TCP'), TCP_NODELAY, 1);

foreach($this->_options['socket'] as $optname => $optval)
socket_set_option($this->socket, SOL_SOCKET, $optname, $optval);

if (!socket_connect($this->socket, $this->_options['host'], $this->_options['port']))
throw new Exception("Unable to connect to Cassandra node: {$this->_options['host']}:{$this->_options['port']}");
if(!$this->isConnected()) {
try {
$this->connect();
} catch(\Exception $e) {
throw new Exception("Unable to connect to Cassandra node: {$this->_options['host']}:{$this->_options['port']}. Reason: ". $e->getMessage(), 0, $e);
}
}

return $this->socket;
}

/**
* @return array
*/
public function getOptions() {
public function getOptions()
{
return $this->_options;
}

/**
* Connect to host/port
*
* @throws Exception
*/
public function connect()
{
$this->createConnection();
$this->setConnectionOptions();

$result = @socket_connect($this->socket, $this->_options['host'], $this->_options['port']);

if($result === false) {
throw new Exception("Unable to connect: " . socket_strerror(socket_last_error($this->socket)));
}
}

/**
* Disconnect
*/
public function disconnect()
{
if(is_resource($this->socket)) {
@socket_close($this->socket);
}

$this->socket = null;
}

/**
* @return bool
*/
public function isConnected()
{
return is_resource($this->socket);
}


/**
* @throws Exception
*/
private function createConnection()
{
$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if($this->socket === false) {
throw new Exception("Unable to create socket: ". socket_strerror(socket_last_error()));
}
}

/**
* @throws Exception
*/
private function setConnectionOptions()
{
$this->setConnectionOption(getprotobyname('tcp'), TCP_NODELAY, 1);

foreach($this->_options['socket'] as $name => $value) {
$this->setConnectionOption(SOL_SOCKET, $name, $value);
}
}

/**
* @param int $level
* @param int $name
* @param mixed $value
* @throws Exception
*/
private function setConnectionOption($level, $name, $value)
{
$result = @socket_set_option($this->socket, $level, $name, $value);

if($result === false) {
throw new Exception("Unable to set socket option: $level, $name");
}
}
}
107 changes: 107 additions & 0 deletions tests/Connection/NodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php namespace Cassandra\Connection;

use Cassandra\Stubs\ListeningServerStub;
use Cassandra\TestCase;

class NodeTest extends TestCase {

/**
* @var ListeningServerStub
*/
protected static $server;

public static function setUpBeforeClass()
{
self::$server = new ListeningServerStub();
self::$server->listen(19042);
}

public static function tearDownAfterClass()
{
self::$server->shutdown();
}

public function testNewInstance()
{
$node = new Node("localhost:19042");

$this->assertInstanceOf('Cassandra\Connection\Node', $node);

return $node;
}

/**
* @param Node $node
* @depends testNewInstance
*/
public function testIsConnected(Node $node)
{
$this->assertFalse($node->isConnected());

$node->connect();

$this->assertTrue($node->isConnected());
}


/**
* @depends testNewInstance
* @param Node $node
*/
public function testGetOptions(Node $node)
{
$this->assertArrayHasKey("host", $node->getOptions());
$this->assertArrayHasKey("port", $node->getOptions());
}

/**
* @depends testNewInstance
* @param Node $node
*/
public function testGetConnectionReturnsResource(Node $node)
{
$connection = $node->getConnection();

$this->assertTrue(is_resource($connection));
}

/**
* @throws Exception
*/
public function testGetConnectionException()
{
$node = new Node("invalidhost:9042");

$this->setExpectedException('Cassandra\Connection\Exception', "invalidhost:9042");

$node->getConnection();
$node->disconnect();
}

public function testInvalidOptions()
{
$node = new Node([
'host' => 'localhost',
'port' => 19042,
'socket' => [
SO_RCVTIMEO => -1,
]
]);

$this->setExpectedException('Cassandra\Connection\Exception', "Unable to set socket option");

$node->connect();
}

/**
*
* @depends testNewInstance
* @param Node $node
*/
public function testDisconnect(Node $node)
{
$node->disconnect();

$this->assertFalse($node->isConnected());
}
}
27 changes: 27 additions & 0 deletions tests/Stubs/ListeningServerStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Cassandra\Stubs;

class ListeningServerStub
{
protected $sock;

public function listen($port = 9042)
{
$this->sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port
if(!socket_bind($this->sock, 'localhost', $port)) {
throw new \RuntimeException('Could not bind to address');
}

socket_set_nonblock($this->sock);

// Start listening for connections
socket_listen($this->sock);
}


public function shutdown()
{
socket_shutdown($this->sock);
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php namespace Cassandra;

class TestCase extends \PHPUnit_Framework_TestCase {


}