Skip to content

Commit 035b581

Browse files
committed
chore: type safe api.
1 parent 8976bf5 commit 035b581

File tree

5 files changed

+140
-66
lines changed

5 files changed

+140
-66
lines changed

util/movement-aptos/movement-aptos-core/src/movement_aptos.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ where
9595
let rest_api_state = self.rest_api.clone();
9696
let rest_api_polling = kestrel::task(async move {
9797
loop {
98+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
99+
println!("POLLING REST API: {:?}", rest_api);
98100
// wait for the rest api to be ready
99101
let response = reqwest::get(rest_api.rest_api_url.clone())
100102
.await
101103
.map_err(|e| MovementAptosError::Internal(e.into()))?;
104+
105+
println!("REST API RESPONSE: {:?}", response);
102106
if response.status().is_success() {
103107
rest_api_state.write().set(rest_api).await;
104108
break;
@@ -151,9 +155,16 @@ mod tests {
151155

152156
let movement_aptos = MovementAptos::<runtime::TokioTest>::try_new(node_config, None)?;
153157
let rest_api_state = movement_aptos.rest_api().read().clone();
154-
movement_aptos.run().await?;
158+
159+
let movement_aptos_task = kestrel::task(async move {
160+
movement_aptos.run().await?;
161+
Ok::<_, MovementAptosError>(())
162+
});
163+
155164
rest_api_state.wait_for(tokio::time::Duration::from_secs(30)).await?;
156165

166+
kestrel::end!(movement_aptos_task)?;
167+
157168
Ok(())
158169
}
159170
}
Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
pub mod delegated;
2+
pub mod native;
3+
pub mod tokio_test;
4+
5+
pub use delegated::*;
6+
pub use native::*;
7+
pub use tokio_test::*;
8+
19
use std::fmt::Debug;
210

311
/// Errors thrown when attempting to use a runtime.
@@ -9,14 +17,6 @@ pub enum RuntimeError {
917
Unavailable(#[source] Box<dyn std::error::Error + Send + Sync>),
1018
}
1119

12-
/// Returns true if the current runtime is multithreaded.
13-
fn is_multithreaded_runtime() -> bool {
14-
std::panic::catch_unwind(|| {
15-
tokio::task::block_in_place(|| {});
16-
})
17-
.is_ok()
18-
}
19-
2020
/// Trait for a runtime that can be used to run [MovementAptos].
2121
///
2222
/// A runtime knows statically whether it is multithreaded or not.
@@ -28,63 +28,15 @@ pub trait Runtime: Sized + Clone + Debug + Send + Sync + 'static {
2828
fn create_global_rayon_pool() -> bool;
2929
}
3030

31-
/// Tokio test runtime.
32-
#[derive(Debug, Clone)]
33-
pub struct TokioTest;
34-
35-
impl Runtime for TokioTest {
36-
/// Try to create a new runtime.
37-
fn try_new() -> Result<Self, RuntimeError> {
38-
if !is_multithreaded_runtime() {
39-
return Err(RuntimeError::Unavailable(Box::new(std::io::Error::new(
40-
std::io::ErrorKind::Other,
41-
"Tokio test runtime is not multithreaded use #[tokio::test(flavor = \"multi_thread\")] instead",
42-
))));
43-
}
44-
45-
Ok(Self)
46-
}
47-
48-
/// Whether to create a global rayon pool.
49-
fn create_global_rayon_pool() -> bool {
50-
false
51-
}
52-
}
53-
54-
/// Native runtime refers to a runtime where the global rayon pool is created within the runner.
55-
#[derive(Debug, Clone)]
56-
pub struct Native;
57-
58-
impl Runtime for Native {
59-
/// Try to create a new runtime.
60-
///
61-
/// There are no restrictions on the surrounding environment here.
62-
fn try_new() -> Result<Self, RuntimeError> {
63-
Ok(Self)
64-
}
65-
66-
/// Whether to create a global rayon pool.
67-
fn create_global_rayon_pool() -> bool {
68-
true
69-
}
31+
/// Returns true if the current runtime is multithreaded.
32+
fn is_multithreaded_runtime() -> bool {
33+
std::panic::catch_unwind(|| {
34+
tokio::task::block_in_place(|| {});
35+
})
36+
.is_ok()
7037
}
7138

72-
/// Delegated runtime refers to a runtime where the global rayon pool is created outside of the runner.
73-
///
74-
/// This is useful when we may be calling from other tasks, i.e., not at the main thread.
75-
#[derive(Debug, Clone)]
76-
pub struct Delegated;
77-
78-
impl Runtime for Delegated {
79-
/// Try to create a new runtime.
80-
///
81-
/// There are no restrictions on the surrounding environment here.
82-
fn try_new() -> Result<Self, RuntimeError> {
83-
Ok(Self)
84-
}
85-
86-
/// Whether to create a global rayon pool.
87-
fn create_global_rayon_pool() -> bool {
88-
false
89-
}
39+
/// Returns true if the current runtime is a tokio runtime.
40+
fn is_in_tokio_runtime() -> bool {
41+
tokio::runtime::Handle::try_current().is_ok()
9042
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use super::{Runtime, RuntimeError};
2+
3+
/// Delegated runtime refers to a runtime where the global rayon pool is created outside of the runner.
4+
///
5+
/// This is useful when we may be calling from other tasks, i.e., not at the main thread.
6+
#[derive(Debug, Clone)]
7+
pub struct Delegated;
8+
9+
impl Runtime for Delegated {
10+
/// Try to create a new runtime.
11+
///
12+
/// There are no restrictions on the surrounding environment here.
13+
fn try_new() -> Result<Self, RuntimeError> {
14+
Ok(Self)
15+
}
16+
17+
/// Whether to create a global rayon pool.
18+
fn create_global_rayon_pool() -> bool {
19+
false
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use super::{is_in_tokio_runtime, Runtime, RuntimeError};
2+
3+
/// Native runtime refers to a runtime where the global rayon pool is created within the runner.
4+
#[derive(Debug, Clone)]
5+
pub struct Native;
6+
7+
impl Runtime for Native {
8+
/// Try to create a new runtime.
9+
///
10+
/// There are no restrictions on the surrounding environment here.
11+
fn try_new() -> Result<Self, RuntimeError> {
12+
if is_in_tokio_runtime() {
13+
return Err(RuntimeError::Unavailable(Box::new(std::io::Error::new(
14+
std::io::ErrorKind::Other,
15+
"Native runtime is not available in a tokio runtime",
16+
))));
17+
}
18+
19+
Ok(Self)
20+
}
21+
22+
/// Whether to create a global rayon pool.
23+
fn create_global_rayon_pool() -> bool {
24+
true
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[tokio::test]
33+
async fn test_native_runtime_should_fail_in_tokio_runtime() -> Result<(), anyhow::Error> {
34+
assert!(Native::try_new().is_err());
35+
Ok(())
36+
}
37+
38+
#[test]
39+
fn test_native_runtime_should_succeed_outside_of_tokio_runtime() -> Result<(), anyhow::Error> {
40+
assert!(Native::try_new().is_ok());
41+
Ok(())
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use super::{is_in_tokio_runtime, is_multithreaded_runtime, Runtime, RuntimeError};
2+
3+
/// Tokio test runtime.
4+
#[derive(Debug, Clone)]
5+
pub struct TokioTest;
6+
7+
impl Runtime for TokioTest {
8+
/// Try to create a new runtime.
9+
fn try_new() -> Result<Self, RuntimeError> {
10+
if !is_in_tokio_runtime() || !is_multithreaded_runtime() {
11+
return Err(RuntimeError::Unavailable(Box::new(std::io::Error::new(
12+
std::io::ErrorKind::Other,
13+
"Tokio test runtime is not multithreaded use #[tokio::test(flavor = \"multi_thread\")] instead",
14+
))));
15+
}
16+
17+
Ok(Self)
18+
}
19+
20+
/// Whether to create a global rayon pool.
21+
fn create_global_rayon_pool() -> bool {
22+
false
23+
}
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[tokio::test(flavor = "multi_thread")]
31+
async fn test_tokio_test_should_succeed_in_multi_thread() -> Result<(), anyhow::Error> {
32+
TokioTest::try_new()?;
33+
Ok(())
34+
}
35+
36+
#[tokio::test]
37+
async fn test_tokio_test_should_fail_in_single_thread() -> Result<(), anyhow::Error> {
38+
assert!(TokioTest::try_new().is_err());
39+
Ok(())
40+
}
41+
42+
#[test]
43+
fn test_tokio_test_should_fail_outside_of_tokio_test() -> Result<(), anyhow::Error> {
44+
assert!(TokioTest::try_new().is_err());
45+
Ok(())
46+
}
47+
}

0 commit comments

Comments
 (0)