Skip to content

Commit df95a68

Browse files
committed
test: demo the implementation is not general enough issue
Signed-off-by: tison <[email protected]>
1 parent 13c9795 commit df95a68

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

fastimer-tokio/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ time = ["dep:fastimer", "dep:tokio", "tokio/time"]
3737
fastimer = { workspace = true, optional = true }
3838
tokio = { workspace = true, optional = true }
3939

40+
[dev-dependencies]
41+
tokio = { workspace = true, features = ["full"] }
42+
4043
[lints]
4144
workspace = true

fastimer-tokio/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod delay {
3131
#[derive(Clone, Copy, Debug, Default)]
3232
pub struct MakeTokioDelay;
3333

34-
impl MakeDelay for MakeTokioDelay {
34+
impl MakeDelay for &'static MakeTokioDelay {
3535
type Delay = tokio::time::Sleep;
3636

3737
fn delay_util(&self, at: Instant) -> Self::Delay {

fastimer-tokio/tests/lifetime.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2024 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use fastimer_tokio::MakeTokioDelay;
16+
use std::time::{Duration, Instant};
17+
18+
fn timer() -> &'static MakeTokioDelay {
19+
static TIMER: MakeTokioDelay = MakeTokioDelay;
20+
&TIMER
21+
}
22+
23+
struct TimerWrapper {
24+
timer: &'static MakeTokioDelay,
25+
}
26+
27+
fn trick_timer() -> TimerWrapper {
28+
TimerWrapper { timer: timer() }
29+
}
30+
31+
impl fastimer::MakeDelay for TimerWrapper {
32+
type Delay = tokio::time::Sleep;
33+
34+
fn delay_util(&self, at: Instant) -> Self::Delay {
35+
self.timer.delay_util(at)
36+
}
37+
38+
fn delay(&self, duration: Duration) -> Self::Delay {
39+
self.timer.delay(duration)
40+
}
41+
}
42+
43+
#[test]
44+
fn test_lifetime_ok() {
45+
let rt1 = tokio::runtime::Runtime::new().unwrap();
46+
let rt2 = tokio::runtime::Runtime::new().unwrap();
47+
48+
let spawn = rt1.spawn(async move {
49+
let mut interval = fastimer::interval(Duration::from_secs(1), trick_timer());
50+
for _ in 0..3 {
51+
interval.tick().await;
52+
}
53+
});
54+
55+
rt2.block_on(spawn).unwrap();
56+
}
57+
58+
// #[test]
59+
// fn test_lifetime_nok() {
60+
// let rt1 = tokio::runtime::Runtime::new().unwrap();
61+
// let rt2 = tokio::runtime::Runtime::new().unwrap();
62+
//
63+
// let spawn = rt1.spawn(async move {
64+
// let mut interval = fastimer::interval(Duration::from_secs(1), timer());
65+
// for _ in 0..3 {
66+
// interval.tick().await;
67+
// }
68+
// });
69+
//
70+
// rt2.block_on(spawn).unwrap();
71+
// }

0 commit comments

Comments
 (0)