|
| 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