From 18ccf1584ab5d7544f6d97ba60627c830b464d75 Mon Sep 17 00:00:00 2001
From: Yaroslav Dynnikov <yaroslav.dynnikov@gmail.com>
Date: Mon, 16 May 2022 16:15:43 +0300
Subject: [PATCH] test: fix flaky args::tests

By default cargo runs tests in parallel in multiple threads.  Both
`test_log_level` and `test_parse` access environment variables which are
shared across threads. Consequently, their concurrent modification
results in the test failure.

This patch unites these two tests making it linear.
---
 src/args.rs | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/args.rs b/src/args.rs
index 8366751a40..c7f37f9ebc 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -279,6 +279,7 @@ mod tests {
             assert_eq!(parsed.peers.as_ref(), vec!["peer-from-env:3301"]);
             assert_eq!(parsed.listen, "localhost:3301"); // default
             assert_eq!(parsed.advertise_address(), "localhost:3301"); // default
+            assert_eq!(parsed.log_level(), SayLevel::Info); // default
 
             let parsed = parse![Run, "--instance-id", "instance-id-from-args"];
             assert_eq!(parsed.instance_id, "instance-id-from-args");
@@ -312,22 +313,14 @@ mod tests {
             assert_eq!(parsed.listen, "listen-from-env:3301");
             assert_eq!(parsed.advertise_address(), "advertise-from-args:3301");
         }
-    }
-
-    #[test]
-    fn test_log_level() {
-        let _env_dump = EnvDump::new();
-        std::env::set_var("PICODATA_INSTANCE_ID", "test-log-level");
-        std::env::set_var("PICODATA_PEER", "test-log-level");
-
-        let parsed = parse![Run,];
-        assert_eq!(parsed.log_level(), SayLevel::Info);
 
         std::env::set_var("PICODATA_LOG_LEVEL", "verbose");
-        let parsed = parse![Run,];
-        assert_eq!(parsed.log_level(), SayLevel::Verbose);
+        {
+            let parsed = parse![Run,];
+            assert_eq!(parsed.log_level(), SayLevel::Verbose);
 
-        let parsed = parse![Run, "--log-level", "warn"];
-        assert_eq!(parsed.log_level(), SayLevel::Warn);
+            let parsed = parse![Run, "--log-level", "warn"];
+            assert_eq!(parsed.log_level(), SayLevel::Warn);
+        }
     }
 }
-- 
GitLab