ntest_timeout-0.8.1/.cargo_vcs_info.json0000644000000001530000000000100137440ustar { "git": { "sha1": "a990cf5ad5f9b266ee744cdfb9d904baa6116203" }, "path_in_vcs": "ntest_timeout" }ntest_timeout-0.8.1/Cargo.toml0000644000000023050000000000100117430ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "ntest_timeout" version = "0.8.1" authors = ["Armin Becher "] description = "Timeout attribute for the ntest framework." documentation = "https://docs.rs/ntest" readme = "README.md" keywords = [ "test", "tests", "unit", "testing", "timeout", ] categories = [ "development-tools", "development-tools::testing", ] license = "MIT" repository = "https://github.com/becheran/ntest" [lib] name = "ntest_timeout" proc-macro = true [dependencies.ntest_proc_macro_helper] version = "0.8" [dependencies.proc-macro-crate] version = "1.1" [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "1.0" features = ["full"] ntest_timeout-0.8.1/Cargo.toml.orig000064400000000000000000000013410072674642500154530ustar 00000000000000[package] name = "ntest_timeout" version = "0.8.1" authors = [ "Armin Becher ",] edition = "2018" description = "Timeout attribute for the ntest framework." keywords = [ "test", "tests", "unit", "testing", "timeout",] categories = [ "development-tools", "development-tools::testing",] readme = "README.md" license = "MIT" repository = "https://github.com/becheran/ntest" documentation = "https://docs.rs/ntest" [lib] name = "ntest_timeout" proc-macro = true [dependencies] quote = "1.0" proc-macro2 = "1.0" proc-macro-crate = "1.1" [dependencies.syn] version = "1.0" features = [ "full",] [dependencies.ntest_proc_macro_helper] version = "0.8" path = "../ntest_proc_macro_helper" ntest_timeout-0.8.1/LICENSE000064400000000000000000000021000072674642500135630ustar 00000000000000MIT License Copyright (c) 2019 Armin Becher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.ntest_timeout-0.8.1/README.md000064400000000000000000000003370072674642500140470ustar 00000000000000# NTest Timeout Part of the [NTest library](https://crates.io/crates/ntest). Add the timeout attribute to the rust test framework using [procedural macros](https://doc.rust-lang.org/reference/procedural-macros.html). ntest_timeout-0.8.1/src/lib.rs000064400000000000000000000076030072674642500144760ustar 00000000000000//! Part of the ntest library. Add timeout attribute to the rust test framework. extern crate proc_macro; extern crate syn; use proc_macro::TokenStream; use quote::quote; use syn::parse_macro_input; /// The timeout attribute can be used for tests to let them fail if they exceed a certain execution time. /// With the `#[timeout]` attribute a timeout in milliseconds is added to a test. /// /// The function input must be of type `int`. For example `#[timeout(10)]` will fail if the test takes longer than 10 milliseconds. /// /// # Examples /// /// This example will not panic /// /// ``` /// #[test] /// #[timeout(100)] /// fn no_timeout() { /// let fifty_millis = time::Duration::from_millis(50); /// thread::sleep(fifty_millis); /// } /// ``` /// /// This example will panic and break the infinite loop after 10 milliseconds. /// /// ``` /// #[test] /// #[timeout(10)] /// #[should_panic] /// fn timeout() { /// loop {}; /// } /// ``` /// /// Also works with test functions using a Result: /// /// ``` /// #[test] /// #[timeout(100)] /// fn timeout_with_result() -> Result<(), String> { /// let ten_millis = time::Duration::from_millis(10); /// thread::sleep(ten_millis); /// Ok(()) /// } /// ``` #[proc_macro_attribute] pub fn timeout(attr: TokenStream, item: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(item as syn::ItemFn); let time_ms = get_timeout(&parse_macro_input!(attr as syn::AttributeArgs)); let sig = &input.sig; let output = &sig.output; let body = &input.block; check_other_attributes(&input); let result = quote! { #sig { fn ntest_callback() #output #body let ntest_timeout_now = std::time::Instant::now(); match ntest::ntest_proc_macro_helper::execute_with_timeout(&ntest_callback, #time_ms as u64) { Some(result) => return result, None => panic!("timeout: the function call took {} ms. Max time {} ms", ntest_timeout_now.elapsed().as_millis(), #time_ms), } } }; result.into() } fn check_other_attributes(input: &syn::ItemFn) { for attribute in &input.attrs { let meta = attribute.parse_meta(); match meta { Ok(m) => match m { syn::Meta::Path(p) => { let identifier = p.get_ident().expect("Expected identifier!"); if identifier == "timeout" { panic!("Timeout attribute is only allowed once"); } } syn::Meta::List(ml) => { let identifier = ml.path.get_ident().expect("Expected identifier!"); if identifier == "timeout" { panic!("Timeout attribute is only allowed once"); } } syn::Meta::NameValue(nv) => { let identifier = nv.path.get_ident().expect("Expected identifier!"); if identifier == "timeout" { panic!("Timeout attribute is only allowed once"); } } }, Err(e) => panic!("Could not determine meta data. Error {}.", e), } } } fn get_timeout(attribute_args: &syn::AttributeArgs) -> u128 { if attribute_args.len() > 1 { panic!("Only one integer expected. Example: #[timeout(10)]"); } match &attribute_args[0] { syn::NestedMeta::Meta(_) => { panic!("Integer expected. Example: #[timeout(10)]"); } syn::NestedMeta::Lit(lit) => match lit { syn::Lit::Int(int) => int.base10_parse::().expect("Integer expected"), _ => { panic!("Integer as timeout in ms expected. Example: #[timeout(10)]"); } }, } }