Skip to content

Commit cdcdd2a

Browse files
committed
fix: error of test case
1 parent 2477056 commit cdcdd2a

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

examples/demo_async_tokio.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ async fn main() -> Result<()> {
1313
// You can also share a tokio runtime with delayTimer, please see api `DelayTimerBuilder::tokio_runtime` for details.
1414

1515
// Build an DelayTimer that uses the default configuration of the Smol runtime internally.
16-
let delay_timer = DelayTimerBuilder::default()
17-
.tokio_runtime_by_default()
18-
.build();
16+
let delay_timer = DelayTimerBuilder::default().build();
1917

2018
// Develop a print job that runs in an asynchronous cycle.
2119
let task_instance_chain = delay_timer.insert_task(build_task_async_print()?)?;

src/entity.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,18 @@ impl DelayTimerBuilder {
224224
.build()
225225
.ok_or_else(|| anyhow!("Missing base component, can't initialize."))?;
226226

227-
let timer = Timer::new(self.get_timer_event_sender(), shared_header.clone());
228-
229227
match self.runtime_instance.kind {
230-
RuntimeKind::Smol => self.assign_task(timer, event_handle),
228+
RuntimeKind::Smol => self.assign_task(event_handle, shared_header),
231229

232-
RuntimeKind::Tokio => self.assign_task_by_tokio(timer, event_handle, shared_header),
230+
RuntimeKind::Tokio => self.assign_task_by_tokio(event_handle, shared_header),
233231
};
234232

235233
Ok(())
236234
}
237235

238-
fn assign_task(&self, timer: Timer, event_handle: EventHandle) {
236+
fn assign_task(&mut self, event_handle: EventHandle, shared_header: SharedHeader) {
237+
let timer = Timer::new(self.get_timer_event_sender(), shared_header.clone());
238+
239239
self.run_async_schedule(timer);
240240

241241
self.run_event_handle(event_handle);
@@ -383,23 +383,22 @@ impl DelayTimer {
383383
/// This function requires the `tokio-support` feature of the `delay_timer`
384384
/// crate to be enabled.
385385
impl DelayTimerBuilder {
386-
fn assign_task_by_tokio(
387-
&mut self,
388-
timer: Timer,
389-
event_handle: EventHandle,
390-
shared_header: SharedHeader,
391-
) {
392-
self.run_async_schedule_by_tokio(timer, shared_header.clone());
386+
fn assign_task_by_tokio(&mut self, event_handle: EventHandle, shared_header: SharedHeader) {
387+
self.run_async_schedule_by_tokio(shared_header.clone());
393388
self.run_event_handle_by_tokio(event_handle, shared_header);
394389
}
395390

396-
fn run_async_schedule_by_tokio(&self, mut timer: Timer, shared_header: SharedHeader) {
391+
fn run_async_schedule_by_tokio(&mut self, shared_header: SharedHeader) {
392+
let shared_header_by_timer = shared_header.clone();
393+
let timer_event_sender = self.get_timer_event_sender();
394+
397395
if let Some(ref tokio_runtime_ref) = shared_header.runtime_instance.inner {
398396
let tokio_runtime = tokio_runtime_ref.clone();
399397
Builder::new()
400398
.name("async_schedule_tokio".into())
401399
.spawn(move || {
402400
tokio_runtime.block_on(async {
401+
let mut timer = Timer::new(timer_event_sender, shared_header_by_timer);
403402
timer.async_schedule().await;
404403
})
405404
})

src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) use log::{debug, error, info, trace};
5454
pub(crate) use smol::channel::{unbounded, Receiver as AsyncReceiver, Sender as AsyncSender};
5555
pub(crate) use smol::future::yield_now;
5656
pub(crate) use smol::lock::Mutex as AsyncMutex;
57-
pub(crate) use smol::Timer;
57+
pub(crate) use smol::Timer as AsyncTimer;
5858
pub(crate) use std::convert::{TryFrom, TryInto};
5959
pub(crate) use std::future::Future;
6060
pub(crate) use std::iter::StepBy;

src/timer/runtime_trace/sweeper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl RecyclingBins {
168168
let duration = duration.unwrap_or_else(|| Duration::from_secs(3));
169169
match self.runtime_kind {
170170
RuntimeKind::Smol => {
171-
Timer::after(duration).await;
171+
AsyncTimer::after(duration).await;
172172
}
173173

174174
RuntimeKind::Tokio => {

src/timer/timer_core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ impl Timer {
426426

427427
mod tests {
428428

429-
#[test]
430-
fn test_next_position() {
429+
#[tokio::test]
430+
async fn test_next_position() {
431431
use super::{SharedHeader, Timer, TimerEvent};
432432
use smol::channel::unbounded;
433433
use std::sync::atomic::Ordering;

tests/simulation.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::time::Duration;
1212
use smol::Timer;
1313

1414
// TODO: Please turn on `--features=full` before test.
15-
#[test]
16-
fn test_instance_state() -> anyhow::Result<()> {
15+
#[tokio::test]
16+
async fn test_instance_state() -> anyhow::Result<()> {
1717
let delay_timer = DelayTimer::new();
1818

1919
let body = || async {
@@ -52,8 +52,8 @@ fn test_instance_state() -> anyhow::Result<()> {
5252
Ok(())
5353
}
5454

55-
#[test]
56-
fn test_instance_timeout_state() -> anyhow::Result<()> {
55+
#[tokio::test]
56+
async fn test_instance_timeout_state() -> anyhow::Result<()> {
5757
let delay_timer = DelayTimer::new();
5858

5959
let body = || async {
@@ -171,8 +171,8 @@ fn test_shell_task_instance_complete_state() -> anyhow::Result<()> {
171171
Ok(())
172172
}
173173

174-
#[test]
175-
fn go_works() -> AnyResult<()> {
174+
#[tokio::test]
175+
async fn go_works() -> AnyResult<()> {
176176
// Coordinates the inner-Runtime with the external(test-thread) clock.
177177
let expression = "0/2 * * * * * *";
178178
let park_time = 2_100_000u64;
@@ -235,8 +235,8 @@ fn test_advance() -> AnyResult<()> {
235235
Ok(())
236236
}
237237

238-
#[test]
239-
fn test_maximum_parallel_runnable_num() -> AnyResult<()> {
238+
#[tokio::test]
239+
async fn test_maximum_parallel_runnable_num() -> AnyResult<()> {
240240
let delay_timer = DelayTimer::new();
241241
let share_num = Arc::new(AtomicU64::new(0));
242242
let share_num_bunshin = share_num.clone();
@@ -267,8 +267,8 @@ fn test_maximum_parallel_runnable_num() -> AnyResult<()> {
267267
Ok(())
268268
}
269269

270-
#[test]
271-
fn tests_countdown() -> AnyResult<()> {
270+
#[tokio::test]
271+
async fn tests_countdown() -> AnyResult<()> {
272272
let delay_timer = DelayTimer::new();
273273
let share_num = Arc::new(AtomicI32::new(3));
274274
let share_num_bunshin = share_num.clone();
@@ -330,7 +330,7 @@ fn test_cron_clock() -> AnyResult<()> {
330330
cron_clock::Schedule::from_str("0 10 22 * * Monday,Tuesday,Wednesday,Thursday,Friday *")?
331331
.upcoming_owned(cron_clock::Utc);
332332

333-
for i in 0..10 {
333+
for _ in 0..10 {
334334
println!("{:?}", s.next());
335335
}
336336

0 commit comments

Comments
 (0)