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

#
# 29151_A: Test the Consistent Read Isolation mode
#          when a blocking transaction aborts.
#
--echo *** Bug #29151 ***

# -----------------------------------------------------
# --- Initialization for Falcon's Consistent Read
# -----------------------------------------------------
eval SET @@storage_engine = Falcon;
SET falcon_consistent_read = on;

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

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

# We need to set up an exlipicit connection here,
# as SET GLOBAL does not set the current connection's setting.
--echo # Establish connection conn1
connect (conn1,localhost,root,,);

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

# -------------------------------------------------------------------
# Test if an UPDATE will succeed after waiting on a trans that aborts
# -------------------------------------------------------------------
--echo # Switch to connection conn1
connection conn1;
SET falcon_consistent_read = on;
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;
SET falcon_consistent_read = on;
BEGIN;
SELECT * FROM t1;
# This one blocks in case the other transaction aborts.
--send UPDATE t1 SET c = 'updated' WHERE a = 490

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

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


# -------------------------------------------------------------------
# Test if a DELETE will succeed after waiting on a trans that aborts
# -------------------------------------------------------------------

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
ROLLBACK;
SELECT * FROM t1;

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

# -------------------------------------------------------------------
# Test if an INSERT will succeed after waiting on a trans that aborts
# -------------------------------------------------------------------

BEGIN;
INSERT INTO t1 VALUES(490, 4, 'inserted');

--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
ROLLBACK;
SELECT * FROM t1;

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