serf_core/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(html_logo_url = "https://raw.githubusercontent.com/al8n/serf/main/art/logo_72x72.png")]
3#![forbid(unsafe_code)]
4// #![deny(warnings, missing_docs)]
5#![allow(clippy::type_complexity)]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![cfg_attr(docsrs, allow(unused_attributes))]
8
9pub(crate) mod broadcast;
10
11mod coalesce;
12
13/// Events for [`Serf`]
14pub mod event;
15
16/// Errors for `serf`.
17pub mod error;
18
19/// Delegate traits and its implementations.
20pub mod delegate;
21
22mod options;
23pub use options::*;
24
25/// The types used in `serf`.
26pub mod types;
27pub use types::coordinate;
28
29/// Secret key management.
30#[cfg(feature = "encryption")]
31#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
32pub mod key_manager;
33
34mod serf;
35pub use serf::*;
36
37mod snapshot;
38pub use snapshot::*;
39
40fn invalid_data_io_error<E: std::error::Error + Send + Sync + 'static>(e: E) -> std::io::Error {
41  std::io::Error::new(std::io::ErrorKind::InvalidData, e)
42}
43
44/// All unit test fns are exported in the `tests` module.
45/// This module is used for users want to use other async runtime,
46/// and want to use the test if memberlist also works with their runtime.
47#[cfg(feature = "test")]
48#[cfg_attr(docsrs, doc(cfg(feature = "test")))]
49pub mod tests {
50  pub use memberlist_core::tests::{AnyError, next_socket_addr_v4, next_socket_addr_v6};
51  pub use paste;
52
53  pub use super::serf::base::tests::{serf::*, *};
54
55  /// Add `test` prefix to the predefined unit test fn with a given [`Runtime`](memberlist_core::agnostic_lite::RuntimeLite)
56  #[cfg(any(feature = "test", test))]
57  #[cfg_attr(docsrs, doc(cfg(any(feature = "test", test))))]
58  #[macro_export]
59  macro_rules! unit_tests {
60    ($runtime:ty => $run:ident($($fn:ident), +$(,)?)) => {
61      $(
62        ::serf_core::tests::paste::paste! {
63          #[test]
64          fn [< test_ $fn >] () {
65            $run($fn::<$runtime>());
66          }
67        }
68      )*
69    };
70  }
71
72  /// Add `test` prefix to the predefined unit test fn with a given [`Runtime`](memberlist_core::agnostic_lite::RuntimeLite)
73  #[cfg(any(feature = "test", test))]
74  #[cfg_attr(docsrs, doc(cfg(any(feature = "test", test))))]
75  #[macro_export]
76  macro_rules! unit_tests_with_expr {
77    ($run:ident($(
78      $(#[$outer:meta])*
79      $fn:ident( $expr:expr )
80    ), +$(,)?)) => {
81      $(
82        ::serf_core::tests::paste::paste! {
83          #[test]
84          $(#[$outer])*
85          fn [< test_ $fn >] () {
86            $run(async move {
87              $expr
88            });
89          }
90        }
91      )*
92    };
93  }
94
95  /// Initialize the tracing for the unit tests.
96  pub fn initialize_tests_tracing() {
97    use std::sync::Once;
98    static TRACE: Once = Once::new();
99    TRACE.call_once(|| {
100      let filter = std::env::var("SERF_TESTING_LOG")
101        .unwrap_or_else(|_| "serf_core=debug,memberlist_core=info".to_owned());
102      memberlist_core::tracing::subscriber::set_global_default(
103        tracing_subscriber::fmt::fmt()
104          .without_time()
105          .with_line_number(true)
106          .with_env_filter(filter)
107          .with_file(false)
108          .with_target(true)
109          .with_ansi(true)
110          .finish(),
111      )
112      .unwrap();
113    });
114  }
115
116  /// Run the unit test with a given async runtime sequentially.
117  pub fn run<B, F>(block_on: B, fut: F)
118  where
119    B: FnOnce(F) -> F::Output,
120    F: std::future::Future<Output = ()>,
121  {
122    // initialize_tests_tracing();
123    block_on(fut);
124  }
125}