-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cancelling a timer also aborts the cancelled task #292
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ mod shared { | |
|
||
StartDebounce, | ||
DurationElapsed(usize, TimeResponse), | ||
Cancel(TimerId), | ||
} | ||
|
||
#[derive(Default)] | ||
|
@@ -96,6 +97,9 @@ mod shared { | |
Event::DurationElapsed(_, _) => { | ||
panic!("Unexpected debounce event") | ||
} | ||
Event::Cancel(timer_id) => { | ||
caps.time.clear(timer_id); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -249,27 +253,33 @@ mod tests { | |
} | ||
|
||
#[test] | ||
pub fn test_cancel_timer() { | ||
pub fn test_start_debounce_then_clear() { | ||
let app = AppTester::<App, _>::default(); | ||
let mut model = Model::default(); | ||
|
||
let request1 = &mut app | ||
let mut debounce = app | ||
.update(Event::StartDebounce, &mut model) | ||
.expect_one_effect() | ||
.expect_time(); | ||
|
||
assert!(model.debounce_time_id.is_some()); | ||
|
||
app.resolve_to_event_then_update( | ||
request1, | ||
TimeResponse::Cleared { | ||
id: model.debounce_time_id.unwrap(), | ||
}, | ||
&mut model, | ||
) | ||
.assert_empty(); | ||
|
||
assert!(!model.debounce_complete); | ||
assert!(model.debounce_time_id.is_none()); | ||
let timer_id = model.debounce_time_id.unwrap(); | ||
let _cancel = app | ||
.update(Event::Cancel(timer_id), &mut model) | ||
.expect_one_effect() | ||
.expect_time(); | ||
// this is a little strange-looking. We have cleared the timer, | ||
// so the in-flight debounce should have resolved. But to force that | ||
// to happen, we have to run the app, and the easiest way to do that | ||
// is to resolve the original debounce effect with a fake outcome - | ||
// which will be ignored in favour of TimeResponse::Cleared | ||
let ev = app | ||
.resolve( | ||
&mut debounce, | ||
TimeResponse::DurationElapsed { id: timer_id }, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good test that this doesn't cause issues if the shell still fires the timer. In a way, we could simply not tell the shell about the cancellation and just stop the task like you are doing which will ignore the timer when it fires. |
||
.unwrap() | ||
.expect_one_event(); | ||
let Event::DurationElapsed(_, TimeResponse::Cleared { id }) = ev else { | ||
panic!() | ||
}; | ||
assert_eq!(id, timer_id); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have to try to have specific advice, but I think you might be able to avoid the
pin_project
byself.deref_mut()
and anUnpin
trait bound for whatever is complaining about not being guaranteed unpin.Was that problematic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have removed
pin_project
, you're right, it wasn't necessary in this instance.