Skip to content

Commit

Permalink
Add method 'close'
Browse files Browse the repository at this point in the history
  • Loading branch information
Timandes committed May 8, 2018
1 parent 41cae37 commit c32207e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
33 changes: 31 additions & 2 deletions php_zookeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ static void php_aclv_destroy(struct ACL_vector *aclv);
static void php_stat_to_array(const struct Stat *stat, zval *array);
static void php_aclv_to_array(const struct ACL_vector *aclv, zval *array);
static void php_zk_dispatch();
static void php_zk_close(php_zk_t *i_obj TSRMLS_DC);


/****************************************
Expand Down Expand Up @@ -812,6 +813,22 @@ static PHP_METHOD(Zookeeper, setLogStream)
}
/* }}} */

/* {{{ Zookeeper::close ( .. )
Close the zookeeper handle and free up any resources */
static PHP_METHOD(Zookeeper, close)
{
ZK_METHOD_INIT_VARS;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}

ZK_METHOD_FETCH_OBJECT;

php_zk_close(i_obj TSRMLS_CC);
}
/* }}} */

PHP_FUNCTION(zookeeper_dispatch)
{
php_zk_dispatch();
Expand All @@ -822,14 +839,21 @@ PHP_FUNCTION(zookeeper_dispatch)
****************************************/

/* {{{ constructor/destructor */
static void php_zk_destroy(php_zk_t *i_obj TSRMLS_DC)
static void php_zk_close(php_zk_t *i_obj TSRMLS_DC)
{
if (i_obj->cb_data) {
zend_hash_index_del(&i_obj->callbacks, i_obj->cb_data->h);
i_obj->cb_data = NULL;
}
if (i_obj->zk) {
zookeeper_close(i_obj->zk);
i_obj->zk = NULL;
}
zend_hash_clean(&i_obj->callbacks);
}

static void php_zk_destroy(php_zk_t *i_obj TSRMLS_DC)
{
php_zk_close(i_obj TSRMLS_CC);
zend_hash_destroy(&i_obj->callbacks);

#ifndef ZEND_ENGINE_3
Expand Down Expand Up @@ -1416,6 +1440,9 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_setLogStream, 0)
ZEND_ARG_INFO(0, stream)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_close, 0)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ zookeeper_class_methods */
Expand Down Expand Up @@ -1448,6 +1475,8 @@ static zend_function_entry zookeeper_class_methods[] = {
ZK_ME(setWatcher, arginfo_setWatcher)
ZK_ME(setLogStream, arginfo_setLogStream)

ZK_ME(close, arginfo_close)

PHP_FE_END
};
#undef ZK_ME
Expand Down
20 changes: 20 additions & 0 deletions tests/check_if_exists_after_closing.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Should check if node exists after closing
--SKIPIF--
<?php
if (!extension_loaded('zookeeper')) {
echo 'ZooKeeper extension is not loaded'
};
--FILE--
<?php
$client = new Zookeeper();
$client->connect('localhost:2181');
$client->close();
try {
$client->exists('/test1');
} catch(ZookeeperConnectionException $zce) {
printf("%s\n%d", $zce->getMessage(), $zce->getCode());
}
--EXPECTF--
Zookeeper->connect() was not called
5998
14 changes: 14 additions & 0 deletions tests/close.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Should connect to the ZooKeeper and close it
--SKIPIF--
<?php
if (!extension_loaded('zookeeper')) {
echo 'ZooKeeper extension is not loaded'
};
--FILE--
<?php
$client = new Zookeeper();
$client->connect('localhost:2181');
echo gettype($client->close());
--EXPECT--
NULL
27 changes: 27 additions & 0 deletions tests/create_node_after_closing_and_connecting_again.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Should create node after closing and connecting again
--SKIPIF--
<?php
if (!extension_loaded('zookeeper')) {
echo 'ZooKeeper extension is not loaded'
};
--FILE--
<?php
$client = new Zookeeper('localhost:2181');

if ($client->exists('/test6')) {
$client->delete('/test6');
}

$client->close();
$client->connect('localhost:2181');

echo $client->create('/test6', null, array(
array(
'perms' => Zookeeper::PERM_ALL,
'scheme' => 'world',
'id' => 'anyone'
)
), 2);
--EXPECTF--
/test6%d

0 comments on commit c32207e

Please sign in to comment.