Removed memory leak in effect tests.

This commit is contained in:
Filipe Rodrigues 2024-03-06 16:26:29 +00:00
parent c9e75a148e
commit 68c971ee15
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU

View File

@ -251,24 +251,18 @@ mod test {
std::cell::OnceCell,
};
/// Leaks a value and returns a `&'static T`
///
/// This is useful because `&'static T` is `Copy`,
/// so we don't need to worry about cloning `Rc`s to
/// pass variables to effects.
fn leaked<T>(value: T) -> &'static T {
Box::leak(Box::new(value))
}
/// Ensures the function returned by `Effect::running` is the same as the future being run.
#[test]
fn running() {
// Create an effect, and save the running effect within it to `running`.
let running = self::leaked(OnceCell::new());
let effect = Effect::new(move || {
running
.set(effect::running().expect("Future wasn't running"))
.expect("Unable to set running effect");
let running = Rc::new(OnceCell::new());
let effect = Effect::new({
let running = Rc::clone(&running);
move || {
running
.set(effect::running().expect("Future wasn't running"))
.expect("Unable to set running effect");
}
});
// Then ensure the running effect is the same as the one created.
@ -286,26 +280,33 @@ mod test {
fn running_stacked() {
// Create 2 stacked effects, saving the running within each to `running1` and `running2`.
// `running1` contains the top-level effect, while `running2` contains the inner one.
let running_top = self::leaked(OnceCell::new());
let running_bottom = self::leaked(OnceCell::new());
let effect = Effect::new(move || {
running_top
.set(effect::running().expect("Future wasn't running"))
.expect("Unable to set running effect");
let effect = Effect::new(move || {
running_bottom
let running_top = Rc::new(OnceCell::new());
let running_bottom = Rc::new(OnceCell::new());
let effect = Effect::new({
let running_top = Rc::clone(&running_top);
let running_bottom = Rc::clone(&running_bottom);
move || {
running_top
.set(effect::running().expect("Future wasn't running"))
.expect("Unable to set running effect");
});
// Then ensure the bottom-level running effect is the same as the one created.
let running_bottom = running_bottom
.get()
.expect("Running effect missing")
.upgrade()
.expect("Running effect was dropped");
assert_eq!(effect, running_bottom);
let effect = Effect::new({
let running_bottom = Rc::clone(&running_bottom);
move || {
running_bottom
.set(effect::running().expect("Future wasn't running"))
.expect("Unable to set running effect");
}
});
// Then ensure the bottom-level running effect is the same as the one created.
let running_bottom = running_bottom
.get()
.expect("Running effect missing")
.upgrade()
.expect("Running effect was dropped");
assert_eq!(effect, running_bottom);
}
});
// Then ensure the top-level running effect is the same as the one created.