######################################################################################
#                                                                                    #
# Variable Name: query_cache_wlock_invalidate                                        #
# Scope: GLOBAL & SESSION                                                            #
# Access Type: Dynamic                                                               #
# Data Type: boolean                                                                 #
# Default Value: FALSE                                                               #
# Values: TRUE / 1, FALSE / 0                                                        #
#                                                                                    #
#                                                                                    #
# Creation Date: 2008-02-21                                                          #
# Author:  Sharique Abdullah                                                         #
# Modified: HHunger 2009-02-27 Replaced sleeps, beautifications                      #
#                                                                                    #
# Description: Test Cases of Dynamic System Variable "query_cache_wlock_invalidate"  #
#              that checks behavior of this variable in the following ways           #
#              * Default Value                                                       #
#              * Valid & Invalid values                                              #
#              * Scope & Access method                                               #
#              * Cache behaviors                                                     #
#                                                                                    #
# Reference:                                                                         #
# http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html                #
#                                                                                    #
######################################################################################

--echo ** Setup **
--echo
#
# Setup
#

--source include/not_embedded.inc

# disabled due to differences in the result
--disable_ps_protocol
#
# Save initial value
#
--echo ** Connecting con0 using root **
connect (con0,localhost,root,,);
--echo ** Connecting con1 using root **
connect (con1, localhost, root,,);
--echo ** Connection con0 **
connection con0;

SET @start_global_value = @@GLOBAL.query_cache_wlock_invalidate;
SET @start_session_value = @@SESSION.query_cache_wlock_invalidate;

SET @old_cache_size = @@GLOBAL.query_cache_size;
SET @old_cache_type = @@GLOBAL.query_cache_type;

#
# Creating test table
#

CREATE TABLE t1(id int, value varchar(10));

INSERT INTO t1 VALUES(1, 'val1');
INSERT INTO t1 VALUES(2, 'val2');
INSERT INTO t1 VALUES(3, 'val3');

#
# Clearing the query cache and setting up cache size
#

SET GLOBAL query_cache_size = 131072;

--echo FLUSHING CACHE
SET GLOBAL query_cache_size = 0;
SET GLOBAL query_cache_size = 131072;

SET GLOBAL query_cache_type = ON;

#
# Testing for value ON
#
--echo Testing for value ON

--echo ** Connection con0 **
connection con0;

SET SESSION query_cache_wlock_invalidate = ON;

--echo ** Connection con1 **
connection con1;

SET SESSION query_cache_wlock_invalidate = ON;

--echo '#----------------------------FN_DYNVARS_136_01--------------#'
#
# Testing for cache invalidation
#

--echo Testing for cache invalidation

SELECT * FROM t1;

SHOW STATUS LIKE 'Qcache_queries_in_cache';
--echo 1 Expected

LOCK TABLE t1 WRITE;

UNLOCK TABLES;

SHOW STATUS LIKE 'Qcache_queries_in_cache';
--echo 0 Expected

--echo '#----------------------------FN_DYNVARS_136_02-----------------------#'
#
# Testing for blockage of access
#

disable_result_log;

SELECT * FROM t1;

SHOW STATUS LIKE 'Qcache_queries_in_cache';

enable_result_log;

--echo ** Connection con1 **
connection con1;

SELECT * FROM t1;

--echo ** Connection con0 **
connection con0;

LOCK TABLE t1 WRITE;

--echo ** Connection con1 **
connection con1;

--echo ** Asynchronous Execution **
send SELECT * FROM t1;

--echo ** Connection con0 **
connection con0;

--echo wait until table is locked
let $wait_condition= SELECT count(*) > 0 FROM information_schema.processlist
                     WHERE state= 'Waiting for table metadata lock';
--source include/wait_condition.inc
UNLOCK TABLES;

--echo ** Connection con1 **
connection con1;
--echo ** Asynchronous Result **
reap;

#
# Testing for value OFF
#
--echo Testing for value OFF

--echo ** Connection con0 **
connection con0;

SET SESSION query_cache_wlock_invalidate = OFF;

--echo ** Connection con1 **
connection con1;

SET SESSION query_cache_wlock_invalidate = OFF;

--echo '#----------------------------FN_DYNVARS_136_03------------------#'
#
# Testing for cache invalidation
#

--echo Testing for cache invalidation

SELECT * FROM t1;

SHOW STATUS LIKE 'Qcache_queries_in_cache';
--echo 1 Expected

LOCK TABLE t1 WRITE;

UNLOCK TABLES;

SHOW STATUS LIKE 'Qcache_queries_in_cache';
--echo 0 Expected

--echo '#----------------------------FN_DYNVARS_136_04---------------------#'
#
# Testing for blockage of access
#

SELECT * FROM t1;

SHOW STATUS LIKE 'Qcache_queries_in_cache';
--echo 1 Expected

--echo ** Connection con1 **
connection con1;

SELECT * FROM t1;

--echo ** Connection con0 **
connection con0;
LOCK TABLE t1 WRITE;

--echo ** Connection con1 **
connection con1;
--echo ** Should not be blocked **
SELECT * FROM t1;
SELECT * FROM t1;
SELECT * FROM t1;

--echo ** Connection con0 **
connection con0;
UNLOCK TABLES;

--echo ** Connection con1 **
connection con1;

--echo '#----------------------------FN_DYNVARS_136_05------------------------#'
#
# Session data integrity check & GLOBAL Value check
#

SET GLOBAL query_cache_wlock_invalidate = OFF;

--echo ** Connecting con_int1 using root **
connect (con_int1,localhost,root,,);
--echo ** Connection con_int1 **
connection con_int1;

SELECT @@SESSION.query_cache_wlock_invalidate;
--echo Expected Value : 0 / OFF;
SET SESSION query_cache_wlock_invalidate = ON;

--echo ** Connecting con_int2 using root **
connect (con_int2,localhost,root,,);
--echo ** Connection con_int2 **
connection con_int2;

SELECT @@SESSION.query_cache_wlock_invalidate;
--echo Expected Value : 0 / OFF;

SET SESSION query_cache_wlock_invalidate = OFF;

--echo ** Connection con_int1 **
connection con_int1;
SELECT @@SESSION.query_cache_wlock_invalidate;
--echo Expected Value : 1 / ON;

--echo ** Connection con_int2 **
connection con_int2;
SELECT @@SESSION.query_cache_wlock_invalidate;
--echo Expected Value : 0 / OFF;

SELECT @@GLOBAL.query_cache_wlock_invalidate;
--echo Expected Value : 0 / OFF;

--echo ** Connection default **
connection default;
--echo Disconnecting Connections con_int1, con_int2
disconnect con_int1;
disconnect con_int2;

#
# Cleanup
#
--echo
--echo CLEANUP

--echo ** Connection con0 **
connection con0;

SET @@GLOBAL.query_cache_wlock_invalidate = @start_global_value;
SET @@SESSION.query_cache_wlock_invalidate = @start_session_value ;
SET @@GLOBAL.query_cache_size = @old_cache_size;
SET @@GLOBAL.query_cache_type = @old_cache_type;

--echo ** Connection default **
connection default;
--echo Disconnecting Connections con0, con1
disconnect con0;
disconnect con1;

DROP TABLE t1;

--enable_ps_protocol

