-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
tests.rs
62 lines (54 loc) · 1.78 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
use std::path::PathBuf;
use store::{ImmutableInputs, Store};
use task_executor::Executor;
use tempfile::TempDir;
use testutil::owned_string_vec;
use workunit_store::WorkunitStore;
use crate::nailgun::NailgunPool;
use crate::{NamedCaches, Process};
fn pool(size: usize) -> (NailgunPool, NamedCaches, ImmutableInputs) {
let _ = WorkunitStore::setup_for_tests();
let named_caches_dir = TempDir::new().unwrap();
let store_dir = TempDir::new().unwrap();
let executor = Executor::new();
let store = Store::local_only(executor.clone(), store_dir.path()).unwrap();
let base_dir = std::env::temp_dir();
let pool = NailgunPool::new(base_dir.clone(), size, store.clone(), executor);
(
pool,
NamedCaches::new(named_caches_dir.path().to_owned()),
ImmutableInputs::new(store, &base_dir).unwrap(),
)
}
async fn run(pool: &(NailgunPool, NamedCaches, ImmutableInputs), port: u16) -> PathBuf {
let mut p = pool
.0
.acquire(
Process::new(owned_string_vec(&[
"/bin/bash",
"-c",
&format!("echo Mock port {}.; sleep 10", port),
])),
&pool.1,
&pool.2,
)
.await
.unwrap();
assert_eq!(port, p.port());
let workdir = p.workdir_path().to_owned();
p.release().await.unwrap();
workdir
}
#[tokio::test]
async fn acquire() {
let pool = pool(1);
// Sequential calls with the same fingerprint reuse the entry.
let workdir_one = run(&pool, 100).await;
let workdir_two = run(&pool, 100).await;
assert_eq!(workdir_one, workdir_two);
// A call with a different fingerprint launches in a new workdir and succeeds.
let workdir_three = run(&pool, 200).await;
assert_ne!(workdir_two, workdir_three);
}