Skip to content

Commit be0da2d

Browse files
authored
test(resolve): avoid temp database collisions
1 parent 02c5359 commit be0da2d

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎crates/codegraph-resolve/tests/tier6_resolution.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
//! file, so the worktree stays clean.
2121
2222
use std::path::{Path, PathBuf};
23+
use std::sync::atomic::{AtomicU64, Ordering};
2324

2425
use codegraph_core::types::{Edge, EdgeKind, FileRecord, Node, NodeKind};
2526
use codegraph_extract::{detect_language, extract_file};
2627
use codegraph_resolve::ReferenceResolver;
2728
use codegraph_store::Store;
2829

30+
static NEXT_TEMP_DB: AtomicU64 = AtomicU64::new(0);
31+
2932
fn fixture_root(name: &str) -> PathBuf {
3033
Path::new(env!("CARGO_MANIFEST_DIR"))
3134
.join("tests")
@@ -35,18 +38,34 @@ fn fixture_root(name: &str) -> PathBuf {
3538
}
3639

3740
fn temp_db_path(test_name: &str) -> PathBuf {
38-
let mut path = std::env::temp_dir();
3941
let nanos = std::time::SystemTime::now()
4042
.duration_since(std::time::UNIX_EPOCH)
4143
.expect("clock after epoch")
4244
.as_nanos();
45+
temp_db_path_at(test_name, nanos)
46+
}
47+
48+
fn temp_db_path_at(test_name: &str, nanos: u128) -> PathBuf {
49+
let mut path = std::env::temp_dir();
50+
// Windows may return the same wall-clock value to parallel tests. The
51+
// process-local nonce keeps same-fixture stores collision-free regardless
52+
// of clock resolution.
53+
let nonce = NEXT_TEMP_DB.fetch_add(1, Ordering::Relaxed);
4354
path.push(format!(
44-
"codegraph-tier6-{test_name}-{}-{nanos}.db",
55+
"codegraph-tier6-{test_name}-{}-{nanos}-{nonce}.db",
4556
std::process::id()
4657
));
4758
path
4859
}
4960

61+
#[test]
62+
fn temp_db_paths_remain_unique_when_clock_does_not_advance() {
63+
assert_ne!(
64+
temp_db_path_at("same_fixture", 42),
65+
temp_db_path_at("same_fixture", 42)
66+
);
67+
}
68+
5069
/// Every indexable file under `root`, as project-relative `/`-separated paths,
5170
/// sorted so indexing order is deterministic.
5271
fn fixture_files(root: &Path) -> Vec<String> {

0 commit comments

Comments
 (0)