Skip to content

Commit a49b2e0

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

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

Diff for: 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

Diff for: 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 {

Diff for: fastimer-tokio/tests/lifetime.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 std::time::Duration;
16+
use std::time::Instant;
17+
18+
use fastimer_tokio::MakeTokioDelay;
19+
20+
fn timer() -> &'static MakeTokioDelay {
21+
static TIMER: MakeTokioDelay = MakeTokioDelay;
22+
&TIMER
23+
}
24+
25+
struct TimerWrapper {
26+
timer: &'static MakeTokioDelay,
27+
}
28+
29+
fn trick_timer() -> TimerWrapper {
30+
TimerWrapper { timer: timer() }
31+
}
32+
33+
impl fastimer::MakeDelay for TimerWrapper {
34+
type Delay = tokio::time::Sleep;
35+
36+
fn delay_util(&self, at: Instant) -> Self::Delay {
37+
self.timer.delay_util(at)
38+
}
39+
40+
fn delay(&self, duration: Duration) -> Self::Delay {
41+
self.timer.delay(duration)
42+
}
43+
}
44+
45+
#[test]
46+
fn test_lifetime_ok() {
47+
let rt1 = tokio::runtime::Runtime::new().unwrap();
48+
let rt2 = tokio::runtime::Runtime::new().unwrap();
49+
50+
let spawn = rt1.spawn(async move {
51+
let mut interval = fastimer::interval(Duration::from_secs(1), trick_timer());
52+
for _ in 0..3 {
53+
interval.tick().await;
54+
}
55+
});
56+
57+
rt2.block_on(spawn).unwrap();
58+
}
59+
60+
// #[test]
61+
// fn test_lifetime_nok() {
62+
// let rt1 = tokio::runtime::Runtime::new().unwrap();
63+
// let rt2 = tokio::runtime::Runtime::new().unwrap();
64+
//
65+
// let spawn = rt1.spawn(async move {
66+
// let mut interval = fastimer::interval(Duration::from_secs(1), timer());
67+
// for _ in 0..3 {
68+
// interval.tick().await;
69+
// }
70+
// });
71+
//
72+
// rt2.block_on(spawn).unwrap();
73+
// }

0 commit comments

Comments
 (0)