From 45b135c1b1775df7f8e230adb7df27245fc3a5b3 Mon Sep 17 00:00:00 2001
From: Zhaofeng Li <hello@zhaofeng.li>
Date: Fri, 4 Feb 2022 20:58:17 -0800
Subject: [PATCH] nix_eval_jobs: Add timeouts to tests

Sometimes nix-eval-jobs would lock up indefinitely, so let's make it
fail fast in CI.

Also ignore test_json_global_error while we investigate.
---
 Cargo.lock                         | 60 ++++++++++++++++++++++++++++++
 Cargo.toml                         |  1 +
 package.nix                        |  2 +-
 src/nix/evaluator/nix_eval_jobs.rs |  9 +++++
 4 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/Cargo.lock b/Cargo.lock
index 98f4c96..0029014 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -141,6 +141,7 @@ dependencies = [
  "lazy_static",
  "libc",
  "log",
+ "ntest",
  "quit",
  "regex",
  "serde",
@@ -520,6 +521,47 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "ntest"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "984caf6c8aa869418ef88062fc685d07d50c04308e63f7eaff6a395b1f5aff33"
+dependencies = [
+ "ntest_proc_macro_helper",
+ "ntest_test_cases",
+ "ntest_timeout",
+]
+
+[[package]]
+name = "ntest_proc_macro_helper"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "115562228962147ca51748d19446a4261535a7b6a7b5ff02681e527dcefc22f7"
+
+[[package]]
+name = "ntest_test_cases"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c03e3201148714c580c5cf1ef68b1ed4039203068193197834a43503164b8237"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "ntest_timeout"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a870300c30d4224cb16022a4660fd8084d6cb4d29618563c3016b50cc757d1e7"
+dependencies = [
+ "ntest_proc_macro_helper",
+ "proc-macro-crate",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "num_cpus"
 version = "1.13.1"
@@ -569,6 +611,15 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
 
+[[package]]
+name = "proc-macro-crate"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
+dependencies = [
+ "toml",
+]
+
 [[package]]
 name = "proc-macro-error"
 version = "1.0.4"
@@ -875,6 +926,15 @@ dependencies = [
  "tokio-stream",
 ]
 
+[[package]]
+name = "toml"
+version = "0.5.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
+dependencies = [
+ "serde",
+]
+
 [[package]]
 name = "unicode-bidi"
 version = "0.3.7"
diff --git a/Cargo.toml b/Cargo.toml
index a15fc5f..1f5759a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -38,6 +38,7 @@ uuid = { version = "0.8.2", features = ["serde", "v4"] }
 validator = { version = "0.14", features = ["derive"] }
 
 [dev-dependencies]
+ntest = "0.7.3"
 tokio-test = "0.4.0"
 
 [dependencies.tokio]
diff --git a/package.nix b/package.nix
index 57427c1..34f8ea5 100644
--- a/package.nix
+++ b/package.nix
@@ -9,7 +9,7 @@ rustPlatform.buildRustPackage rec {
     src = lib.cleanSource ./.;
   };
 
-  cargoSha256 = "sha256-IENRzHvv9TdLj/aTkm/MZtUKSXjtNWrspOQGLK2xFoo=";
+  cargoSha256 = "sha256-kWxCc9UqM96Hqph4Aj39gv83PP6bNHripRTvMiucYFg=";
 
   nativeBuildInputs = [ installShellFiles ];
 
diff --git a/src/nix/evaluator/nix_eval_jobs.rs b/src/nix/evaluator/nix_eval_jobs.rs
index cec00bc..26a2419 100644
--- a/src/nix/evaluator/nix_eval_jobs.rs
+++ b/src/nix/evaluator/nix_eval_jobs.rs
@@ -223,10 +223,12 @@ pub fn get_pinned_nix_eval_jobs() -> Option<&'static str> {
 mod tests {
     use super::*;
 
+    use ntest::timeout;
     use tokio_test::block_on;
     use tokio_stream::StreamExt;
 
     #[test]
+    #[timeout(30000)]
     fn test_eval() {
         let evaluator = NixEvalJobs::default();
         let expr = r#"with import <nixpkgs> {}; { a = pkgs.hello; b = pkgs.bash; }"#.to_string();
@@ -247,6 +249,7 @@ mod tests {
     }
 
     #[test]
+    #[timeout(30000)]
     fn test_global_error() {
         let evaluator = NixEvalJobs::default();
         let expr = r#"gibberish"#.to_string();
@@ -266,6 +269,7 @@ mod tests {
     }
 
     #[test]
+    #[timeout(30000)]
     fn test_attribute_error() {
         let evaluator = NixEvalJobs::default();
         let expr = r#"with import <nixpkgs> {}; { a = pkgs.hello; b = throw "an error"; }"#.to_string();
@@ -300,7 +304,12 @@ mod tests {
     }
 
     #[test]
+    #[timeout(30000)]
+    #[ignore]
     fn test_json_global_error() {
+        // #[ignore]: nix-eval-jobs locks up when run in parallel to other tests
+        // cannot consistently reproduce and more investigation is needed
+
         let evaluator = NixEvalJobs::default();
         let expr = r#"with import <nixpkgs> {}; { a = pkgs.hello; b = pkgs.writeText "x" (import /sys/nonexistentfile); }"#.to_string();