Skip to content

Commit

Permalink
add new tests and fix the error handling stuff with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikko Koppanen committed Sep 20, 2010
1 parent 81e3246 commit 1952427
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 55 deletions.
19 changes: 19 additions & 0 deletions php_zmq_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,23 @@ typedef struct _php_zmq_poll_object {
# define Z_REFCOUNT_P(pz) (pz)->refcount
#endif

#if ZEND_MODULE_API_NO > 20060613

#define PHP_ZMQ_ERROR_HANDLING_INIT() zend_error_handling error_handling;

#define PHP_ZMQ_ERROR_HANDLING_THROW() zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling TSRMLS_CC);

#define PHP_ZMQ_ERROR_HANDLING_RESTORE() zend_restore_error_handling(&error_handling TSRMLS_CC);

#else

#define PHP_ZMQ_ERROR_HANDLING_INIT()

#define PHP_ZMQ_ERROR_HANDLING_THROW() php_set_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry TSRMLS_CC);

#define PHP_ZMQ_ERROR_HANDLING_RESTORE() php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);

#endif


#endif /* _PHP_ZMQ_PRIVATE_H_ */
29 changes: 29 additions & 0 deletions tests/005-forceconnectarg.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Test forcing connect
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

include dirname(__FILE__) . '/zeromq_test_helper.inc';

$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ);
$socket->connect("tcp://127.0.0.1:5566");
$socket->connect("tcp://127.0.0.1:5566", true);
$socket->connect("tcp://127.0.0.1:5566", true);
$socket->connect("tcp://127.0.0.1:5566", true);
$socket->connect("tcp://127.0.0.1:5566");

var_dump($socket->getEndpoints());

--EXPECTF--
array(2) {
["connect"]=>
array(1) {
[0]=>
string(20) "tcp://127.0.0.1:5566"
}
["bind"]=>
array(0) {
}
}
19 changes: 3 additions & 16 deletions tests/019-callbackinvalidsignature.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
--TEST--
Test callback edge-cases
--SKIPIF--
--XFAIL--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php
Expand All @@ -11,26 +10,14 @@ function try_to_force_ref(&$a, $b)
echo "CALLED\n";
}

function throw_exception($socket, $pid = null)
{
/* This exception should bubble up */
throw new Exception("hello world");
}

try {
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, 'persistent_socket', 'try_to_force_ref');
echo "Fail\n";
} catch (ZMQSocketException $e) {
echo $e->getMessage() . "\n";
}

try {
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, 'persistent_socket', 'throw_exception');
echo "Fail\n";
} catch (ZMQSocketException $e) {
echo $e->getMessage() . "\n";
}

--EXPECTF--
Parameter 1 to try_to_force_ref() expected to be a reference, value given
hello world
Warning: Parameter 1 to try_to_force_ref() expected to be a reference, value given in %s on line %d
Failed to invoke callback try_to_force_ref()

22 changes: 22 additions & 0 deletions tests/020-exceptionincallback.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test throwing exception from callback
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

function this_throws($a, $b)
{
throw new Exception("Hello there");
}

try {
$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, 'persistent_socket', 'this_throws');
echo "Fail\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}

--EXPECTF--
Hello there

17 changes: 17 additions & 0 deletions tests/021-callbackwarning.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test warning in callback
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

function this_throws($a, $b)
{
in_array(1, 1);
}

$socket = new ZMQSocket(new ZMQContext(), ZMQ::SOCKET_REQ, 'persistent_socket', 'this_throws');

--EXPECTF--
Warning: in_array() expects parameter 2 to be array, integer given in %s on line %d

50 changes: 11 additions & 39 deletions zmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,7 @@ static php_zmq_socket *php_zmq_socket_get(php_zmq_context *context, int type, co
static void php_zmq_connect_callback(zval *socket, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, char *persistent_id, int persistent_id_len)
{
zval *retval_ptr, *pid_z;
zval **params[2];
#if ZEND_MODULE_API_NO > 20060613
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry TSRMLS_CC);
#endif
zval **params[2];

ALLOC_INIT_ZVAL(pid_z);

Expand All @@ -288,7 +282,7 @@ static void php_zmq_connect_callback(zval *socket, zend_fcall_info *fci, zend_fc
fci->retval_ptr_ptr = &retval_ptr;
fci->no_separation = 1;

if (zend_call_function(fci, fci_cache TSRMLS_CC) == FAILURE || EG(exception)) {
if (zend_call_function(fci, fci_cache TSRMLS_CC) == FAILURE) {
if (!EG(exception)) {
zend_throw_exception_ex(php_zmq_socket_exception_sc_entry, 0 TSRMLS_CC, "Failed to invoke callback %s()", Z_STRVAL_P(fci->function_name));
}
Expand All @@ -298,12 +292,6 @@ static void php_zmq_connect_callback(zval *socket, zend_fcall_info *fci, zend_fc
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}

#if ZEND_MODULE_API_NO > 20060613
zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
}

/* {{{ proto ZMQContext ZMQContext::getSocket(integer $type[, string $persistent_id = null, callback $on_new_socket = null])
Expand All @@ -322,20 +310,12 @@ PHP_METHOD(zmqcontext, getsocket)
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;

#if ZEND_MODULE_API_NO > 20060613
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry TSRMLS_CC);
#endif
PHP_ZMQ_ERROR_HANDLING_INIT()
PHP_ZMQ_ERROR_HANDLING_THROW()

rc = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!f", &type, &persistent_id, &persistent_id_len, &fci, &fci_cache);

#if ZEND_MODULE_API_NO > 20060613
zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
PHP_ZMQ_ERROR_HANDLING_RESTORE()

if (rc == FAILURE) {
return;
Expand Down Expand Up @@ -364,7 +344,7 @@ PHP_METHOD(zmqcontext, getsocket)
}

if (is_new && ZEND_NUM_ARGS() > 2) {
php_zmq_connect_callback(return_value, &fci, &fci_cache, persistent_id, persistent_id_len);
php_zmq_connect_callback(return_value, &fci, &fci_cache, persistent_id, persistent_id_len);
}
return;
}
Expand All @@ -390,7 +370,7 @@ PHP_METHOD(zmqcontext, ispersistent)

/* --- START ZMQ --- */

/* {{{ proto ZMQSocket ZMQSocket::__construct(ZMQContext $context, integer $type[, string $persistent_id = null])
/* {{{ proto ZMQSocket ZMQSocket::__construct(ZMQContext $context, integer $type[, string $persistent_id = null, callback $on_new_socket = null])
Build a new ZMQSocket object
*/
PHP_METHOD(zmqsocket, __construct)
Expand All @@ -407,20 +387,12 @@ PHP_METHOD(zmqsocket, __construct)
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;

#if ZEND_MODULE_API_NO > 20060613
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry, &error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_THROW, php_zmq_socket_exception_sc_entry TSRMLS_CC);
#endif
PHP_ZMQ_ERROR_HANDLING_INIT()
PHP_ZMQ_ERROR_HANDLING_THROW()

rc = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol|s!f", &obj, php_zmq_context_sc_entry, &type, &persistent_id, &persistent_id_len, &fci, &fci_cache);

#if ZEND_MODULE_API_NO > 20060613
zend_restore_error_handling(&error_handling TSRMLS_CC);
#else
php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
#endif
PHP_ZMQ_ERROR_HANDLING_RESTORE()

if (rc == FAILURE) {
return;
Expand Down Expand Up @@ -448,7 +420,7 @@ PHP_METHOD(zmqsocket, __construct)
}

if (is_new && ZEND_NUM_ARGS() > 3) {
php_zmq_connect_callback(getThis(), &fci, &fci_cache, persistent_id, persistent_id_len);
php_zmq_connect_callback(getThis(), &fci, &fci_cache, persistent_id, persistent_id_len);
}
return;
}
Expand Down

0 comments on commit 1952427

Please sign in to comment.