-- source include/master-slave.inc

# There is no point in running this test in more
# than once, ie, one for each binary log format.
-- source include/have_binlog_format_row.inc

#
# BUG#43535: last_io_error in show slave status is confusing
#

--let $ts_regexp= [0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]

############# CHECKS SQL ERRORS #############

--let $field= Last_SQL_Error_Timestamp

-- connection master
CREATE TABLE t1 (a INT, b blob, PRIMARY KEY(b(512)));
--source include/sync_slave_sql_with_master.inc
# action: remove table on the slave to force SQL error
DROP TABLE t1;

-- connection master
# action: drop the table on the master
DROP TABLE t1; 

-- connection slave
# action: now  wait for the slave to stop because it cannot
#         remove a table that does not exist
let $slave_sql_errno=1051; # ER_BAD_TABLE_ERROR
call mtr.add_suppression("Slave SQL: .*Error .Unknown table .test.t1.. on query.* Error_code: 1051");
call mtr.add_suppression("The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state");

-- source include/wait_for_slave_sql_error.inc

--let $errts0= query_get_value("SHOW SLAVE STATUS", $field, 1)
--let $assert_text= $field is not null and matches the expected format
--let $assert_cond= `SELECT ("$errts0" REGEXP "$ts_regexp")`
--source include/assert.inc

# action: stop the slave (this should preserve the error)
-- source include/stop_slave.inc
--let $assert_cond= "$errts0" = "[SHOW SLAVE STATUS, $field, 1]"
--let $assert_text= $field matches the one reported before stopping slave threads
--source include/assert.inc

-- connection slave
# action: create the table again on the slave so that it resumes replication
#         correctly when we restart the slave
CREATE TABLE  t1 (a INT, b blob, PRIMARY KEY(b(512)));
-- let $sql_errno= query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1)
-- echo Last_SQL_Errno: $sql_errno
-- source include/start_slave.inc

-- echo # assertion: show that the error is not reported anymore since replication
-- echo #            has resumed correctly
let $slave_param= Last_SQL_Errno;
let $slave_param_comparison= =; 
let $slave_param_value= 0;
let $slave_error_message= Failed while waiting for slave to clear the SQL error after starting;
source include/wait_for_slave_param.inc;


############# CHECKS IO ERRORS #############

--let $rpl_only_running_threads= 1
--source include/rpl_reset.inc
--let $field= Last_IO_Error_Timestamp

# mostly copied and extended from rpl_server_id1.test
-- connection slave

# replicate ourselves
-- source include/stop_slave.inc
-- replace_result $SLAVE_MYPORT SLAVE_PORT
-- eval change master to master_port=$SLAVE_MYPORT
START SLAVE;

-- let $slave_param= Last_IO_Errno
-- let $slave_param_value= 1593
-- source include/wait_for_slave_param.inc
-- echo *** must be having the replicate-same-server-id IO thread error ***

--let $errts0= query_get_value("SHOW SLAVE STATUS", $field, 1)
--let $assert_text= $field is not null and matches the expected format
--let $assert_cond= `SELECT ("$errts0" REGEXP "$ts_regexp")`
--source include/assert.inc

# action: stop the slave
-- source include/stop_slave.inc

--let $assert_cond= "$errts0" = "[SHOW SLAVE STATUS, $field, 1]"
--let $assert_text= $field matches the one reported before stopping slave threads
--source include/assert.inc

# action: restore correct settings
-- replace_result $MASTER_MYPORT MASTER_PORT
-- eval change master to master_port=$MASTER_MYPORT
-- let $io_errno= query_get_value("SHOW SLAVE STATUS", Last_IO_Errno, 1)
-- echo Last_IO_Errno: $io_errno
-- source include/start_slave.inc

-- echo # assertion: show that no error is reported anymore
let $slave_param= Last_IO_Errno;
let $slave_param_comparison= =; 
let $slave_param_value= 0;
let $slave_error_message= Failed while waiting for slave to clear the IO error after starting;
source include/wait_for_slave_param.inc;

--source include/rpl_end.inc
