Skip to content
Merged
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: 1 addition & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if test "$PHP_ZOOKEEPER" != "no"; then
PHP_ADD_LIBRARY_WITH_PATH(zookeeper_mt, $PHP_LIBZOOKEEPER_DIR/lib, ZOOKEEPER_SHARED_LIBADD)

PHP_SUBST(ZOOKEEPER_SHARED_LIBADD)
PHP_NEW_EXTENSION(zookeeper, php_zookeeper.c zoo_lock.c $SESSION_EXTRA_FILES, $ext_shared,,$SESSION_INCLUDES)
PHP_NEW_EXTENSION(zookeeper, php_zookeeper.c zoo_lock.c $SESSION_EXTRA_FILES php_zookeeper_exceptions.c, $ext_shared,,$SESSION_INCLUDES)

fi

Expand Down
29 changes: 17 additions & 12 deletions php_zookeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "php_zookeeper.h"
#include "php_zookeeper_private.h"
#include "php_zookeeper_session.h"
#include "php_zookeeper_exceptions.h"

/****************************************
Helper macros
Expand All @@ -51,7 +52,7 @@
#define ZK_METHOD_FETCH_OBJECT \
i_obj = (php_zk_t *) zend_object_store_get_object( object TSRMLS_CC ); \
if (!i_obj->zk) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zookeeper connect was not called"); \
php_zk_throw_exception(PHPZK_CONNECT_NOT_CALLED TSRMLS_CC); \
return; \
} \

Expand Down Expand Up @@ -115,6 +116,7 @@ static void php_zookeeper_connect_impl(INTERNAL_FUNCTION_PARAMETERS, char *host,
php_cb_data_t *cb_data = NULL;

if (recv_timeout <= 0) {
php_zk_throw_exception(ZBADARGUMENTS TSRMLS_CC);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recv_timeout parameter has to be greater than 0");
ZVAL_NULL(object);
return;
Expand All @@ -129,7 +131,7 @@ static void php_zookeeper_connect_impl(INTERNAL_FUNCTION_PARAMETERS, char *host,
recv_timeout, 0, cb_data, 0);

if (zk == NULL) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not init zookeeper instance");
php_zk_throw_exception(PHPZK_CONNECTION_FAILURE TSRMLS_CC);
/* not reached */
ZVAL_NULL(object);
return;
Expand Down Expand Up @@ -220,7 +222,7 @@ static PHP_METHOD(Zookeeper, create)
realpath, realpath_max);
if (status != ZOK) {
efree(realpath);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -248,7 +250,7 @@ static PHP_METHOD(Zookeeper, delete)

status = zoo_delete(i_obj->zk, path, version);
if (status != ZOK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -284,7 +286,7 @@ static PHP_METHOD(Zookeeper, getChildren)
cb_data, &strings);
if (status != ZOK) {
php_cb_data_destroy(&cb_data);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -328,7 +330,7 @@ static PHP_METHOD(Zookeeper, get)

if (status != ZOK) {
php_cb_data_destroy(&cb_data);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}
length = stat.dataLength;
Expand All @@ -347,7 +349,7 @@ static PHP_METHOD(Zookeeper, get)
if (status != ZOK) {
efree (buffer);
php_cb_data_destroy(&cb_data);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);

/* Indicate data marshalling failure with boolean false so that user can retry */
if (status == ZMARSHALLINGERROR) {
Expand Down Expand Up @@ -397,7 +399,7 @@ static PHP_METHOD(Zookeeper, exists)
cb_data, &stat);
if (status != ZOK && status != ZNONODE) {
php_cb_data_destroy(&cb_data);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -437,7 +439,7 @@ static PHP_METHOD(Zookeeper, set)
}
status = zoo_set2(i_obj->zk, path, value, value_len, version, stat_ptr);
if (status != ZOK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -490,7 +492,7 @@ static PHP_METHOD(Zookeeper, getAcl)

status = zoo_get_acl(i_obj->zk, path, &aclv, &stat);
if (status != ZOK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -527,7 +529,7 @@ static PHP_METHOD(Zookeeper, setAcl)
status = zoo_set_acl(i_obj->zk, path, version, &aclv);
php_aclv_destroy(&aclv);
if (status != ZOK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}
RETURN_TRUE;
Expand Down Expand Up @@ -640,7 +642,7 @@ static PHP_METHOD(Zookeeper, addAuth)
status = zoo_add_auth(i_obj->zk, scheme, cert, cert_len,
(fci.size != 0) ? php_zk_completion_marshal : NULL, cb_data);
if (status != ZOK) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error: %s", zerror(status));
php_zk_throw_exception(status TSRMLS_CC);
return;
}

Expand Down Expand Up @@ -1232,6 +1234,9 @@ PHP_MINIT_FUNCTION(zookeeper)
#ifdef HAVE_ZOOKEEPER_SESSION
php_session_register_module(ps_zookeeper_ptr);
#endif

php_zk_register_exceptions(TSRMLS_C);

return SUCCESS;
}
/* }}} */
Expand Down
3 changes: 3 additions & 0 deletions php_zookeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ PS_FUNCS(zookeeper);

#endif /* HAVE_ZOOKEEPER_SESSION */

#define PHPZK_CONNECTION_FAILURE 5999
#define PHPZK_CONNECT_NOT_CALLED 5998

#endif /* PHP_ZOOKEEPER_H */


Expand Down
104 changes: 104 additions & 0 deletions php_zookeeper_exceptions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Ryan Uber <[email protected]> |
| Timandes White <[email protected]> |
+----------------------------------------------------------------------+
*/

#include <php.h>

#ifdef ZTS
#include "TSRM.h"
#endif

#include "php_zookeeper.h"
#include "php_zookeeper_exceptions.h"

void php_zk_register_exceptions(TSRMLS_D)
{
zend_class_entry ce;

INIT_CLASS_ENTRY(ce, "ZookeeperException", NULL);
zk_base_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperOperationTimeoutException", NULL);
zk_optimeout_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperConnectionException", NULL);
zk_connection_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperMarshallingException", NULL);
zk_marshalling_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperAuthenticationException", NULL);
zk_auth_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperSessionException", NULL);
zk_session_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);

INIT_CLASS_ENTRY(ce, "ZookeeperNoNodeException", NULL);
zk_nonode_exception = zend_register_internal_class_ex(&ce, zk_base_exception, "ZookeeperException" TSRMLS_CC);
}

zend_class_entry * php_zk_get_exception_with_message(zend_class_entry *ce, char *message TSRMLS_DC)
{
zend_declare_property_string(ce, "message", strlen("message"), message, ZEND_ACC_PUBLIC TSRMLS_CC);
return ce;
}

void php_zk_throw_exception(int zk_status TSRMLS_DC)
{
zend_class_entry *ce;
char *message = NULL;

switch(zk_status) {
case PHPZK_CONNECTION_FAILURE:
ce = zk_connection_exception;
message = "Failed to connect to Zookeeper";
break;
case PHPZK_CONNECT_NOT_CALLED:
ce = zk_connection_exception;
message = "Zookeeper->connect() was not called";
break;
case ZCONNECTIONLOSS:
ce = zk_connection_exception;
break;
case ZOPERATIONTIMEOUT:
ce = zk_optimeout_exception;
break;
case ZMARSHALLINGERROR:
ce = zk_marshalling_exception;
break;
case ZNOAUTH:
case ZAUTHFAILED:
ce = zk_auth_exception;
break;
case ZSESSIONEXPIRED:
case ZSESSIONMOVED:
ce = zk_session_exception;
break;
case ZNONODE:
ce = zk_nonode_exception;
break;
default:
ce = zk_base_exception;
break;
}

if (!message) {
message = (char *)zerror(zk_status);
}

zend_throw_exception(php_zk_get_exception_with_message(ce, message TSRMLS_CC), NULL, zk_status TSRMLS_CC);
return;
}
41 changes: 41 additions & 0 deletions php_zookeeper_exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
+----------------------------------------------------------------------+
| Copyright (c) 2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Ryan Uber <[email protected]> |
| Timandes White <[email protected]> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_ZOOKEEPER_EXCEPTIONS
#define PHP_ZOOKEEPER_EXCEPTIONS

#include <Zend/zend_exceptions.h>

zend_class_entry *zk_base_exception;
zend_class_entry *zk_optimeout_exception;
zend_class_entry *zk_connection_exception;
zend_class_entry *zk_marshalling_exception;
zend_class_entry *zk_auth_exception;
zend_class_entry *zk_session_exception;
zend_class_entry *zk_nonode_exception;

/**
* register exceptions
*/
void php_zk_register_exceptions(TSRMLS_D);
zend_class_entry * php_zk_get_exception_with_message(zend_class_entry *ce, char *message TSRMLS_DC);
/**
* throw exception according to status
*/
void php_zk_throw_exception(int zk_status TSRMLS_DC);

#endif /* PHP_ZOOKEEPER_EXCEPTIONS */
10 changes: 10 additions & 0 deletions tests/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Check for zookeeper presence
--SKIPIF--
<?php if (!extension_loaded("zookeeper")) print "skip"; ?>
--FILE--
<?php
echo "zookeeper extension is available";
?>
--EXPECT--
zookeeper extension is available
9 changes: 7 additions & 2 deletions tests/check_if_exists_without_connect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (!extension_loaded('zookeeper')) {
--FILE--
<?php
$client = new Zookeeper();
$client->exists('/test1');
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests must be more simple.
E.g:

--TEST--
Should throw exception when check if node exists without connect
--SKIPIF--
<?php
if (!extension_loaded('zookeeper')) {
    echo 'Zookeeper extension is not loaded'
};
--FILE--
<?php
$client = new Zookeeper();
try {
    $client->exists('/test1');
} catch(Exception $e) {
    echo $e->getMessage() . "\n" . $e->getCode();
}
--EXPECT--
Zookeeper->connect() was not called
5998

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does --EXPECT-- part support PHP code?

like:

--EXPECT--
Zookeeper->connect() was not called
<?php echo Zookeeper::CONNECT_NOT_CALLED; ?>

I'm worried about future changing of constant value.

Is this my over-worriness?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, see these docs bellow and be a monster in making tests!

https://qa.php.net/write-test.php
https://qa.php.net/phpt_details.php

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Monster +_+....

$client->exists('/test1');
} catch(ZookeeperConnectionException $zce) {
printf("%s\n%d", $zce->getMessage(), $zce->getCode());
}
--EXPECTF--
Warning: Zookeeper::exists(): Zookeeper connect was not called in %s on line %d
Zookeeper->connect() was not called
5998
8 changes: 7 additions & 1 deletion tests/construct_retrieve_error_with_invalid_recv.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ if (!extension_loaded('zookeeper')) {
};
--FILE--
<?php
$client = new Zookeeper('localhost:2181', null, -1);
try {
$client = new Zookeeper('localhost:2181', null, -1);
} catch (ZookeeperException $ze) {
printf("%s\n%d", $ze->getMessage(), $ze->getCode());
}
--EXPECTF--
Warning: Zookeeper::__construct(): recv_timeout parameter has to be greater than 0 in %s on line %d
bad arguments
-8
9 changes: 7 additions & 2 deletions tests/create_node_retrieve_invalid_status_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (!extension_loaded('zookeeper')) {
--FILE--
<?php
$client = new Zookeeper('localhost:2181');
$client->create('/test5', '', array());
try {
$client->create('/test5', '', array());
} catch (ZookeeperException $ze) {
printf("%s\n%d", $ze->getMessage(), $ze->getCode());
}
--EXPECTF--
Warning: Zookeeper::create(): error: invalid acl in %s on line %d
invalid acl
-114
22 changes: 13 additions & 9 deletions tests/create_node_without_connect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ if (!extension_loaded('zookeeper')) {
--FILE--
<?php
$client = new Zookeeper();

$client->create('/test6', null, array(
array(
'perms' => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone'
)
));
try {
$client->create('/test6', null, array(
array(
'perms' => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone'
)
));
} catch(ZookeeperConnectionException $zce) {
printf("%s\n%d", $zce->getMessage(), $zce->getCode());
}
--EXPECTF--
Warning: Zookeeper::create(): Zookeeper connect was not called in %s on line %d
Zookeeper->connect() was not called
5998
9 changes: 7 additions & 2 deletions tests/exists_retrieve_error_with_invalid_param.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ if (!extension_loaded('zookeeper')) {
--FILE--
<?php
$client = new Zookeeper('localhost:2181');
$client->exists(10);
try {
$client->exists(10);
} catch (ZookeeperException $ze) {
printf("%s\n%d", $ze->getMessage(), $ze->getCode());
}
--EXPECTF--
Warning: Zookeeper::exists(): error: bad arguments in %s on line %d
bad arguments
-8
Loading