# ==== Purpose ====
#
# This test verifies correctness and effectiveness of the
# until-stopping when the position used is inside the Gtid_log_event
# of a group (transaction) in the relaylog.
#
# ==== Method ====
#
# The test will create a two events transaction (GTID + QUERY), and will start
# the SQL thread using the UNTIL MASTER_LOG_FILE/POS clause, pointing to the
# first byte of the created transaction (inside of the Gtid_log_event).
#
# The test expects that, after issuing the START SLAVE SQL_THREAD with the
# UNTIL clause, the SQL thread will apply the created transaction (both
# GTID and QUERY events) even though pointing to stop after the GTID.
# The test also expects that the SQL thread will update its position
# before stopping.
#
# The test will then verify SQL thread's binlog file, position, and
# finally will wait for the SQL thread to stop.
#
# When the SQL thread stops, the test verifies if the second event of
# the transaction (QUERY) was successfully applied.
#
# ==== Related bugs ====
#
# Bug #18306199 START SLAVE UNTIL MASTER_LOG_POS=MIDDLE-OF-GTID-EVENT
#               STOPS BEFORE TRANSACTION

--source include/have_binlog_format_statement.inc
--source include/have_gtid.inc
--source include/master-slave.inc

# Stop the SQL thread
--source include/rpl_connection_slave.inc
--source include/stop_slave_sql.inc

--echo # Saving master position to use on UNTIL clause of START SLAVE
--source include/rpl_connection_master.inc
--let $master_file= query_get_value(SHOW MASTER STATUS, File, 1)
--let $master_pos= query_get_value(SHOW MASTER STATUS, Position, 1)
--inc $master_pos

# Create a table in the master (one event without GTIDS, two with GTIDS)
CREATE TABLE t1 (a INT) ENGINE=InnoDB;

# Insert some data
BEGIN;
INSERT INTO t1 VALUES (1);
COMMIT;

# Sync the IO thread with the master
--source include/sync_slave_io_with_master.inc

# Check if SQL thread stops correctly
# after executing the next event of the master binlog
# based on master binlog file and position

--echo # Start SQL thread until it executed the CREATE TABLE
--replace_result $master_file MASTER_FILE $master_pos MASTER_POS
# Disable warnings to avoid logging "UNTIL condtion is not supported ..." on MTS
--disable_warnings
eval START SLAVE SQL_THREAD UNTIL MASTER_LOG_FILE = "$master_file", MASTER_LOG_POS = $master_pos;
--enable_warnings

--echo # Wait until SQL thread reaches last master binlog file
--let $slave_param= Relay_Master_Log_File
--let $slave_param_value= $master_file
--let $slave_param_comparison= =
--source include/wait_for_slave_param.inc

--echo # Wait until SQL thread reaches desired master binlog position
--let $slave_param= Exec_Master_Log_Pos
--let $slave_param_value= $master_pos
--let $slave_param_comparison= >=
--source include/wait_for_slave_param.inc

--echo # If the desired position was reached, SQL thread should stop.
--source include/wait_for_slave_sql_to_stop.inc

--let $assert_cond= "[SHOW TABLES LIKE "t1"]" = "t1"
--let $assert_text= t1 should be replicated
--source include/assert.inc

# Cleanup
--source include/start_slave_sql.inc
--source include/rpl_connection_master.inc
DROP TABLE t1;

# End of tests
--source include/rpl_end.inc
