--source include/have_falcon.inc
#--disable_abort_on_error

#
# Bug #29151: Falcon: running sysbench 0.4.8 leads to duplicate key errors
#
--echo *** Bug #29151 ***

# -----------------------------------------------------
--echo *** Initialization for 'InnoDB Compatibility Mode'
# -----------------------------------------------------
SET @@storage_engine = Falcon;
SET falcon_consistent_read = off;

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

CREATE TABLE t1 (a int, b int, c char(10), PRIMARY KEY (a));

--echo # Establish connection conn1
connect (conn1,localhost,root,,);
SET falcon_consistent_read = off;

--echo # Establish connection conn2
connect (conn2,localhost,root,,);
SET falcon_consistent_read = off;
INSERT INTO t1 VALUES(490, 0, 'inserted');

# -------------------------------------------------------
--echo *** Test if an UPDATE will get the new record after waiting
# -------------------------------------------------------
--echo # Switch to connection conn1
connection conn1;
BEGIN;
UPDATE t1 SET c = 'updated' WHERE a = 490;
DELETE FROM t1 WHERE a = 490;
INSERT INTO t1 VALUES(490, 1, 'inserted');
SELECT * FROM t1;

--echo # Switch to connection conn2
connection conn2;
BEGIN;
SELECT * FROM t1;
# This one blocks until other transaction COMMITs.
--send UPDATE t1 SET c = 'updated' WHERE a = 490

--echo # Switch to connection conn1
connection conn1;
SELECT * FROM t1;
--real_sleep 1
COMMIT;

--echo # Switch to connection conn2
connection conn2;
--reap
DELETE FROM t1 WHERE a = 490;
INSERT INTO t1 VALUES(490, 2, 'inserted');
COMMIT;
SELECT * FROM t1;

# -------------------------------------------------------
--echo *** Test if a DELETE will get the new record after waiting
# -------------------------------------------------------

BEGIN;
UPDATE t1 SET c = 'updated' WHERE a = 490;
DELETE FROM t1 WHERE a = 490;
INSERT INTO t1 VALUES(490, 3, 'inserted');

--echo # Switch to connection conn1
connection conn1;
BEGIN;
SELECT * FROM t1;
--send DELETE FROM t1 WHERE a = 490

--echo # Switch to connection conn2
connection conn2;
SELECT * FROM t1;
--real_sleep 1
COMMIT;

--echo # Switch to connection conn1
connection conn1;
--reap
SELECT * FROM t1;
COMMIT;
SELECT * FROM t1;
DELETE FROM t1 WHERE a = 490;
SELECT * FROM t1;

# -------------------------------------------------------
--echo *** Test if an INSERT will wait on a pending non-block
# -------------------------------------------------------

INSERT INTO t1 VALUES(490, 4, 'inserted');
BEGIN;
UPDATE t1 SET c = 'updated' WHERE a = 490;
DELETE FROM t1 WHERE a = 490;
SELECT * FROM t1;

--echo # Switch to connection conn2
connection conn2;
BEGIN;
SELECT * FROM t1;
--send INSERT INTO t1 VALUES(490, 5, 'inserted')

--echo # Switch to connection conn1
connection conn1;
SELECT * FROM t1;
--real_sleep 1
COMMIT;

--echo # Switch to connection conn2
connection conn2;
--reap
SELECT * FROM t1;
COMMIT;

--echo # Switch to connection conn1
connection conn1;
SELECT * FROM t1;

# ---------------------------------------------
# --- Final cleanup
# ---------------------------------------------
--echo # Switch to connection default
connection default;
disconnect conn1;
disconnect conn2;
DROP TABLE t1;
