stop slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
reset master;
reset slave;
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t9;
start slave;

# BUG#56118 STOP SLAVE does not wait till trx with CREATE TMP TABLE ends
#
# If a temporary table is created or dropped, the transaction should be
# regarded similarly that a non-transactional table is modified. So 
# STOP SLAVE should wait until the transaction has finished.
CREATE TABLE t1(c1 INT) ENGINE=InnoDB;
CREATE TABLE t2(c1 INT) ENGINE=InnoDB;
SET DEBUG_SYNC= 'RESET';
include/stop_slave.inc

# Suspend the INSERT statement in current transaction on SQL thread.
# It guarantees that SQL thread is applying the transaction when
# STOP SLAVE command launchs.
SET GLOBAL debug= 'd,after_mysql_insert';
include/start_slave.inc

# CREATE TEMPORARY TABLE with InnoDB engine
# -----------------------------------------

[ On Master ]
BEGIN;
DELETE FROM t1;
CREATE TEMPORARY TABLE tt1(c1 INT) ENGINE = InnoDB;
INSERT INTO t1 VALUES (1);
DROP TEMPORARY TABLE tt1;
COMMIT;

[ On Slave ]
STOP SLAVE SQL_THREAD;

[ On Slave1 ]
# To resume slave SQL thread
SET DEBUG_SYNC= 'now SIGNAL signal.continue';
SET DEBUG_SYNC= 'RESET';

[ On Slave ]
# Slave should stop after the transaction has committed.
# So t1 on master is same to t1 on slave.
Comparing tables master:test.t1 and slave:test.t1
START SLAVE SQL_THREAD;

# CREATE TEMPORARY TABLE with MyISAM engine
# -----------------------------------------

[ On Master ]
BEGIN;
DELETE FROM t1;
CREATE TEMPORARY TABLE tt1(c1 INT) ENGINE = MyISAM;
INSERT INTO t1 VALUES (1);
DROP TEMPORARY TABLE tt1;
COMMIT;

[ On Slave ]
STOP SLAVE SQL_THREAD;

[ On Slave1 ]
# To resume slave SQL thread
SET DEBUG_SYNC= 'now SIGNAL signal.continue';
SET DEBUG_SYNC= 'RESET';

[ On Slave ]
# Slave should stop after the transaction has committed.
# So t1 on master is same to t1 on slave.
Comparing tables master:test.t1 and slave:test.t1
START SLAVE SQL_THREAD;

# CREATE TEMPORARY TABLE ... SELECT with InnoDB engine
# ----------------------------------------------------

[ On Master ]
BEGIN;
DELETE FROM t1;
CREATE TEMPORARY TABLE tt1(c1 INT) ENGINE = InnoDB
SELECT c1 FROM t2;
INSERT INTO t1 VALUES (1);
DROP TEMPORARY TABLE tt1;
COMMIT;

[ On Slave ]
STOP SLAVE SQL_THREAD;

[ On Slave1 ]
# To resume slave SQL thread
SET DEBUG_SYNC= 'now SIGNAL signal.continue';
SET DEBUG_SYNC= 'RESET';

[ On Slave ]
# Slave should stop after the transaction has committed.
# So t1 on master is same to t1 on slave.
Comparing tables master:test.t1 and slave:test.t1
START SLAVE SQL_THREAD;

# CREATE TEMPORARY TABLE ... SELECT with MyISAM engine
# ----------------------------------------------------

[ On Master ]
BEGIN;
DELETE FROM t1;
CREATE TEMPORARY TABLE tt1(c1 INT) ENGINE = MyISAM
SELECT 1 AS c1;
INSERT INTO t1 VALUES (1);
DROP TEMPORARY TABLE tt1;
COMMIT;

[ On Slave ]
STOP SLAVE SQL_THREAD;

[ On Slave1 ]
# To resume slave SQL thread
SET DEBUG_SYNC= 'now SIGNAL signal.continue';
SET DEBUG_SYNC= 'RESET';

[ On Slave ]
# Slave should stop after the transaction has committed.
# So t1 on master is same to t1 on slave.
Comparing tables master:test.t1 and slave:test.t1
START SLAVE SQL_THREAD;
# Test end
SET GLOBAL debug= '$debug_save';
DROP TABLE t1, t2;
