###############################################################################
#
# When a member has recovery blocked and is stopped, STOP GROUP_REPLICATION
# will return with an warning and log an error about the not stopping the
# threads.
#
# Test:
#   0. This test requires two servers
#   1. Bootstrap the group on server1
#   2. Set a lower components timeout and lock tables on server2.
#      Start group replication that will block when applying VCLE
#   3. Wait recovery is on hold due lock on tables and Stop Group
#      Replication.
#   4. Starting server2 will fail, recovery module fail to stop, as it is
#      blocked
#   5. UNLOCK tables on server2 and recovery module shall stop. Now Group
#      Replication is able to start
#   6. Assert that even stop command returns a warning is thrown as
#      recovery fails to stop
#   7. Cleanup
#
###############################################################################

--source include/big_test.inc
--source include/not_have_privilege_checks_user.inc
--source include/have_group_replication_plugin.inc
--let $rpl_server_count= 2
--let $rpl_skip_group_replication_start= 1
--source include/group_replication.inc

--echo
--echo # 1. Bootstrap the group on server1

--source include/start_and_bootstrap_group_replication.inc

--echo
--echo # 2. Set a lower components timeout and lock tables on server2.
--echo #    Start group replication that will block when applying VCLE

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

SET @group_replication_components_stop_timeout_save = @@GLOBAL.group_replication_components_stop_timeout;
SET GLOBAL group_replication_components_stop_timeout = 30;

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

SET GLOBAL super_read_only= 1;
FLUSH TABLES WITH READ LOCK;

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

--let $group_replication_start_member_state= RECOVERING
--source include/start_group_replication.inc

--echo
--echo # 3. Wait recovery is on hold due lock on tables and Stop Group
--echo #    Replication.

--let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist WHERE user='system user' AND state='Waiting for commit lock'
--source include/wait_condition.inc

--source include/stop_group_replication.inc

--echo
--echo # 4. Starting server2 will fail, recovery module fail to stop, as it is
--echo #    blocked

--error ER_GRP_RPL_RECOVERY_CHANNEL_STILL_RUNNING
START GROUP_REPLICATION;

--echo
--echo # 5. UNLOCK tables on server2 and recovery module shall stop. Now Group
--echo #    Replication is able to start

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

--source include/start_group_replication.inc

--echo # 6. Assert that even stop command returns a warning is thrown as
--echo #    recovery fails to stop

--let $assert_file= $MYSQLTEST_VARDIR/log/mysqld.2.err
--let $assert_text = STOP GROUP REPLICATION return timeout on stopping recovery module
--let $assert_select = \[Warning\] \[MY-011653\] \[Repl\] Plugin group_replication reported: 'On shutdown there was a timeout on the Group Replication recovery module termination. Check the log for more details'
--let $assert_count = 1
--source include/assert_grep.inc


--echo
--echo # 7. Cleanup

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

SET SESSION sql_log_bin= 0;
call mtr.add_suppression("Error when stopping the group replication incremental recovery's donor connection");
call mtr.add_suppression('On shutdown there was a timeout on the Group Replication recovery module termination. Check the log for more details');
SET SESSION sql_log_bin= 1;

SET @@GLOBAl.group_replication_components_stop_timeout = @group_replication_components_stop_timeout_save;

--source include/group_replication_end.inc
