Commit 3c02262f authored by Nicolas Patry's avatar Nicolas Patry
Browse files

Reduce race condition on file system for test

parent c6bb7670
...@@ -300,15 +300,22 @@ mod tests { ...@@ -300,15 +300,22 @@ mod tests {
use tokenizers::Tokenizer; use tokenizers::Tokenizer;
pub(crate) async fn get_tokenizer() -> Tokenizer { pub(crate) async fn get_tokenizer() -> Tokenizer {
if !std::path::Path::new("tokenizer.json").exists() { let filename = std::path::Path::new("tokenizer.json");
if !filename.exists() {
let content = reqwest::get("https://huggingface.co/gpt2/raw/main/tokenizer.json") let content = reqwest::get("https://huggingface.co/gpt2/raw/main/tokenizer.json")
.await .await
.unwrap() .unwrap()
.bytes() .bytes()
.await .await
.unwrap(); .unwrap();
let mut file = std::fs::File::create("tokenizer.json").unwrap(); let tmp_filename = "tokenizer.json.temp";
let mut file = std::fs::File::create(tmp_filename).unwrap();
file.write_all(&content).unwrap(); file.write_all(&content).unwrap();
// Re-check if another process has written this file maybe.
if !filename.exists() {
std::fs::rename(tmp_filename, filename).unwrap()
}
} }
Tokenizer::from_file("tokenizer.json").unwrap() Tokenizer::from_file("tokenizer.json").unwrap()
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment