Skip to content

Commit 01bf931

Browse files
fix: use serial_test for env var tests and improve MySQL readiness check (#213)
- Added serial_test dependency for tests that modify environment variables - Marked env var tests with #[serial] to prevent race conditions - Changed MySQL readiness check to use TCP (127.0.0.1) instead of Unix socket - Require 2 consecutive successful connections before declaring ready - Updated test to also use TCP connection for consistency
1 parent 81bb5a7 commit 01bf931

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ tempfile = "3.0"
8181
which = "8.0"
8282
serde_json = "1.0"
8383
proptest = "1.6"
84+
serial_test = "3.3.1"
8485

8586
[[example]]
8687
name = "basic_usage"

src/template/database/mysql.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,19 @@ impl Template for MysqlTemplate {
319319
let check_interval = Duration::from_millis(1000);
320320

321321
timeout(wait_timeout, async {
322+
let mut consecutive_successes = 0;
322323
loop {
323324
// Check if container is running - keep retrying if not yet started
324325
// Don't fail immediately as the container may still be starting up
325326
if !self.is_running().await.unwrap_or(false) {
327+
consecutive_successes = 0;
326328
sleep(check_interval).await;
327329
continue;
328330
}
329331

330-
// Try to connect to MySQL using mysqladmin
332+
// Try to connect to MySQL using the mysql client with an actual query
333+
// Use 127.0.0.1 instead of localhost to force TCP connection
334+
// (localhost uses Unix socket which may not be ready even when TCP is)
331335
let password = self
332336
.config
333337
.env
@@ -338,23 +342,31 @@ impl Template for MysqlTemplate {
338342

339343
let password_arg = format!("-p{}", password);
340344
let check_cmd = vec![
341-
"mysqladmin",
345+
"mysql",
342346
"-h",
343-
"localhost",
347+
"127.0.0.1",
344348
"-u",
345349
"root",
346350
&password_arg,
347-
"ping",
351+
"-e",
352+
"SELECT 1",
348353
];
349354

350355
// Execute readiness check
351356
if let Ok(result) = self.exec(check_cmd).await {
352-
// mysqladmin ping returns "mysqld is alive" on success
353-
if result.stdout.contains("mysqld is alive") {
354-
return Ok(());
357+
// If we got output containing '1', MySQL responded successfully
358+
if result.stdout.contains('1') {
359+
consecutive_successes += 1;
360+
// Require 2 consecutive successes to ensure stability
361+
if consecutive_successes >= 2 {
362+
return Ok(());
363+
}
364+
sleep(Duration::from_millis(500)).await;
365+
continue;
355366
}
356367
}
357368

369+
consecutive_successes = 0;
358370
sleep(check_interval).await;
359371
}
360372
})

src/template/redis/cluster.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ impl RedisClusterConnection {
812812
#[cfg(test)]
813813
mod tests {
814814
use super::*;
815+
use serial_test::serial;
815816

816817
#[test]
817818
fn test_redis_cluster_template_basic() {
@@ -899,6 +900,7 @@ mod tests {
899900
}
900901

901902
#[test]
903+
#[serial]
902904
fn test_redis_cluster_from_env_defaults() {
903905
// Clear any existing env vars to ensure defaults are used
904906
std::env::remove_var("REDIS_CLUSTER_PORT_BASE");
@@ -914,6 +916,7 @@ mod tests {
914916
}
915917

916918
#[test]
919+
#[serial]
917920
fn test_redis_cluster_from_env_with_vars() {
918921
std::env::set_var("REDIS_CLUSTER_PORT_BASE", "8000");
919922
std::env::set_var("REDIS_CLUSTER_NUM_MASTERS", "6");

tests/database_template_integration.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ mod database_template_tests {
228228
assert!(conn.url().contains("testdb"));
229229
assert!(conn.url().contains("testuser"));
230230

231-
// Execute a simple query
231+
// Execute a simple query (use 127.0.0.1 to force TCP, matching readiness check)
232232
let result = mysql
233233
.exec(vec![
234234
"mysql",
235+
"-h",
236+
"127.0.0.1",
235237
"-u",
236238
"root",
237239
"-prootpass",

0 commit comments

Comments
 (0)