Skip to content

Commit a76e0a2

Browse files
authored
Make GatedReader a test-only symbol, and allow all bevy_asset tests to all run single threaded. (#18473)
# Objective - Some tests have been disabled in single-threaded mode. - This was because the channel blocks the executing thread when in the asset reader. But that means we never get to the test line to open the gate! So we need multithreading just to let the reader block and the test can make progress in the background. ## Solution - The solution to allowing all tests to run single threaded is to make `GatedReader` await for gates to open rather than blocking on it. - This however requires adding `async-channel` (already in tree). To avoid this additional dependency in most cases, I made this a dev-dependency. - However, this now means that `GatedReader` can only be used in tests (in bevy_asset). We could make this a feature flag, but it seems highly unlikely that real users need a `GatedReader` - and if they need it for their test, they can just fork the `GatedReader`. ## Testing - The `bevy_asset` tests pass without setting the `--features multi_threaded` flag! --- ## Migration Guide `bevy_asset::io::gated::GatedReader` and `bevy_asset::io::gated::GatedOpener` are no longer accessible to users.
1 parent 7baef26 commit a76e0a2

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

crates/bevy_asset/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.17.0-dev", default-featu
8888
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
8989
notify-debouncer-full = { version = "0.5.0", default-features = false, optional = true }
9090

91+
[dev-dependencies]
92+
async-channel = "2"
93+
9194
[lints]
9295
workspace = true
9396

crates/bevy_asset/src/io/gated.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::io::{AssetReader, AssetReaderError, PathStream, Reader};
22
use alloc::{boxed::Box, sync::Arc};
3+
use async_channel::{Receiver, Sender};
34
use bevy_platform::collections::HashMap;
4-
use crossbeam_channel::{Receiver, Sender};
55
use parking_lot::RwLock;
66
use std::path::Path;
77

@@ -35,8 +35,8 @@ impl GateOpener {
3535
let mut gates = self.gates.write();
3636
let gates = gates
3737
.entry_ref(path.as_ref())
38-
.or_insert_with(crossbeam_channel::unbounded);
39-
gates.0.send(()).unwrap();
38+
.or_insert_with(async_channel::unbounded);
39+
gates.0.send_blocking(()).unwrap();
4040
}
4141
}
4242

@@ -61,10 +61,10 @@ impl<R: AssetReader> AssetReader for GatedReader<R> {
6161
let mut gates = self.gates.write();
6262
let gates = gates
6363
.entry_ref(path.as_ref())
64-
.or_insert_with(crossbeam_channel::unbounded);
64+
.or_insert_with(async_channel::unbounded);
6565
gates.1.clone()
6666
};
67-
receiver.recv().unwrap();
67+
receiver.recv().await.unwrap();
6868
let result = self.reader.read(path).await?;
6969
Ok(result)
7070
}

crates/bevy_asset/src/io/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ pub mod android;
1010
pub mod embedded;
1111
#[cfg(not(target_arch = "wasm32"))]
1212
pub mod file;
13-
pub mod gated;
1413
pub mod memory;
1514
pub mod processor_gated;
1615
#[cfg(target_arch = "wasm32")]
1716
pub mod wasm;
1817

18+
#[cfg(test)]
19+
pub mod gated;
20+
1921
mod source;
2022

2123
pub use futures_lite::AsyncWriteExt;

crates/bevy_asset/src/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,6 @@ mod tests {
893893

894894
#[test]
895895
fn load_dependencies() {
896-
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
897-
#[cfg(not(feature = "multi_threaded"))]
898-
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
899-
900896
let dir = Dir::default();
901897

902898
let a_path = "a.cool.ron";
@@ -1201,10 +1197,6 @@ mod tests {
12011197

12021198
#[test]
12031199
fn failure_load_states() {
1204-
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
1205-
#[cfg(not(feature = "multi_threaded"))]
1206-
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
1207-
12081200
let dir = Dir::default();
12091201

12101202
let a_path = "a.cool.ron";
@@ -1334,10 +1326,6 @@ mod tests {
13341326

13351327
#[test]
13361328
fn dependency_load_states() {
1337-
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
1338-
#[cfg(not(feature = "multi_threaded"))]
1339-
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
1340-
13411329
let a_path = "a.cool.ron";
13421330
let a_ron = r#"
13431331
(
@@ -1473,10 +1461,6 @@ mod tests {
14731461

14741462
#[test]
14751463
fn manual_asset_management() {
1476-
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
1477-
#[cfg(not(feature = "multi_threaded"))]
1478-
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
1479-
14801464
let dir = Dir::default();
14811465
let dep_path = "dep.cool.ron";
14821466

@@ -1574,10 +1558,6 @@ mod tests {
15741558

15751559
#[test]
15761560
fn load_folder() {
1577-
// The particular usage of GatedReader in this test will cause deadlocking if running single-threaded
1578-
#[cfg(not(feature = "multi_threaded"))]
1579-
panic!("This test requires the \"multi_threaded\" feature, otherwise it will deadlock.\ncargo test --package bevy_asset --features multi_threaded");
1580-
15811561
let dir = Dir::default();
15821562

15831563
let a_path = "text/a.cool.ron";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: `GatedReader` and `GatedOpener` are now private.
3+
pull_requests: [18473]
4+
---
5+
6+
The `GatedReader` and `GatedOpener` for `bevy_asset` have been made private. These were really only
7+
for testing, but were being compiled even in release builds. Now they are guarded by `#[cfg(test)]`!
8+
9+
If you were using this in your own tests, you could fork the `GatedReader` (it still exists in the
10+
Bevy repo!) into your own code, or write your own version (if more useful to you).

0 commit comments

Comments
 (0)