--source include/have_falcon.inc
--source include/big_test.inc
#
# Bug #34351: Update Conflict on non-overlapping transactions.
#
# Test A-big: Running loop of 20 000 iterations inside procedure. 
# Original test ran 10k iterations but could potentially time 
# out in Pushbuild (automatic testing). Original test 
# (falcon_bug_24351_A.test) changed to run the loop 5k times 
# per procedure call instead.
#
# This test works because the INSERT and DELETE are in the same 
# transaction and so the INSERT is not seen by other transactions.
#
--echo *** Bug #34351_A ***

# ----------------------------------------------------- #
# --- 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 (
  t1_autoinc INTEGER NOT NULL AUTO_INCREMENT,
  t1_uuid CHAR(36),
  PRIMARY KEY (t1_autoinc), KEY (t1_uuid)
) ENGINE = Falcon;

#   declare continue handler for sqlexception

delimiter //;
CREATE PROCEDURE p1 ()
begin
  DECLARE my_count INT DEFAULT 0;
  DECLARE my_uuid CHAR(36) DEFAULT 0;
  while my_count < 20000 do
    SET my_uuid = UUID();
	START TRANSACTION;
    INSERT INTO t1 (t1_uuid) VALUES (my_uuid);
    DELETE FROM t1 WHERE t1_uuid IN (my_uuid);
    COMMIT;
    SET my_count = my_count + 1;
  end while;
end//
delimiter ;//

# ----------------------------------------------------- #
# --- Test                                          --- #
# ----------------------------------------------------- #

--echo # Send 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 # Send 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                                         --- #
# ----------------------------------------------------- #
# Checking row count is not applicable here.
SELECT count(*) FROM t1;

# ----------------------------------------------------- #
# --- Final cleanup                                 --- #
# ----------------------------------------------------- #
DROP PROCEDURE p1;
DROP TABLE t1;
