source include/not_embedded.inc;
source include/not_aix.inc;

-- source include/no_view_protocol.inc

let $have_plugin = `SELECT COUNT(*) FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_STATUS='ACTIVE' AND PLUGIN_NAME = 'THREAD_POOL_GROUPS'`;
if(!$have_plugin)
{
  --skip Need thread_pool_groups plugin
}

#I_S.THREAD_POOL_GROUPS
DESC INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT COUNT(*)=@@thread_pool_size FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT SUM(CONNECTIONS) FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT SUM(THREADS) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT SUM(ACTIVE_THREADS) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT SUM(QUEUE_LENGTH) FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;
SELECT SUM(IS_STALLED) FROM INFORMATION_SCHEMA.THREAD_POOL_GROUPS;


# I_S.THREAD_POOL_STATS
DESC INFORMATION_SCHEMA.THREAD_POOL_STATS;
# The following query does not reliably give results, after FLUSH
# so if the test runs with --repeat, it would fail
#SELECT SUM(THREAD_CREATIONS) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
SELECT SUM(DEQUEUES_BY_LISTENER+DEQUEUES_BY_WORKER) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
SELECT SUM(POLLS_BY_LISTENER+POLLS_BY_WORKER) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
--disable_ps_protocol
FLUSH THREAD_POOL_STATS;
SELECT SUM(DEQUEUES_BY_LISTENER+DEQUEUES_BY_WORKER)  FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
SELECT SUM(POLLS_BY_LISTENER) FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
SELECT SUM(POLLS_BY_WORKER) FROM INFORMATION_SCHEMA.THREAD_POOL_STATS;
--enable_ps_protocol

#I_S.THREAD_POOL_WAITS
DESC INFORMATION_SCHEMA.THREAD_POOL_WAITS;
SELECT REASON FROM INFORMATION_SCHEMA.THREAD_POOL_WAITS;
SELECT COUNT FROM INFORMATION_SCHEMA.THREAD_POOL_WAITS WHERE REASON='Sleep';
--disable_ps2_protocol
SELECT SLEEP(0.01);
--enable_ps2_protocol
SELECT COUNT FROM INFORMATION_SCHEMA.THREAD_POOL_WAITS WHERE REASON='Sleep';
FLUSH THREAD_POOL_WAITS;


# I_S.THREAD_POOL_QUEUES
DESC INFORMATION_SCHEMA.THREAD_POOL_QUEUES;
let $extra_port=`select @@port+1`;

let $restart_parameters=--extra-port=$extra_port;
let $restart_noprint=1;
source include/restart_mysqld.inc;

connect (con1, localhost, root,,test);
connection con1;
let $con1_id=`SELECT CONNECTION_ID()`;

connect (con2, localhost, root,,test);
connection con2;
let $con2_id=`SELECT CONNECTION_ID()`;

connect(extra_con,127.0.0.1,root,,test,$extra_port,);

connection con1;
send SELECT SLEEP(1000);

connection extra_con;
let $wait_condition=
  SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.PROCESSLIST
  WHERE STATE='User sleep' AND ID=$con1_id;
--source include/wait_condition.inc

connection con2;
send DO 1;


connection extra_con;
let $wait_condition=
  SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.THREAD_POOL_QUEUES
  WHERE CONNECTION_ID IS NOT NULL;
--source include/wait_condition.inc

--replace_result $con1_id con1_id
eval KILL QUERY $con1_id;
disconnect extra_con;

connection con1;
error 0,ER_QUERY_INTERRUPTED;
reap;
disconnect con1;

connection con2;
reap;
disconnect con2;

connection default;
