###########################################################################
# Author: Oystein Grovlen
# Date: 2008-07-31
# Purpose: To test starting backups/retore in parallel
###############################################################################
--source include/not_embedded.inc
--source include/have_debug_sync.inc

# Suppress warning about expected backup/restore errors
disable_query_log;
call mtr.add_suppression("Backup:");
call mtr.add_suppression("Restore:");
enable_query_log;

#Create Database and object view for this test.

--disable_warnings
SET DEBUG_SYNC= 'reset';
DROP DATABASE IF EXISTS backup_concurrent;
--enable_warnings

let $MYSQLD_BACKUPDIR= `select @@backupdir`;
--error 0,1
remove_file $MYSQLD_BACKUPDIR/backup1;
--error 0,1
remove_file $MYSQLD_BACKUPDIR/backup2;
--error 0,1
remove_file $MYSQLD_BACKUPDIR/backup3;

CREATE DATABASE backup_concurrent;
USE backup_concurrent;

#Create table

--echo Creating Table
CREATE TABLE t (
t1 INTEGER NOT NULL,
t2 CHAR(36),
PRIMARY KEY (t1)
);

--echo ---------------------------------------------------
--echo Testing starting new backup while backup is ongoing
--echo ---------------------------------------------------
--echo Starting first backup
connect (backup,localhost,root,,);
SET DEBUG_SYNC= 'after_backup_start_backup SIGNAL running WAIT_FOR backup';
send BACKUP DATABASE backup_concurrent TO 'backup1';

connection default;
--echo Waiting for first backup to get going
SET DEBUG_SYNC= 'now WAIT_FOR running';
--echo Starting second backup in another connection.  
--echo (Should fail because another backup is running.)
--error ER_BACKUP_RUNNING
BACKUP DATABASE backup_concurrent TO 'backup2';

--echo Insert Data
INSERT INTO t VALUES (1, 'test');

--echo Wait for first backup to complete
SET DEBUG_SYNC= 'now SIGNAL backup';
connection backup;
replace_column 1 #;
reap;

--echo ---------------------------------------------------
--echo Testing starting restore while backup is ongoing
--echo ---------------------------------------------------
--echo Starting backup
SET DEBUG_SYNC= 'after_backup_start_backup SIGNAL running WAIT_FOR backup';
send BACKUP DATABASE backup_concurrent TO 'backup3';

connection default;
--echo Waiting for backup to get going
SET DEBUG_SYNC= 'now WAIT_FOR running';
--echo Starting restore in another connection.  
--echo (Should fail because another backup is running.)
--error ER_BACKUP_RUNNING
RESTORE FROM 'backup1';

--echo Insert Data
INSERT INTO t VALUES (2, 'test');

--echo Wait for backup to complete
SET DEBUG_SYNC= 'now SIGNAL backup';
connection backup;
replace_column 1 #;
reap;

--echo ---------------------------------------------------
--echo Testing starting backup/restore restore is ongoing
--echo ---------------------------------------------------
--echo Starting restore
SET DEBUG_SYNC= 'after_backup_start_restore SIGNAL running WAIT_FOR restore';
send RESTORE FROM 'backup1' OVERWRITE;

connection default;
--echo Waiting for restore to get going
SET DEBUG_SYNC= 'now WAIT_FOR running';

--echo Starting backup in another connection.  
--echo (Should fail because restore is running.)
--error ER_BACKUP_RUNNING
BACKUP DATABASE backup_concurrent TO 'backup4';

--echo Insert Data
INSERT INTO t VALUES (3, 'test');

--echo Starting a new restore in another connection.  
--echo (Should fail because another restore is running.)
--error ER_BACKUP_RUNNING
RESTORE FROM 'backup3';

--echo Insert Data
INSERT INTO t VALUES (4, 'test');

--echo Wait for backup to complete
SET DEBUG_SYNC= 'now SIGNAL restore';
connection backup;
replace_column 1 #;
reap;


# Test cleanup section

--echo
--echo Test completed. Cleaning up.
--echo

DROP DATABASE backup_concurrent;
SET DEBUG_SYNC= 'reset';

remove_file $MYSQLD_BACKUPDIR/backup1;
remove_file $MYSQLD_BACKUPDIR/backup3;

