bug79375.phpt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. --TEST--
  2. Bug #79375: mysqli_store_result does not report error from lock wait timeout
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
  8. require_once('skipifconnectfailure.inc');
  9. if (!defined('MYSQLI_STORE_RESULT_COPY_DATA')) die('skip requires mysqlnd');
  10. ?>
  11. --FILE--
  12. <?php
  13. require_once("connect.inc");
  14. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  15. $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  16. $mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  17. $mysqli->query('DROP TABLE IF EXISTS test');
  18. $mysqli->query('CREATE TABLE test (first int) ENGINE = InnoDB');
  19. $mysqli->query('INSERT INTO test VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9)');
  20. function testStmtStoreResult(mysqli $mysqli, string $name) {
  21. $mysqli->query("SET innodb_lock_wait_timeout = 1");
  22. $mysqli->query("START TRANSACTION");
  23. $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
  24. echo "Running query on $name\n";
  25. $stmt = $mysqli->prepare($query);
  26. $stmt->execute();
  27. try {
  28. $stmt->store_result();
  29. echo "Got {$stmt->num_rows} for $name\n";
  30. } catch(mysqli_sql_exception $e) {
  31. echo $e->getMessage()."\n";
  32. }
  33. }
  34. function testStmtGetResult(mysqli $mysqli, string $name) {
  35. $mysqli->query("SET innodb_lock_wait_timeout = 1");
  36. $mysqli->query("START TRANSACTION");
  37. $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
  38. echo "Running query on $name\n";
  39. $stmt = $mysqli->prepare($query);
  40. $stmt->execute();
  41. try {
  42. $res = $stmt->get_result();
  43. echo "Got {$res->num_rows} for $name\n";
  44. } catch(mysqli_sql_exception $e) {
  45. echo $e->getMessage()."\n";
  46. }
  47. }
  48. function testNormalQuery(mysqli $mysqli, string $name) {
  49. $mysqli->query("SET innodb_lock_wait_timeout = 1");
  50. $mysqli->query("START TRANSACTION");
  51. $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
  52. echo "Running query on $name\n";
  53. try {
  54. $res = $mysqli->query($query);
  55. echo "Got {$res->num_rows} for $name\n";
  56. } catch(mysqli_sql_exception $e) {
  57. echo $e->getMessage()."\n";
  58. }
  59. }
  60. function testStmtUseResult(mysqli $mysqli, string $name) {
  61. $mysqli->query("SET innodb_lock_wait_timeout = 1");
  62. $mysqli->query("START TRANSACTION");
  63. $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
  64. echo "Running query on $name\n";
  65. $stmt = $mysqli->prepare($query);
  66. $stmt->execute();
  67. try {
  68. $stmt->fetch(); // should throw an error
  69. $stmt->fetch();
  70. echo "Got {$stmt->num_rows} for $name\n";
  71. } catch (mysqli_sql_exception $e) {
  72. echo $e->getMessage()."\n";
  73. }
  74. }
  75. function testResultFetchRow(mysqli $mysqli, string $name) {
  76. $mysqli->query("SET innodb_lock_wait_timeout = 1");
  77. $mysqli->query("START TRANSACTION");
  78. $query = "SELECT first FROM test WHERE first = 1 FOR UPDATE";
  79. echo "Running query on $name\n";
  80. $res = $mysqli->query($query, MYSQLI_USE_RESULT);
  81. try {
  82. $res->fetch_row();
  83. $res->fetch_row();
  84. echo "Got {$res->num_rows} for $name\n";
  85. } catch(mysqli_sql_exception $e) {
  86. echo $e->getMessage()."\n";
  87. }
  88. }
  89. testStmtStoreResult($mysqli, 'first connection');
  90. testStmtStoreResult($mysqli2, 'second connection');
  91. $mysqli->close();
  92. $mysqli2->close();
  93. echo "\n";
  94. // try it again for get_result
  95. $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  96. $mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  97. testStmtGetResult($mysqli, 'first connection');
  98. testStmtGetResult($mysqli2, 'second connection');
  99. $mysqli->close();
  100. $mysqli2->close();
  101. echo "\n";
  102. // try it again with unprepared query
  103. $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  104. $mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  105. testNormalQuery($mysqli, 'first connection');
  106. testNormalQuery($mysqli2, 'second connection');
  107. $mysqli->close();
  108. $mysqli2->close();
  109. echo "\n";
  110. // try it again with unprepared query
  111. $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  112. $mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  113. testStmtUseResult($mysqli, 'first connection');
  114. testStmtUseResult($mysqli2, 'second connection');
  115. $mysqli->close();
  116. $mysqli2->close();
  117. echo "\n";
  118. // try it again using fetch_row on a result object
  119. $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  120. $mysqli2 = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
  121. testResultFetchRow($mysqli, 'first connection');
  122. testResultFetchRow($mysqli2, 'second connection');
  123. $mysqli->close();
  124. $mysqli2->close();
  125. ?>
  126. --CLEAN--
  127. <?php
  128. require_once("clean_table.inc");
  129. ?>
  130. --EXPECTF--
  131. Running query on first connection
  132. Got %d for first connection
  133. Running query on second connection
  134. Lock wait timeout exceeded; try restarting transaction
  135. Running query on first connection
  136. Got %d for first connection
  137. Running query on second connection
  138. Lock wait timeout exceeded; try restarting transaction
  139. Running query on first connection
  140. Got %d for first connection
  141. Running query on second connection
  142. Lock wait timeout exceeded; try restarting transaction
  143. Running query on first connection
  144. Got %d for first connection
  145. Running query on second connection
  146. Lock wait timeout exceeded; try restarting transaction
  147. Running query on first connection
  148. Got 1 for first connection
  149. Running query on second connection
  150. Lock wait timeout exceeded; try restarting transaction