mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-24 17:28:40 +00:00
Add a 'seed' parameter to gpui::test macro
This lets you control the starting seed
This commit is contained in:
parent
140c8833fe
commit
9bfa5b62f2
1 changed files with 30 additions and 27 deletions
|
@ -2,7 +2,8 @@ use proc_macro::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use syn::{
|
use syn::{
|
||||||
parse_macro_input, parse_quote, AttributeArgs, ItemFn, Lit, Meta, MetaNameValue, NestedMeta,
|
parse_macro_input, parse_quote, spanned::Spanned as _, AttributeArgs, ItemFn, Lit, Meta,
|
||||||
|
NestedMeta,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
|
@ -11,7 +12,8 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
let args = syn::parse_macro_input!(args as AttributeArgs);
|
let args = syn::parse_macro_input!(args as AttributeArgs);
|
||||||
let mut max_retries = 0;
|
let mut max_retries = 0;
|
||||||
let mut iterations = 1;
|
let mut num_iterations = 1;
|
||||||
|
let mut starting_seed = 0;
|
||||||
for arg in args {
|
for arg in args {
|
||||||
match arg {
|
match arg {
|
||||||
NestedMeta::Meta(Meta::Path(name))
|
NestedMeta::Meta(Meta::Path(name))
|
||||||
|
@ -20,17 +22,25 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
namespace = format_ident!("crate");
|
namespace = format_ident!("crate");
|
||||||
}
|
}
|
||||||
NestedMeta::Meta(Meta::NameValue(meta)) => {
|
NestedMeta::Meta(Meta::NameValue(meta)) => {
|
||||||
if let Some(result) = parse_int_meta(&meta, "retries") {
|
let key_name = meta.path.get_ident().map(|i| i.to_string());
|
||||||
match result {
|
let variable = match key_name.as_ref().map(String::as_str) {
|
||||||
Ok(value) => max_retries = value,
|
Some("retries") => &mut max_retries,
|
||||||
Err(error) => return TokenStream::from(error.into_compile_error()),
|
Some("iterations") => &mut num_iterations,
|
||||||
|
Some("seed") => &mut starting_seed,
|
||||||
|
_ => {
|
||||||
|
return TokenStream::from(
|
||||||
|
syn::Error::new(meta.path.span(), "invalid argument")
|
||||||
|
.into_compile_error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else if let Some(result) = parse_int_meta(&meta, "iterations") {
|
};
|
||||||
match result {
|
|
||||||
Ok(value) => iterations = value,
|
*variable = match parse_int(&meta.lit) {
|
||||||
Err(error) => return TokenStream::from(error.into_compile_error()),
|
Ok(value) => value,
|
||||||
|
Err(error) => {
|
||||||
|
return TokenStream::from(error.into_compile_error());
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
other => {
|
other => {
|
||||||
return TokenStream::from(
|
return TokenStream::from(
|
||||||
|
@ -61,8 +71,9 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
#inner_fn
|
#inner_fn
|
||||||
|
|
||||||
let mut retries = 0;
|
let mut retries = 0;
|
||||||
let mut seed = 0;
|
let mut i = 0;
|
||||||
loop {
|
loop {
|
||||||
|
let seed = #starting_seed + i;
|
||||||
let result = std::panic::catch_unwind(|| {
|
let result = std::panic::catch_unwind(|| {
|
||||||
let (foreground, background) = #namespace::executor::deterministic(seed as u64);
|
let (foreground, background) = #namespace::executor::deterministic(seed as u64);
|
||||||
foreground.run(#inner_fn_name(#inner_fn_args));
|
foreground.run(#inner_fn_name(#inner_fn_args));
|
||||||
|
@ -70,9 +81,9 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
seed += 1;
|
|
||||||
retries = 0;
|
retries = 0;
|
||||||
if seed == #iterations {
|
i += 1;
|
||||||
|
if i == #num_iterations {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +92,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
retries += 1;
|
retries += 1;
|
||||||
println!("retrying: attempt {}", retries);
|
println!("retrying: attempt {}", retries);
|
||||||
} else {
|
} else {
|
||||||
if #iterations > 1 {
|
if #num_iterations > 1 {
|
||||||
eprintln!("failing seed: {}", seed);
|
eprintln!("failing seed: {}", seed);
|
||||||
}
|
}
|
||||||
std::panic::resume_unwind(error);
|
std::panic::resume_unwind(error);
|
||||||
|
@ -131,18 +142,10 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||||
TokenStream::from(quote!(#outer_fn))
|
TokenStream::from(quote!(#outer_fn))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_int_meta(meta: &MetaNameValue, name: &str) -> Option<syn::Result<usize>> {
|
fn parse_int(literal: &Lit) -> syn::Result<usize> {
|
||||||
let ident = meta.path.get_ident();
|
if let Lit::Int(int) = &literal {
|
||||||
if ident.map_or(false, |n| n == name) {
|
int.base10_parse()
|
||||||
if let Lit::Int(int) = &meta.lit {
|
|
||||||
Some(int.base10_parse())
|
|
||||||
} else {
|
|
||||||
Some(Err(syn::Error::new(
|
|
||||||
meta.lit.span(),
|
|
||||||
format!("{} mut be an integer", name),
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(syn::Error::new(literal.span(), "must be an integer"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue