PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysqli/tests/mysqli_poll_kill.php

http://github.com/facebook/hiphop-php
PHP | 160 lines | 116 code | 31 blank | 13 comment | 34 complexity | b8137f446c9564d5fcc7296033ba9834 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once('connect.inc');
  3. function get_connection() {
  4. global $host, $user, $passwd, $db, $port, $socket;
  5. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  6. printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  7. return $link;
  8. }
  9. // Killing connection - 1
  10. $link = get_connection();
  11. if (true !== ($tmp = mysqli_query($link, "SELECT 1 AS 'processed before killed'", MYSQLI_ASYNC | MYSQLI_USE_RESULT)))
  12. printf("[002] Expecting boolean/true got %s/%s\n", gettype($tmp), var_export($tmp, true));
  13. // Sleep 0.1s - the asynchronous query should have been processed after the wait period
  14. usleep(100000);
  15. $thread_id = mysqli_thread_id($link);
  16. mysqli_kill(get_connection(), $thread_id);
  17. $links = array($link);
  18. $errors = array($link);
  19. $reject = array($link);
  20. // Yes, 1 - the asynchronous query should have been processed
  21. if (1 !== ($tmp = (mysqli_poll($links, $errors, $reject, 0, 10000))))
  22. printf("[003] Expecting int/1 got %s/%s\n", gettype($tmp), var_export($tmp, true));
  23. if (!is_array($links) || empty($links))
  24. printf("[004] Expecting non-empty array got %s/%s\n", gettype($links), var_export($links, true));
  25. else
  26. foreach ($links as $link) {
  27. if (is_object($res = mysqli_reap_async_query($link))) {
  28. // Yes, you can fetch a result - the query has been processed
  29. var_dump(mysqli_fetch_assoc($res));
  30. mysqli_free_result($res);
  31. } else if ($link->errno > 0) {
  32. printf("[005] Error: %d\n", $link->errno);
  33. }
  34. }
  35. // No error!
  36. if (!is_array($errors) || !empty($errors))
  37. printf("[006] Expecting non-empty array got %s/%s\n", gettype($errors), var_export($errors, true));
  38. if (!is_array($reject) || !empty($reject))
  39. printf("[007] Expecting empty array got %s/%s\n", gettype($reject), var_export($reject, true));
  40. // Lets pass a dead connection
  41. $links = array($link);
  42. $errors = array($link);
  43. $reject = array($link);
  44. if (0 !== ($tmp = mysqli_poll($links, $errors, $reject, 1)))
  45. printf("[008] There should be no connection ready! Returned %s/%s, expecting int/0.\n",
  46. gettype($tmp), var_export($tmp, true));
  47. if (!empty($errors))
  48. printf("[009] There should be no errors but one rejected connection\n");
  49. foreach ($reject as $mysqli)
  50. if (mysqli_thread_id($mysqli) != $thread_id) {
  51. printf("[010] Rejected thread %d should have rejected thread %d\n",
  52. mysqli_thread_id($mysqli), $thread_id);
  53. }
  54. // Killing connection - 2
  55. $link = get_connection();
  56. if (true !== ($tmp = mysqli_query($link, "SELECT 1", MYSQLI_ASYNC | MYSQLI_USE_RESULT)))
  57. printf("[011] Expecting boolean/true got %s/%s\n", gettype($tmp), var_export($tmp, true));
  58. usleep(100000);
  59. $thread_id = mysqli_thread_id($link);
  60. mysqli_kill(get_connection(), $thread_id);
  61. // Yes, 1 - fetch OK packet of kill!
  62. $processed = 0;
  63. do {
  64. $links = array($link, $link);
  65. $errors = array($link, $link);
  66. $reject = array($link, $link);
  67. $ready = mysqli_poll($links, $errors, $reject, 1);
  68. if (!empty($errors)) {
  69. foreach ($errors as $mysqli) {
  70. printf("[012] Error on thread %d: %s/%s\n",
  71. mysqli_thread_id($mysqli),
  72. mysqli_errno($mysqli),
  73. mysqli_error($mysqli));
  74. }
  75. break;
  76. }
  77. if (!empty($reject)) {
  78. foreach ($reject as $mysqli) {
  79. printf("[013] Rejecting thread %d: %s/%s\n",
  80. mysqli_thread_id($mysqli),
  81. mysqli_errno($mysqli),
  82. mysqli_error($mysqli));
  83. }
  84. $processed += count($reject);
  85. }
  86. foreach ($links as $mysqli) {
  87. if (is_object($res = mysqli_reap_async_query($mysqli))) {
  88. printf("Fetching from thread %d...\n", mysqli_thread_id($mysqli));
  89. var_dump(mysqli_fetch_assoc($res));
  90. } else if (mysqli_errno($mysqli) > 0) {
  91. printf("[014] %d/%s\n", mysqli_errno($mysqli), mysqli_error($mysqli));
  92. }
  93. $processed++;
  94. }
  95. } while ($processed < 2);
  96. // Killing connection - 3
  97. $link = get_connection();
  98. $thread_id = mysqli_thread_id($link);
  99. mysqli_kill(get_connection(), $thread_id);
  100. // Sleep 0.1s to ensure the KILL gets recognized
  101. usleep(100000);
  102. if (false !== ($tmp = mysqli_query($link, "SELECT 1 AS 'processed before killed'", MYSQLI_ASYNC | MYSQLI_USE_RESULT)))
  103. printf("[015] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
  104. $links = array($link);
  105. $errors = array($link);
  106. $reject = array($link);
  107. if (0 !== ($tmp = (mysqli_poll($links, $errors, $reject, 0, 10000))))
  108. printf("[016] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));
  109. if (!is_array($links) || empty($links))
  110. printf("[017] Expecting non-empty array got %s/%s\n", gettype($links), var_export($links, true));
  111. else
  112. foreach ($links as $link) {
  113. if (is_object($res = mysqli_reap_async_query($link))) {
  114. // No, you cannot fetch the result
  115. var_dump(mysqli_fetch_assoc($res));
  116. mysqli_free_result($res);
  117. } else if ($link->errno > 0) {
  118. // But you are supposed to handle the error the way its shown here!
  119. printf("[018] Error: %d/%s\n", $link->errno, $link->error);
  120. }
  121. }
  122. // None of these will indicate an error, check errno on the list of returned connections!
  123. if (!is_array($errors) || !empty($errors))
  124. printf("[019] Expecting non-empty array got %s/%s\n", gettype($errors), var_export($errors, true));
  125. if (!is_array($reject) || !empty($reject))
  126. printf("[020] Expecting empty array got %s/%s\n", gettype($reject), var_export($reject, true));
  127. mysqli_close($link);
  128. print "done!";
  129. ?>