--source include/have_falcon.inc
--source include/big_test.inc

#
# Bug #34351: Update Conflict on non-overlapping transactions
# This test works because it uses FALCON_CONSISTENT_READ=OFF
#
# Test C-big: Running loop of 20 000 iterations inside procedure.
# Original test ran 10k iterations but would sometimes time
# out in Pushbuild (automatic testing) - Bug#35503.
# 
# Original test (falcon_bug_24351_C.test) changed to run the 
# loop 5k times per procedure call instead, hoping that it 
# would still be able to detect a regression. 
# This -big test was created as an extra precaution, in 
# case 5k iterations is not enough.
#
#
--echo *** Bug #34351_C-big ***

# ----------------------------------------------------- #
# --- 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;

delimiter //;
CREATE PROCEDURE p1 ()
begin
  DECLARE my_count INT DEFAULT 0;
  DECLARE my_uuid CHAR(36) DEFAULT 0;
  SET FALCON_CONSISTENT_READ=OFF;
  while my_count < 20000 do
    SET my_uuid = UUID();
    INSERT INTO t1 (t1_uuid) VALUES (my_uuid);
    DELETE FROM t1 WHERE t1_uuid IN (my_uuid);
    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;
