Skip to content

Commit 0ae4ee0

Browse files
committed
Add test that reproduces #18808
1 parent 00f2842 commit 0ae4ee0

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

tests/render_asset_leaks.rs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,71 @@ fn check_standard_material_churn_leak() {
151151
}
152152
}
153153

154+
#[ignore = "FIXME Failing test"]
155+
#[test]
156+
fn check_mesh_churn_insert_leak() {
157+
let mut app = App::new();
158+
app.add_plugins((
159+
DefaultPlugins
160+
.build()
161+
.disable::<AudioPlugin>()
162+
.disable::<WinitPlugin>()
163+
.disable::<WindowPlugin>(),
164+
LogDiagnosticsPlugin {
165+
wait_duration: Duration::ZERO,
166+
..Default::default()
167+
},
168+
RenderAssetDiagnosticPlugin::<RenderMesh>::new(" meshes"),
169+
MeshAllocatorDiagnosticPlugin,
170+
))
171+
.add_systems(Startup, mesh_setup)
172+
.add_systems(
173+
Update,
174+
(churn_using_insert::<Mesh>, crash_on_mesh_leak_detection),
175+
);
176+
177+
app.finish();
178+
app.cleanup();
179+
180+
for _ in 0..100 {
181+
app.update();
182+
}
183+
}
184+
185+
#[ignore = "FIXME Failing test"]
186+
#[test]
187+
fn check_standard_material_churn_insert_leak() {
188+
let mut app = App::new();
189+
app.add_plugins((
190+
DefaultPlugins
191+
.build()
192+
.disable::<AudioPlugin>()
193+
.disable::<WinitPlugin>()
194+
.disable::<WindowPlugin>(),
195+
LogDiagnosticsPlugin {
196+
wait_duration: Duration::ZERO,
197+
..Default::default()
198+
},
199+
RenderAssetDiagnosticPlugin::<PreparedMaterial<StandardMaterial>>::new(" materials"),
200+
MaterialAllocatorDiagnosticPlugin::<StandardMaterial>::default(),
201+
))
202+
.add_systems(Startup, mesh_setup)
203+
.add_systems(
204+
Update,
205+
(
206+
churn_using_insert::<StandardMaterial>,
207+
crash_on_material_leak_detection::<StandardMaterial>,
208+
),
209+
);
210+
211+
app.finish();
212+
app.cleanup();
213+
214+
for _ in 0..100 {
215+
app.update();
216+
}
217+
}
218+
154219
#[derive(Resource)]
155220
struct Leaker<A: Asset>(Vec<Handle<A>>);
156221

@@ -185,13 +250,23 @@ fn churn<A: Asset>(mut assets: ResMut<Assets<A>>, mut leaker: ResMut<Leaker<A>>)
185250
}
186251
}
187252

253+
fn churn_using_insert<A: Asset>(mut assets: ResMut<Assets<A>>, leaker: Res<Leaker<A>>) {
254+
for id in &leaker.0 {
255+
let asset = assets.remove(id.id()).unwrap();
256+
assets.insert(id.id(), asset);
257+
}
258+
}
259+
188260
fn crash_on_mesh_leak_detection(diagnostic_store: Res<DiagnosticsStore>) {
189-
if let (Some(render_meshes), Some(allocations)) = (
261+
if let (Some(render_meshes), Some(slab_size), Some(allocations)) = (
190262
diagnostic_store
191263
.get_measurement(
192264
&RenderAssetDiagnosticPlugin::<RenderMesh>::render_asset_diagnostic_path(),
193265
)
194266
.filter(|diag| diag.value > 0.),
267+
diagnostic_store
268+
.get_measurement(MeshAllocatorDiagnosticPlugin::slabs_size_diagnostic_path())
269+
.filter(|diag| diag.value > 0.),
195270
diagnostic_store
196271
.get_measurement(MeshAllocatorDiagnosticPlugin::allocations_diagnostic_path())
197272
.filter(|diag| diag.value > 0.),
@@ -200,20 +275,31 @@ fn crash_on_mesh_leak_detection(diagnostic_store: Res<DiagnosticsStore>) {
200275
allocations.value < render_meshes.value * 10.,
201276
"Detected leak"
202277
);
278+
assert!(
279+
slab_size.value < (1 << 30) as f64,
280+
"Exceeded 1GB of allocations."
281+
);
203282
}
204283
}
205284

206285
fn crash_on_material_leak_detection<M: Material>(diagnostic_store: Res<DiagnosticsStore>) {
207-
if let (Some(materials), Some(allocations)) = (
286+
if let (Some(materials), Some(slab_size), Some(allocations)) = (
208287
diagnostic_store
209288
.get_measurement(
210289
&RenderAssetDiagnosticPlugin::<PreparedMaterial<M>>::render_asset_diagnostic_path(),
211290
)
212291
.filter(|diag| diag.value > 0.),
292+
diagnostic_store
293+
.get_measurement(&MaterialAllocatorDiagnosticPlugin::<M>::slabs_size_diagnostic_path())
294+
.filter(|diag| diag.value > 0.),
213295
diagnostic_store
214296
.get_measurement(&MaterialAllocatorDiagnosticPlugin::<M>::allocations_diagnostic_path())
215297
.filter(|diag| diag.value > 0.),
216298
) {
217299
assert!(allocations.value < materials.value * 10., "Detected leak");
300+
assert!(
301+
slab_size.value < (1 << 30) as f64,
302+
"Exceeded 1GB of allocations."
303+
);
218304
}
219305
}

0 commit comments

Comments
 (0)