userland test
authorMichael Wallner <mike@php.net>
Fri, 5 Dec 2014 10:26:18 +0000 (11:26 +0100)
committerMichael Wallner <mike@php.net>
Fri, 5 Dec 2014 10:26:18 +0000 (11:26 +0100)
php_raphf.c
php_raphf.h
php_raphf_test.c
raphf_test.php [deleted file]
tests/test.phpt [new file with mode: 0644]

index 8b89669574de608e782737e32029e7cb02531331..dcb69040ae0c358c4afccd07b58be9dbe2067859 100644 (file)
@@ -20,7 +20,7 @@
 #include "php_raphf.h"
 
 #ifndef PHP_RAPHF_TEST
-#      define PHP_RAPHF_TEST 1
+#      define PHP_RAPHF_TEST 0
 #endif
 
 struct php_persistent_handle_globals {
@@ -137,8 +137,8 @@ static int php_persistent_handle_apply_stat(zval *p TSRMLS_DC, int argc,
        zval zsubentry, *zentry = va_arg(argv, zval *);
 
        array_init(&zsubentry);
-       add_assoc_long_ex(&zsubentry, ZEND_STRS("used"), list->used);
-       add_assoc_long_ex(&zsubentry, ZEND_STRS("free"),
+       add_assoc_long_ex(&zsubentry, ZEND_STRL("used"), list->used);
+       add_assoc_long_ex(&zsubentry, ZEND_STRL("free"),
                        zend_hash_num_elements(&list->free));
        if (key->key) {
                add_assoc_zval_ex(zentry, key->key->val, key->key->len, &zsubentry);
@@ -315,7 +315,7 @@ ZEND_RESULT_CODE php_persistent_handle_provide(const char *name_str,
 
                        ZVAL_PTR(&p, provider);
                        if (zend_symtable_str_update(&PHP_RAPHF_G->persistent_handle.hash,
-                                       name_str, name_len + 1, &p)) {
+                                       name_str, name_len, &p)) {
                                return SUCCESS;
                        }
                        php_resource_factory_dtor(&provider->rf);
@@ -340,7 +340,7 @@ php_persistent_handle_factory_t *php_persistent_handle_concede(
        memset(a, 0, sizeof(*a));
 
        a->provider = zend_symtable_str_find_ptr(&PHP_RAPHF_G->persistent_handle.hash,
-                       name_str, name_len+1);
+                       name_str, name_len);
 
        if (a->provider) {
                a->ident.str = estrndup(ident_str, ident_len);
@@ -474,7 +474,7 @@ void php_persistent_handle_cleanup(const char *name_str, size_t name_len,
 
        if (name_str && name_len) {
                provider = zend_symtable_str_find_ptr(&PHP_RAPHF_G->persistent_handle.hash,
-                               name_str, name_len+1);
+                               name_str, name_len);
 
                if (provider) {
                        if (ident_str && ident_len) {
@@ -570,6 +570,7 @@ static const zend_function_entry raphf_functions[] = {
                        ai_raphf_clean_persistent_handles, 0)
 #if PHP_RAPHF_TEST
        ZEND_NS_FENTRY("raphf", provide, ZEND_FN(raphf_provide), NULL, 0)
+       ZEND_NS_FENTRY("raphf", conceal, ZEND_FN(raphf_conceal), NULL, 0)
        ZEND_NS_FENTRY("raphf", concede, ZEND_FN(raphf_concede), NULL, 0)
        ZEND_NS_FENTRY("raphf", dispute, ZEND_FN(raphf_dispute), NULL, 0)
        ZEND_NS_FENTRY("raphf", handle_ctor, ZEND_FN(raphf_handle_ctor), NULL, 0)
index e58273a713163a2d0c6831f693a43e2fdfe24545..dcf1594c12f040c0377ad68cb1c32be2edd69327 100644 (file)
@@ -129,7 +129,7 @@ PHP_RAPHF_API void php_resource_factory_dtor(php_resource_factory_t *f);
 /**
  * Destroy and free the resource factory.
  *
- * Calls php_resource_factory_dtor() and frees \æ f if the factory's refcount
+ * Calls php_resource_factory_dtor() and frees \a f if the factory's refcount
  * reached 0.
  *
  * @param f the resource factory
index 6d5978904ac15adc4a09081c5c812716548ef2d8..3bf7c5d4d38894c872b6343ffb0fc7a1bd9989df 100644 (file)
@@ -46,7 +46,6 @@ static void raphf_user_dtor(void *opaque)
 {
        struct raphf_user *ru = opaque;
 
-       fprintf(stderr, "Freeing raphf_user struct\n");
        zend_fcall_info_argn(&ru->data.dtor.fci, 1, &ru->data.data);
        zend_fcall_info_call(&ru->data.dtor.fci, &ru->data.dtor.fcc, NULL, NULL);
        zend_fcall_info_args_clear(&ru->data.dtor.fci, 1);
@@ -141,8 +140,20 @@ static PHP_FUNCTION(raphf_provide)
 
        if (SUCCESS != php_persistent_handle_provide(name_str, name_len,
                        &user_ops, ru, raphf_user_dtor)) {
+               RETURN_FALSE;
+       }
+       RETURN_TRUE;
+}
+
+static PHP_FUNCTION(raphf_conceal)
+{
+       zend_string *name;
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &name)) {
                return;
        }
+
+       RETURN_BOOL(FAILURE != zend_hash_del(&PHP_RAPHF_G->persistent_handle.hash, name));
 }
 
 static PHP_FUNCTION(raphf_concede)
@@ -186,7 +197,7 @@ static PHP_FUNCTION(raphf_dispute)
                return;
        }
 
-       zend_list_close(Z_RES_P(zrf));
+       RETURN_BOOL(SUCCESS == zend_list_close(Z_RES_P(zrf)));
 }
 
 static PHP_FUNCTION(raphf_handle_ctor)
@@ -231,6 +242,7 @@ static PHP_FUNCTION(raphf_handle_dtor)
 
 static PHP_MINIT_FUNCTION(raphf_test)
 {
+       zend_register_long_constant(ZEND_STRL("RAPHF_TEST"), PHP_RAPHF_TEST, CONST_CS|CONST_PERSISTENT, module_number);
        raphf_user_le = zend_register_list_destructors_ex(raphf_user_res_dtor, NULL,
                        "raphf_user", module_number);
        return SUCCESS;
diff --git a/raphf_test.php b/raphf_test.php
deleted file mode 100644 (file)
index 70b06de..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php
-function dumper($id) {
-       return function() use ($id) {
-               echo "### back '$id':\n";
-               for ($i=0; $i<func_num_args(); ++$i) {
-                       echo "#### arg $i: ";
-                       var_dump(func_get_arg($i));
-               }
-               /* relay arguments back */
-               return func_get_args();
-       };
-}
-
-echo "## call provide:\n";
-var_dump(raphf\provide("test",dumper("ctor"),dumper("copy"),dumper("dtor"),"data value",dumper("data_dtor")));
-
-echo "## call concede:\n";
-var_dump($rf = raphf\concede("test","1"));
-
-echo "## call handle_ctor:\n";
-var_dump($h = raphf\handle_ctor($rf, 1));
-
-echo "## call handle_copy:\n";
-var_dump($h2 = raphf\handle_copy($rf, $h));
-
-var_dump(raphf\stat_persistent_handles());
-
-echo "## call handle_dtor:\n";
-var_dump(raphf\handle_dtor($rf, $h));
-var_dump(raphf\stat_persistent_handles());
-
-echo "## call handle_dtor:\n";
-var_dump(raphf\handle_dtor($rf, $h2));
-var_dump(raphf\stat_persistent_handles());
-
-var_dump(raphf\dispute($rf), $rf);
-
diff --git a/tests/test.phpt b/tests/test.phpt
new file mode 100644 (file)
index 0000000..60c87b6
--- /dev/null
@@ -0,0 +1,158 @@
+--TEST--
+raphf test
+--SKIPIF--
+<?php
+if (!extension_loaded("raphf")) {
+       die("skip need ext/raphf");
+}
+if (!defined("RAPHF_TEST")) {
+       die("skip need RAPHF_TEST defined (-DPHP_RAPHF_TEST=1)");
+}
+?>
+--INI--
+raphf.persistent_handle.limit=0
+--FILE--
+<?php
+
+function dumper($id) {
+       return function() use ($id) {
+               echo "### back '$id':\n";
+               for ($i=0; $i<func_num_args(); ++$i) {
+                       echo "#### arg $i: ";
+                       var_dump(func_get_arg($i));
+               }
+               /* relay arguments back */
+               return func_get_args();
+       };
+}
+
+echo "## call provide:\n";
+var_dump(raphf\provide("test",dumper("ctor"),dumper("copy"),dumper("dtor"),"data value",dumper("data_dtor")));
+
+echo "## call concede:\n";
+var_dump($rf = raphf\concede("test","1"));
+
+echo "## call handle_ctor:\n";
+var_dump($h = raphf\handle_ctor($rf, 1));
+
+echo "## call handle_copy:\n";
+var_dump($h2 = raphf\handle_copy($rf, $h));
+var_dump(raphf\stat_persistent_handles());
+
+echo "## call handle_dtor:\n";
+var_dump(raphf\handle_dtor($rf, $h));
+var_dump(raphf\stat_persistent_handles());
+
+echo "## call handle_dtor:\n";
+var_dump(raphf\handle_dtor($rf, $h2));
+var_dump(raphf\stat_persistent_handles());
+
+echo "## cleanup:\n";
+var_dump(raphf\dispute($rf), $rf);
+var_dump(raphf\conceal("test"));
+var_dump(raphf\stat_persistent_handles());
+
+?>
+--EXPECTF--
+## call provide:
+bool(true)
+## call concede:
+resource(4) of type (raphf_user)
+## call handle_ctor:
+### back 'ctor':
+#### arg 0: string(10) "data value"
+#### arg 1: int(1)
+array(2) {
+  [0]=>
+  string(10) "data value"
+  [1]=>
+  int(1)
+}
+## call handle_copy:
+### back 'copy':
+#### arg 0: string(10) "data value"
+#### arg 1: array(2) {
+  [0]=>
+  string(10) "data value"
+  [1]=>
+  int(1)
+}
+array(2) {
+  [0]=>
+  string(10) "data value"
+  [1]=>
+  array(2) {
+    [0]=>
+    string(10) "data value"
+    [1]=>
+    int(1)
+  }
+}
+object(stdClass)#%d (1) {
+  ["test"]=>
+  array(1) {
+    [1]=>
+    array(2) {
+      ["used"]=>
+      int(2)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+## call handle_dtor:
+### back 'dtor':
+#### arg 0: string(10) "data value"
+#### arg 1: array(2) {
+  [0]=>
+  string(10) "data value"
+  [1]=>
+  int(1)
+}
+NULL
+object(stdClass)#%d (1) {
+  ["test"]=>
+  array(1) {
+    [1]=>
+    array(2) {
+      ["used"]=>
+      int(1)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+## call handle_dtor:
+### back 'dtor':
+#### arg 0: string(10) "data value"
+#### arg 1: array(2) {
+  [0]=>
+  string(10) "data value"
+  [1]=>
+  array(2) {
+    [0]=>
+    string(10) "data value"
+    [1]=>
+    int(1)
+  }
+}
+NULL
+object(stdClass)#%d (1) {
+  ["test"]=>
+  array(1) {
+    [1]=>
+    array(2) {
+      ["used"]=>
+      int(0)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+## cleanup:
+bool(true)
+resource(4) of type (Unknown)
+### back 'data_dtor':
+#### arg 0: string(10) "data value"
+bool(true)
+bool(false)