mysqli_poll.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. --TEST--
  2. int mysqli_poll() simple
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('connect.inc');
  8. require_once('skipifconnectfailure.inc');
  9. if (!$IS_MYSQLND)
  10. die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");
  11. ?>
  12. --FILE--
  13. <?php
  14. require_once('connect.inc');
  15. function get_connection() {
  16. global $host, $user, $passwd, $db, $port, $socket;
  17. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  18. printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  19. return $link;
  20. }
  21. if (!$link = get_connection())
  22. printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  23. $read = $error = $reject = array($link);
  24. if (0 !== ($tmp = (mysqli_poll($read, $error, $reject, 0, 1))))
  25. printf("[009] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));
  26. $read = $error = $reject = array($link);
  27. try {
  28. mysqli_poll($read, $error, $reject, -1, 1);
  29. } catch (\ValueError $e) {
  30. echo $e->getMessage() . \PHP_EOL;
  31. }
  32. try {
  33. mysqli_poll($read, $error, $reject, 0, -1);
  34. } catch (\ValueError $e) {
  35. echo $e->getMessage() . \PHP_EOL;
  36. }
  37. $link->close();
  38. $read[0] = get_connection();
  39. try {
  40. mysqli_poll($read, $error, $reject, 0, 1);
  41. } catch (\Error $e) {
  42. echo $e->getMessage() . \PHP_EOL;
  43. }
  44. function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax) {
  45. if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000)))
  46. printf("[%03d + 1] There should be %d links ready to read from, %d ready\n",
  47. $exp_ready, $tmp);
  48. foreach ($links as $mysqli) {
  49. if ($use_oo_syntax) {
  50. $res = $mysqli->reap_async_query();
  51. } else {
  52. $res = mysqli_reap_async_query($mysqli);
  53. }
  54. if (is_object($res)) {
  55. printf("[%03d + 2] Can fetch resultset although no query has been run!\n", $offset);
  56. } else if (mysqli_errno($mysqli) > 0) {
  57. printf("[%03d + 3] Error indicated through links array: %d/%s",
  58. $offset, mysqli_errno($mysqli), mysqli_error($mysqli));
  59. } else {
  60. printf("[%03d + 4] Cannot fetch and no error set - non resultset query (no SELECT)!\n", $offset);
  61. }
  62. }
  63. foreach ($errors as $mysqli)
  64. printf("[%03d + 5] Error on %d: %d/%s\n",
  65. $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
  66. foreach ($reject as $mysqli)
  67. printf("[%03d + 6] Rejecting thread %d: %d/%s\n",
  68. $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
  69. }
  70. // Connections on which no query has been send - 1
  71. $link = get_connection();
  72. $links = array($link);
  73. $errors = array($link);
  74. $reject = array($link);
  75. poll_async(12, $link, $links, $errors, $reject, 0, false);
  76. mysqli_close($link);
  77. $link = get_connection();
  78. $links = array($link);
  79. $errors = array($link);
  80. $reject = array($link);
  81. poll_async(13, $link, $links, $errors, $reject, 0, true);
  82. mysqli_close($link);
  83. // Connections on which no query has been send - 2
  84. // Difference: pass $links twice
  85. $link = get_connection();
  86. $links = array($link, $link);
  87. $errors = array($link, $link);
  88. $reject = array();
  89. poll_async(14, $link, $links, $errors, $reject, 0, false);
  90. // Connections on which no query has been send - 3
  91. // Difference: pass two connections
  92. $link = get_connection();
  93. $links = array($link, get_connection());
  94. $errors = array($link, $link);
  95. $reject = array();
  96. poll_async(15, $link, $links, $errors, $reject, 0, false);
  97. // Reference mess...
  98. $link = get_connection();
  99. $links = array($link);
  100. $errors = array($link);
  101. $ref_errors =& $errors;
  102. $reject = array();
  103. poll_async(16, $link, $links, $ref_errors, $reject, 0, false);
  104. print "done!";
  105. ?>
  106. --EXPECTF--
  107. mysqli_poll(): Argument #4 ($seconds) must be greater than or equal to 0
  108. mysqli_poll(): Argument #5 ($microseconds) must be greater than or equal to 0
  109. mysqli object is already closed
  110. [012 + 6] Rejecting thread %d: 0/
  111. [013 + 6] Rejecting thread %d: 0/
  112. [014 + 6] Rejecting thread %d: 0/
  113. [014 + 6] Rejecting thread %d: 0/
  114. [015 + 6] Rejecting thread %d: 0/
  115. [015 + 6] Rejecting thread %d: 0/
  116. [016 + 6] Rejecting thread %d: 0/
  117. done!