From d386a5d416d5cbfc279a1645ee8fb12ef00dae85 Mon Sep 17 00:00:00 2001 From: AIkorsky Date: Sat, 5 Feb 2022 13:47:54 +0300 Subject: [PATCH] ci: Test that pool reuses TiDB connections --- azure-pipelines.yml | 21 +++++++++++++++++++++ src/conn/mod.rs | 5 +++++ src/conn/pool.rs | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5a6a5c0..454cd8b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -152,6 +152,27 @@ jobs: DATABASE_URL: mysql://root:password@localhost/mysql displayName: Run tests + - job: "TestTiDB" + pool: + vmImage: "ubuntu-latest" + strategy: + matrix: + v5.3.0: + DB_VERSION: "v5.3.0" + v5.0.6: + DB_VERSION: "v5.0.6" + steps: + - displayName: Install TiDB + bash: | + curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh + source ~/.bash_profile + tiup playground $(DB_VERSION) --db 1 --pd 1 --kv 1 & + while ! nc -W 1 localhost 4000 | grep -q -P '.+'; do sleep 1; done + cargo test should_reuse_connections -- --nocapture + env: + RUST_BACKTRACE: 1 + DATABASE_URL: mysql://root@127.0.0.1:4000/mysql + - job: "TestMySql" pool: vmImage: "ubuntu-latest" diff --git a/src/conn/mod.rs b/src/conn/mod.rs index ef2efbb..499d8ac 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -201,6 +201,11 @@ impl ConnInner { pub struct Conn(Box); impl Conn { + /// Returns version number reported by the server. + pub fn server_version(&self) -> (u16, u16, u16) { + self.0.server_version.unwrap() + } + /// Returns connection identifier. pub fn connection_id(&self) -> u32 { self.0.connection_id diff --git a/src/conn/pool.rs b/src/conn/pool.rs index ced8bca..37886d9 100644 --- a/src/conn/pool.rs +++ b/src/conn/pool.rs @@ -622,6 +622,24 @@ mod test { a.add(); } + #[test] + fn should_reuse_connections() -> crate::Result<()> { + let pool = Pool::new_manual(1, 1, get_opts())?; + let mut conn = pool.get_conn()?; + + let server_version = conn.server_version(); + let connection_id = conn.connection_id(); + + for _ in 0..16 { + drop(conn); + conn = pool.get_conn()?; + println!("CONN connection_id={}", conn.connection_id()); + assert!(conn.connection_id() == connection_id || server_version < (5, 7, 2)); + } + + Ok(()) + } + #[test] fn should_start_transaction_on_PooledConn() { let pool = Pool::new(get_opts()).unwrap();