Skip to content

Commit 05004c5

Browse files
committed
Write a test to show that we can't add or remove processed sources after the processor starts.
1 parent 5e8489c commit 05004c5

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

crates/bevy_asset/src/processor/tests.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use bevy_tasks::BoxedFuture;
2323
use crate::{
2424
io::{
2525
memory::{Dir, MemoryAssetReader, MemoryAssetWriter},
26-
AssetReader, AssetReaderError, AssetSourceBuilder, AssetSourceEvent, AssetWatcher,
27-
PathStream, Reader,
26+
AddSourceError, AssetReader, AssetReaderError, AssetSourceBuilder, AssetSourceEvent,
27+
AssetWatcher, PathStream, Reader, RemoveSourceError,
2828
},
2929
processor::{
3030
AssetProcessor, LoadTransformAndSave, LogEntry, ProcessorState, ProcessorTransactionLog,
@@ -33,8 +33,8 @@ use crate::{
3333
saver::AssetSaver,
3434
tests::{run_app_until, CoolText, CoolTextLoader, CoolTextRon, SubText},
3535
transformer::{AssetTransformer, TransformedAsset},
36-
Asset, AssetApp, AssetLoader, AssetMode, AssetPath, AssetPlugin, DefaultAssetSource,
37-
LoadContext,
36+
Asset, AssetApp, AssetLoader, AssetMode, AssetPath, AssetPlugin, AssetServer,
37+
DefaultAssetSource, LoadContext,
3838
};
3939

4040
#[derive(Clone)]
@@ -1491,3 +1491,55 @@ fn only_reprocesses_wrong_hash_on_startup() {
14911491
serialize_as_cool_text("dep_changed processed DIFFERENT processed")
14921492
);
14931493
}
1494+
1495+
// TODO: Replace this test once the asset processor can handle adding and removing sources.
1496+
#[test]
1497+
fn fails_to_add_or_remove_source_after_processor_starts() {
1498+
let AppWithProcessor {
1499+
mut app,
1500+
source_gate,
1501+
..
1502+
} = create_app_with_asset_processor();
1503+
1504+
let asset_server = app.world().resource::<AssetServer>().clone();
1505+
1506+
app.register_asset_source("custom_1", create_source(source_gate.clone()).0);
1507+
// Despite the source being processed, we can remove it before the processor starts.
1508+
asset_server.remove_source("custom_1").unwrap();
1509+
1510+
// We can still add processed sources before the processor starts.
1511+
asset_server
1512+
.add_source("custom_2", &mut create_source(source_gate.clone()).0)
1513+
.unwrap();
1514+
1515+
let guard = source_gate.write_blocking();
1516+
// The processor starts as soon as we update for the first time.
1517+
app.update();
1518+
// Starting the processor task does not guarantee that the start flag is set in multi_threaded,
1519+
// so wait for processing to finish to avoid that race condition.
1520+
run_app_until_finished_processing(&mut app, guard);
1521+
1522+
// We can't remove the source because it is processed.
1523+
assert_eq!(
1524+
asset_server.remove_source("custom_2"),
1525+
Err(RemoveSourceError::SourceIsProcessed)
1526+
);
1527+
1528+
// We can't add a processed source, since the processor has started.
1529+
assert_eq!(
1530+
asset_server.add_source("custom_3", &mut create_source(source_gate).0),
1531+
Err(AddSourceError::SourceIsProcessed)
1532+
);
1533+
1534+
// However we can add unprocessed sources even after the processor has started!
1535+
asset_server
1536+
.add_source(
1537+
"custom_4",
1538+
&mut AssetSourceBuilder::new(move || {
1539+
Box::new(MemoryAssetReader {
1540+
root: Dir::default(),
1541+
})
1542+
}),
1543+
)
1544+
.unwrap();
1545+
}

0 commit comments

Comments
 (0)