Skip to content

Commit

Permalink
CLIENTAPI-27: Refactor unit tests to use less VoltDB servers
Browse files Browse the repository at this point in the history
Split tests in ClientTest up into ClientTest, ClientOfflineTest, and ClientSecurityTest based on how each needs to interact with a VoltDB server (if at all). ClientTest and ClientSecurityTest each start just a single VoltDB server.

Leaving StatusListenerTest alone for now because the minimal speed gains don't justify the optimization. Those tests are better off left independent.

Overall, the running time of the unit tests went from 44 seconds to 24 seconds on my desktop.
  • Loading branch information
Ed Mazur committed Aug 24, 2010
1 parent 5e10026 commit dcc8c04
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 165 deletions.
2 changes: 2 additions & 0 deletions test/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<file>tests/examples/HelloWorldAsyncTest.php</file>
</testsuite>
<testsuite name="core">
<file>tests/core/ClientOfflineTest.php</file>
<file>tests/core/ClientSecurityTest.php</file>
<file>tests/core/ClientTest.php</file>
<file>tests/core/ParameterTest.php</file>
<file>tests/core/StatusListenerTest.php</file>
Expand Down
4 changes: 4 additions & 0 deletions test/projects/HelloWorld.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function __construct() {
'Select',
'SELECT HELLO, WORLD FROM HELLOWORLD WHERE DIALECT = ?;',
'HELLOWORLD.DIALECT: 0');
$procedures[] = new ProcedureInfo(
array(),
'Delete',
'DELETE FROM HELLOWORLD;');
parent::setProcedures($procedures);

$partitions = array();
Expand Down
118 changes: 118 additions & 0 deletions test/tests/core/ClientOfflineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/* This file is part of VoltDB.
* Copyright (C) 2008-2010 VoltDB L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* Tests that all Client methods behave correctly and throw proper exceptions when no VoltDB server is running.
*/
class ClientOfflineTest extends PHPUnit_Framework_TestCase {

/**
* Client::create(), Client::createConnection()
*/

public function testCreateConnectionNoServerRunning() {
$client = Client::create();
parent::setExpectedException('ConnectException');
$client->createConnection('localhost');
}

/**
* Client::createFromPool()
*/

public function testCreateFromPoolNoServerRunning() {
parent::setExpectedException('ConnectException');
Client::createFromPool('localhost');
}

/**
* Client::invoke()
*/

public function testInvokeNoConnection() {
$client = Client::create();
parent::setExpectedException('NoConnectionsException');
$client->invoke($this->getSelect());
}

/**
* Client::invokeAsync()
*/

public function testInvokeAsyncNoConnection() {
$client = Client::create();
parent::setExpectedException('NoConnectionsException');
$client->invokeAsync($this->getSelect(), new ClientOfflineTestCallback());
}

/**
* Client::runOnce()
*/

public function testRunOnceNoConnections() {
$client = Client::create();
parent::setExpectedException('NoConnectionsException');
$client->runOnce();
}

/**
* Client::run()
*/

public function testRunNoConnections() {
$client = Client::create();
parent::setExpectedException('NoConnectionsException');
$client->run();
}

/**
* Client::drain()
*/

public function testDrainNoConnections() {
$client = Client::create();
parent::setExpectedException('NoConnectionsException');
$client->drain();
}

/**
* Test helper
*/

private function getSelect() {
$parameters = new Parameters();
$parameters->push(new Parameter(voltdb::WIRE_TYPE_STRING));
$selectProcedure = new Procedure('Select', $parameters);
$selectProcedure->params()->addString('Spanish');
return $selectProcedure;
}

}

class ClientOfflineTestCallback extends ProcedureCallback {

public function callback($response) {}

}
86 changes: 86 additions & 0 deletions test/tests/core/ClientSecurityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/* This file is part of VoltDB.
* Copyright (C) 2008-2010 VoltDB L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* Tests that Client methods involving security behave correctly.
*/
class ClientSecurityTest extends PHPUnit_Framework_TestCase {

private static $server;

public static function setUpBeforeClass() {
self::$server = new SecuritySuite();
self::$server->start();
}

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

public function testCreateConnectionSecurity() {
$client = Client::create();

// don't specify credentials
try {
$client->createConnection('localhost');
parent::assertTrue(false, 'Shouldn\'t have been able to authenticate.');
} catch (ConnectException $e) {}

// specify incorrect credentials
try {
$client->createConnection('localhost', 'user-1', 'password-x');
parent::assertTrue(false, 'Shouldn\'t have been able to authenticate.');
} catch (ConnectException $e) {}

// specify correct credentials
try {
$client->createConnection('localhost', 'user-1', 'password-1');
} catch (ConnectException $e) {
parent::assertTrue(false, 'Should have been able to authenticate.');
}
}

public function testCreateFromPoolSecurity() {
// don't specify credentials
try {
Client::createFromPool('localhost');
parent::assertTrue(false, 'Shouldn\'t have been able to authenticate.');
} catch (ConnectException $e) {}

// specify incorrect credentials
try {
Client::createFromPool('localhost', 'user-1', 'password-x');
parent::assertTrue(false, 'Shouldn\'t have been able to authenticate.');
} catch (ConnectException $e) {}

// specify correct credentials
try {
Client::createFromPool('localhost', 'user-1', 'password-1');
} catch (ConnectException $e) {
parent::assertTrue(false, 'Should have been able to authenticate.');
}
}

}
Loading

0 comments on commit dcc8c04

Please sign in to comment.