Skip to content

Commit f9035de

Browse files
committed
tests: cleanup of Sess.pool_ttl test
1 parent 3246e1f commit f9035de

File tree

1 file changed

+60
-82
lines changed

1 file changed

+60
-82
lines changed

devapi/tests/session-t.cc

Lines changed: 60 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,31 @@ TEST_F(Sess, pool_use)
22342234
EXPECT_EQ(1, res.fetchOne()[0].get<int>());
22352235
}
22362236

2237+
// Corner ccase of 1 slot in the pool
2238+
2239+
{
2240+
settings.set(ClientOption::POOL_MAX_SIZE, 1);
2241+
2242+
mysqlx::Client cli(settings);
2243+
mysqlx::Session s1 = cli.getSession();
2244+
s1.close();
2245+
mysqlx::Session s2 = cli.getSession();
2246+
}
2247+
2248+
// Using many clients
2249+
2250+
{
2251+
settings.set(ClientOption::POOL_MAX_SIZE, 10);
2252+
std::vector<mysqlx::Session> session_list;
2253+
for (int i = 0; i < 5; ++i)
2254+
{
2255+
mysqlx::Client cli(settings);
2256+
for (int j = 0; j < 10; ++j)
2257+
{
2258+
session_list.emplace_back(cli.getSession());
2259+
}
2260+
}
2261+
}
22372262

22382263
}
22392264

@@ -2369,111 +2394,64 @@ TEST_F(Sess, pool_ttl)
23692394

23702395
mysqlx::Client client(settings);
23712396

2397+
/*
2398+
Open as many sessions as there are slots in the session pool, and then
2399+
close them so that they return to the pool. Returns ids of the created
2400+
sessions.
2401+
*/
23722402

2373-
auto get_sessions = [&client, max_connections]()
2403+
auto get_sessions = [&client, max_connections]() -> std::set<unsigned>
23742404
{
23752405
std::list<mysqlx::Session> sessions;
2406+
std::set<unsigned> sess_ids;
2407+
23762408
for (int i = 0; i < max_connections; ++i)
23772409
{
23782410
sessions.emplace_back(client);
2379-
EXPECT_EQ(1, sessions.back().sql("select 1").execute().count());
2411+
auto row = sessions.back().sql("SELECT CONNECTION_ID()").execute().fetchOne();
2412+
sess_ids.insert(row[0]);
23802413
}
2381-
};
2382-
2383-
get_sessions();
2384-
2385-
std::cout << "Kill connections" << std::endl;
2386-
2387-
std::vector<int> proccess_ids;
23882414

2389-
auto proccesslist = sql("show processlist");
2415+
return std::move(sess_ids);
2416+
};
23902417

2391-
unsigned db_idx = 0;
2418+
auto ids = get_sessions();
2419+
EXPECT_EQ(max_connections, ids.size());
23922420

2393-
for(auto column : proccesslist.getColumns())
2394-
{
2395-
if (column.getColumnLabel() == "db" )
2396-
{
2397-
break;
2398-
}
2399-
++db_idx;
2400-
}
2421+
/*
2422+
Now we have pool full of sessions, and none of them has expired yet.
2423+
When we request sessions again, we should get sessions from the pool,
2424+
no new connections.
2425+
*/
24012426

2402-
EXPECT_LT(db_idx,proccesslist.getColumnCount());
2427+
std::cout << "Get sessions" << std::endl;
24032428

2404-
for (auto row : proccesslist)
2405-
{
2406-
//UT created sessions all use pool_ttl schema, so we will look for
2407-
//connections having that schema to kill them
2408-
auto db = row.get(db_idx);
2409-
if (db.isNull() || db.get<string>() != "pool_ttl")
2410-
continue;
2411-
int thread_id = row.get(0).get<int>();
2412-
proccess_ids.push_back(thread_id);
2413-
}
2429+
auto ids1 = get_sessions();
2430+
EXPECT_EQ(max_connections, ids1.size());
24142431

2432+
// Check that all connection ids are from the original set
24152433

2416-
for (auto id : proccess_ids)
2434+
for (unsigned id : ids1)
24172435
{
2418-
std::stringstream query;
2419-
query << "KILL CONNECTION " << id;
2420-
sql(query.str());
2436+
EXPECT_TRUE(ids.count(id));
24212437
}
24222438

2439+
std::cout << "Wait 15s to timeout sessions" << std::endl;
2440+
std::this_thread::sleep_for(std::chrono::seconds(12));
24232441

2424-
std::cout << "set global mysqlx_wait_timeout=20" << std::endl;
2425-
2426-
sql("set global mysqlx_wait_timeout=20");
2427-
2428-
std::cout << "Get sessions" << std::endl;
2429-
2430-
get_sessions();
2431-
2432-
std::cout << "Wait 25s to timeout sessions" << std::endl;
2433-
std::this_thread::sleep_for(std::chrono::seconds(25));
2442+
/*
2443+
Now the idle timeout has expired, so sessions in the pool shoul
2444+
not be used but new sessions should be created.
2445+
*/
24342446

24352447
std::cout << "Get sessions" << std::endl;
24362448

2437-
get_sessions();
2438-
2439-
//Create a new session, since previous has timed-out!
2440-
create_session();
2441-
std::cout << "set global mysqlx_wait_timeout=28800" << std::endl;
2442-
sql("set global mysqlx_wait_timeout=28800");
2443-
}
2449+
auto ids2 = get_sessions();
2450+
EXPECT_EQ(max_connections, ids2.size());
24442451

2445-
{
2446-
settings.set(ClientOption::POOL_MAX_SIZE, 1);
2447-
2448-
mysqlx::Client cli(settings);
2449-
mysqlx::Session s1 = cli.getSession();
2450-
s1.close();
2451-
mysqlx::Session s2 = cli.getSession();
2452-
2453-
}
2454-
2455-
{
2456-
2457-
std::stringstream uri;
2458-
uri << "mysqlx://" << get_user();
2459-
if (get_password() && *get_password())
2460-
uri << ":" << get_password();
2461-
uri << "@" << "localhost:" << get_port();
2462-
2463-
mysqlx::Session s = mysqlx::getSession(uri.str());
2464-
s.close();
2465-
}
2466-
2467-
{
2468-
settings.set(ClientOption::POOL_MAX_SIZE, 10);
2469-
std::vector<mysqlx::Session> session_list;
2470-
for (int i=0; i < 5; ++i)
2452+
for (unsigned id : ids2)
24712453
{
2472-
mysqlx::Client cli(settings);
2473-
for (int j=0; j < 10; ++j)
2474-
{
2475-
session_list.emplace_back(cli.getSession());
2476-
}
2454+
EXPECT_FALSE(ids1.count(id));
24772455
}
24782456
}
24792457

0 commit comments

Comments
 (0)