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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::Parser as _;
use syn::punctuated::Punctuated;
use syn::Attribute;
use syn::ItemFn;
use syn::Lit;
use syn::Meta;
use syn::MetaList;
use syn::MetaNameValue;
use syn::NestedMeta;
use syn::Token;

struct FlakyTestArgs {
  times: usize,
  runtime: Runtime,
}

enum Runtime {
  Sync,
  Tokio(Option<Punctuated<NestedMeta, Token![,]>>),
}

impl Default for FlakyTestArgs {
  fn default() -> Self {
    FlakyTestArgs {
      times: 3,
      runtime: Runtime::Sync,
    }
  }
}

fn parse_attr(attr: proc_macro2::TokenStream) -> syn::Result<FlakyTestArgs> {
  let parser = Punctuated::<Meta, Token![,]>::parse_terminated;
  let punctuated = parser.parse2(attr)?;

  let mut ret = FlakyTestArgs::default();

  for meta in punctuated {
    match meta {
      Meta::Path(path) => {
        if path.is_ident("tokio") {
          ret.runtime = Runtime::Tokio(None);
        } else {
          return Err(syn::Error::new_spanned(path, "expected `tokio`"));
        }
      }
      Meta::NameValue(MetaNameValue {
        path,
        lit: Lit::Int(lit_int),
        ..
      }) => {
        if path.is_ident("times") {
          ret.times = lit_int.base10_parse::<usize>()?;
        } else {
          return Err(syn::Error::new_spanned(
            path,
            "expected `times = <int>`",
          ));
        }
      }
      Meta::List(MetaList { path, nested, .. }) => {
        if path.is_ident("tokio") {
          ret.runtime = Runtime::Tokio(Some(nested));
        } else {
          return Err(syn::Error::new_spanned(path, "expected `tokio`"));
        }
      }
      _ => {
        return Err(syn::Error::new_spanned(
          meta,
          "expected `times = <int>` or `tokio`",
        ));
      }
    }
  }

  Ok(ret)
}

/// A flaky test will be run multiple times until it passes.
///
/// # Example
///
/// ```rust
/// use flaky_test::flaky_test;
///
/// // By default it will be retried up to 3 times.
/// #[flaky_test]
/// fn test_default() {
///  println!("should pass");
/// }
///
/// // The number of max attempts can be adjusted via `times`.
/// #[flaky_test(times = 5)]
/// fn usage_with_named_args() {
///   println!("should pass");
/// }
///
/// # use std::convert::Infallible;
/// # async fn async_operation() -> Result<i32, Infallible> {
/// #   Ok(42)
/// # }
/// // Async tests can be run by passing `tokio`.
/// // Make sure `tokio` is added in your `Cargo.toml`.
/// #[flaky_test(tokio)]
/// async fn async_test() {
///   let res = async_operation().await.unwrap();
///   assert_eq!(res, 42);
/// }
///
/// // `tokio` and `times` can be combined.
/// #[flaky_test(tokio, times = 5)]
/// async fn async_test_five_times() {
///   let res = async_operation().await.unwrap();
///   assert_eq!(res, 42);
/// }
///
/// // Any arguments that `#[tokio::test]` supports can be specified.
/// #[flaky_test(tokio(flavor = "multi_thraed", worker_threads = 2))]
/// async fn async_test_complex() {
///   let res = async_operation().await.unwrap();
///   assert_eq!(res, 42);
/// }
/// ```
#[proc_macro_attribute]
pub fn flaky_test(attr: TokenStream, input: TokenStream) -> TokenStream {
  let attr = proc_macro2::TokenStream::from(attr);
  let mut input = proc_macro2::TokenStream::from(input);

  match inner(attr, input.clone()) {
    Err(e) => {
      input.extend(e.into_compile_error());
      input.into()
    }
    Ok(t) => t.into(),
  }
}

fn inner(
  attr: proc_macro2::TokenStream,
  input: proc_macro2::TokenStream,
) -> syn::Result<proc_macro2::TokenStream> {
  let args = parse_attr(attr)?;
  let input_fn: ItemFn = syn::parse2(input)?;
  let attrs = input_fn.attrs.clone();

  match args.runtime {
    Runtime::Sync => sync(input_fn, attrs, args.times),
    Runtime::Tokio(tokio_args) => {
      tokio(input_fn, attrs, args.times, tokio_args)
    }
  }
}

fn sync(
  input_fn: ItemFn,
  attrs: Vec<Attribute>,
  times: usize,
) -> syn::Result<proc_macro2::TokenStream> {
  let fn_name = input_fn.sig.ident.clone();

  Ok(quote! {
    #[test]
    #(#attrs)*
    fn #fn_name() {
      #input_fn

      for i in 0..#times {
        println!("flaky_test retry {}", i);
        let r = ::std::panic::catch_unwind(|| {
          #fn_name();
        });
        if r.is_ok() {
          return;
        }
        if i == #times - 1 {
          ::std::panic::resume_unwind(r.unwrap_err());
        }
      }
    }
  })
}

fn tokio(
  input_fn: ItemFn,
  attrs: Vec<Attribute>,
  times: usize,
  tokio_args: Option<Punctuated<NestedMeta, Token![,]>>,
) -> syn::Result<proc_macro2::TokenStream> {
  if input_fn.sig.asyncness.is_none() {
    return Err(syn::Error::new_spanned(input_fn.sig, "must be `async fn`"));
  }

  let fn_name = input_fn.sig.ident.clone();
  let tokio_macro = match tokio_args {
    Some(args) => quote! { #[::tokio::test(#args)] },
    None => quote! { #[::tokio::test] },
  };

  Ok(quote! {
    #tokio_macro
    #(#attrs)*
    async fn #fn_name() {
      #input_fn

      for i in 0..#times {
        println!("flaky_test retry {}", i);
        let fut = ::std::panic::AssertUnwindSafe(#fn_name());
        let r = <_ as ::flaky_test::futures_util::future::FutureExt>::catch_unwind(fut).await;
        if r.is_ok() {
          return;
        }
        if i == #times - 1 {
          ::std::panic::resume_unwind(r.unwrap_err());
        }
      }
    }
  })
}