###############################################################################
#
# Any failure to enable the read mode in the server for data protection
# shall result in a server abort.
#
# Test:
#   0. A group of 3 members in multi primary mode.
#   1. Set server 1 to fail when setting the read mode
#   2. Execute a change to single primary mode with server 2 as the primary
#   3. Wait for server 1 to crash and restart it.
#      Check other members see it leave
#   4. Check the error message is in the log
#   5. Cleanup
#
###############################################################################

--source include/have_debug.inc
--source include/not_valgrind.inc
--source include/have_group_replication_plugin.inc
--let $rpl_server_count= 3
--source include/group_replication.inc

--echo
--echo # 1. Set server 1 to fail when setting the read

#This is cleaned on restart
SET @@GLOBAL.DEBUG= '+d,group_replication_read_mode_error';

# Prepare server 1 for impending abort()
--exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect

--echo
--echo # 2. Execute a change to single primary mode with server 2 as the primary

--let $rpl_connection_name= server2
--source include/rpl_connection.inc

--let $server2_uuid= query_get_value(SELECT @@SERVER_UUID, @@SERVER_UUID, 1)

--replace_result $server2_uuid MEMBER2_UUID
--send_eval SELECT group_replication_switch_to_single_primary_mode("$server2_uuid")

--echo
--echo # 3. Wait for server 1 to crash and restart it
--echo # Check other members see it leave

--let $rpl_connection_name= server1
--source include/rpl_connection.inc

--source include/wait_until_disconnected.inc

--exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
--enable_reconnect
--source include/wait_until_connected_again.inc

--let $rpl_server_number= 1
--source include/rpl_reconnect.inc

--let $rpl_connection_name= server_2
--source include/rpl_connection.inc

--let $group_replication_number_of_members= 2
--source include/gr_wait_for_number_of_members.inc

--let $rpl_connection_name= server2
--source include/rpl_connection.inc

--replace_result $server2_uuid MEMBER2_UUID
--reap

--echo # 4. Check the error message is in the log

--let $assert_file= $MYSQLTEST_VARDIR/log/mysqld.1.err
--let $assert_count = 1
--let $assert_select = Cannot enable the super read only mode on a secondary member.
--let $assert_text = Found the expected error in server log
--source include/assert_grep.inc

--echo # 5. Cleanup

--let $rpl_connection_name= server1
--source include/rpl_connection.inc

set session sql_log_bin=0;
call mtr.add_suppression("The plugin encountered a critical error and will abort: Cannot enable the super read only mode on a secondary member.*");
call mtr.add_suppression("Error, maximum number of retries exceeded when waiting for the internal server session state to be operating");
call mtr.add_suppression("Failed to establish an internal server connection to execute plugin operations");
call mtr.add_suppression("There was an issue on the primary election process.*");
call mtr.add_suppression("The server was automatically set into read only mode after an error was detected.");
call mtr.add_suppression("Skipping leave operation: concurrent attempt to leave the group is on-going.");
call mtr.add_suppression("Error while executing a group configuration operation: This operation was locally aborted and for that reason terminated.*");
set session sql_log_bin=1;

--let $rpl_group_replication_single_primary_mode=1
--let $rpl_group_replication_reset_persistent_vars=1
--source include/group_replication_end.inc
