-- source include/have_falcon.inc
-- source include/not_embedded.inc

#
# This test was derived from innodb_deadlock.test,
# which sources in include/deadlock.inc.
#
# As Falcon has different behavior than InnoDB,
# we rewrote the parts in question.
#
--echo *** falcon_deadlock ***

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

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

--echo # Establish connection conn1 (user=root)
connect (conn1,localhost,root,,);
eval SET @@storage_engine = $engine;

--echo # Establish connection conn2 (user=root)
connect (conn2,localhost,root,,);
eval SET @@storage_engine = $engine;

# ----------------------------------------------------- #
# --- Test                                          --- #
# ----------------------------------------------------- #
# Testing of SELECT ... FOR UPDATE, first take.
#
--echo # Switch to connection conn1
connection conn1;
CREATE TABLE t1 (
  id integer,
  x integer
);
INSERT INTO t1 VALUES (0, 0);
SET @@autocommit = 0;
SELECT * FROM t1 WHERE id = 0 FOR UPDATE;

--echo # Switch to connection conn2
connection conn2;
SET @@autocommit = 0;

# The following query should hang because conn1 is locking the record
--real_sleep 1
--send UPDATE t1 SET x = 2 WHERE id = 0

--echo # Switch to connection conn1
connection conn1;
UPDATE t1 SET x = 1 WHERE id = 0;
SELECT * FROM t1;
COMMIT;

--echo # Switch to connection conn2
connection conn2;
# If we set Falcon to
#     falcon_consistent_read = on
#
# we should get a
#     Record has changed since last read in table 't1'
# here. Please note that falcon_consistent_read = on
# is the default setting.
--error ER_CHECKREAD
--reap
ROLLBACK;

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

DROP TABLE t1;

#
# Testing of SELECT ... FOR UPDATE, second take.
#
--echo # Switch to connection conn1
connection conn1;
CREATE TABLE t1 (
  id integer,
  x integer
);

CREATE TABLE t2 (
  a integer,
  b integer
);

INSERT INTO t1 VALUES (0, 0), (300, 300);
INSERT INTO t2 VALUES (0, 10), (1, 20), (2, 30);
COMMIT;

SELECT * FROM t2;
UPDATE t2 SET b = 100 WHERE a = (SELECT x FROM t1 WHERE id = a FOR UPDATE);
SELECT * FROM t2;
SELECT * FROM t1;

--echo # Switch to connection conn2
connection conn2;

# The following query should hang because conn1 is locking the record
--real_sleep 1
--send UPDATE t1 SET x = 2 WHERE id = 0

--echo # Switch to connection conn1
connection conn1;
UPDATE t1 SET x = 1 WHERE id = 0;
SELECT * FROM t1;
COMMIT;

--echo # Switch to connection conn2
connection conn2;
# If we set Falcon to
#     falcon_consistent_read = on
#
# we should get a
#     Record has changed since last read in table 't1'
# here. Please note that falcon_consistent_read = on
# is the default setting.
--error ER_CHECKREAD
--reap
ROLLBACK;

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

DROP TABLE t1;
DROP TABLE t2;

#
# Testing of SELECT ... FOR UPDATE, third take.
#
CREATE TABLE t1 (
  id integer,
  x integer
);

CREATE TABLE t2 (
  a integer,
  b integer
);

INSERT INTO t1 VALUES (0, 0), (300, 300);
INSERT INTO t2 VALUES (0, 0), (1, 20), (2, 30);
COMMIT;

SELECT a, b FROM t2 UNION SELECT id, x FROM t1 FOR UPDATE;
SELECT * FROM t2;
SELECT * FROM t1;

--echo # Switch to connection conn2
connection conn2;

UPDATE t2 SET a = 2 WHERE b = 0;
SELECT * FROM t2;
# The following query should hang because conn1 is locking the record
--real_sleep 1
--send UPDATE t1 SET x = 2 WHERE id = 0

--echo # Switch to connection conn1
connection conn1;
UPDATE t1 SET x = 1 WHERE id = 0;
SELECT * FROM t1;
COMMIT;

--echo # Switch to connection conn2
connection conn2;
# If we set Falcon to
#     falcon_consistent_read = on
#
# we should get a
#     Record has changed since last read in table 't1'
# here. Please note that falcon_consistent_read = on
# is the default setting.
--error ER_CHECKREAD
--reap
ROLLBACK;

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

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

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