--source include/have_falcon.inc
--source include/big_test.inc
#
# Bug #22150: Error "Can't find record"
#   Slightly modified not to output randon numbers. Run
#   this test with --big-test option.
#
--echo *** Bug #22150 ***

# ----------------------------------------------------- #
# --- Initialisation                                --- #
# ----------------------------------------------------- #
let $engine = 'Falcon';
eval SET @@storage_engine = $engine;

--disable_warnings
DROP TABLE IF EXISTS t1;
DROP PROCEDURE IF EXISTS p1;
--enable_warnings

--echo # Establish connection conn1 (user = root)
connect (conn1,localhost,root,,);
SET @@autocommit = 1;

--echo # Switch to connection default
connection default;
SET @@autocommit = 1;
CREATE TABLE t1 (a int(11), KEY a (a));

DELIMITER //;
CREATE PROCEDURE p1()
BEGIN
  DECLARE v1 int default 0;
  DECLARE v2 int;
  DECLARE CONTINUE HANDLER FOR 1020 BEGIN END;
  DECLARE CONTINUE HANDLER FOR 1213 BEGIN END;
  WHILE v1 < 100000 DO
    /* SELECT 'insert', v1; */
    INSERT INTO t1 VALUES (v1);
    SET v2 = rand() * 10000;
    UPDATE t1 SET a = v2 WHERE a = v1;
    /* SELECT ' update', v1; */
    SET v1 = v1 + 1;
    IF v1 MOD 50 = 0 THEN
      /* SELECT ' delete'; */
      DELETE FROM t1;
    END IF;
  END WHILE;
END//
DELIMITER ;//

# ----------------------------------------------------- #
# --- Test                                          --- #
# ----------------------------------------------------- #
--echo # Sent call p1() to the server but do not pull the results
--send CALL p1()

--echo # Switch to connection conn1
connection conn1;
--real_sleep 1
CALL p1();

--echo # Switch to connection default
connection default;
--echo # Pull the results of the preceeding call p1()
--reap
--echo # Sent call p1() to the server but do not pull the results
--real_sleep 1
--send CALL p1()

--echo # Switch to connection conn1
connection conn1;
--real_sleep 1
CALL p1();

--echo # Switch to connection default
connection default;
--echo # Pull the results of the preceeding call p1()
--reap

# ----------------------------------------------------- #
# --- Check                                         --- #
# ----------------------------------------------------- #
SELECT count(*) FROM t1;

# ----------------------------------------------------- #
# --- Final cleanup                                 --- #
# ----------------------------------------------------- #
--echo # Disconnect conn1
disconnect conn1;
DROP TABLE t1;
DROP PROCEDURE p1;
