@@ -2234,6 +2234,31 @@ TEST_F(Sess, pool_use)
2234
2234
EXPECT_EQ (1 , res.fetchOne ()[0 ].get <int >());
2235
2235
}
2236
2236
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
+ }
2237
2262
2238
2263
}
2239
2264
@@ -2369,111 +2394,64 @@ TEST_F(Sess, pool_ttl)
2369
2394
2370
2395
mysqlx::Client client (settings);
2371
2396
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
+ */
2372
2402
2373
- auto get_sessions = [&client, max_connections]()
2403
+ auto get_sessions = [&client, max_connections]() -> std::set< unsigned >
2374
2404
{
2375
2405
std::list<mysqlx::Session> sessions;
2406
+ std::set<unsigned > sess_ids;
2407
+
2376
2408
for (int i = 0 ; i < max_connections; ++i)
2377
2409
{
2378
2410
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 ]);
2380
2413
}
2381
- };
2382
-
2383
- get_sessions ();
2384
-
2385
- std::cout << " Kill connections" << std::endl;
2386
-
2387
- std::vector<int > proccess_ids;
2388
2414
2389
- auto proccesslist = sql (" show processlist" );
2415
+ return std::move (sess_ids);
2416
+ };
2390
2417
2391
- unsigned db_idx = 0 ;
2418
+ auto ids = get_sessions ();
2419
+ EXPECT_EQ (max_connections, ids.size ());
2392
2420
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
+ */
2401
2426
2402
- EXPECT_LT (db_idx,proccesslist. getColumnCount ()) ;
2427
+ std::cout << " Get sessions " << std::endl ;
2403
2428
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 ());
2414
2431
2432
+ // Check that all connection ids are from the original set
2415
2433
2416
- for (auto id : proccess_ids )
2434
+ for (unsigned id : ids1 )
2417
2435
{
2418
- std::stringstream query;
2419
- query << " KILL CONNECTION " << id;
2420
- sql (query.str ());
2436
+ EXPECT_TRUE (ids.count (id));
2421
2437
}
2422
2438
2439
+ std::cout << " Wait 15s to timeout sessions" << std::endl;
2440
+ std::this_thread::sleep_for (std::chrono::seconds (12 ));
2423
2441
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
+ */
2434
2446
2435
2447
std::cout << " Get sessions" << std::endl;
2436
2448
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 ());
2444
2451
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)
2471
2453
{
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));
2477
2455
}
2478
2456
}
2479
2457
0 commit comments