Skip to content

Commit ae47099

Browse files
andriyDevmate-h
authored andcommitted
Convert ProcessorTransactionLog into a trait. (bevyengine#21476)
# Objective - There was a TODO to make this type actually a trait. - bevyengine#21409 introduced a test flake on WIndows (since the transaction log file may not have been fully released by the time the next test ran), and made tests create the `crates/bevy_asset/imported_assets/log` file. ## Solution - Create two traits: `ProcessorTransactionLogFactory` and `ProcessorTransactionLog`. The former creates the latter. - Rewrite the test helper for asset processing to use a fake transaction log that doesn't do anything. - Note: pretty much the entire implementation was just reformatted into the trait. - Note: these transaction log methods are returning `BevyError` instead of something like `std::io::Error`. ## Testing - I ran the asset tests on repeat for a while (including with bevyengine#21433 where the flake was first seen and I was able to reproduce fairly quickly). No issues! Note: we could make a release notes for the fact that users can make their own transaction logs, but this feature is primarily for unit testing. Maybe a user could make a more robust transaction log, but it's really not clear that it would be important for anyone.
1 parent 7b68866 commit ae47099

File tree

3 files changed

+337
-160
lines changed

3 files changed

+337
-160
lines changed

crates/bevy_asset/src/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ mod tests {
716716
AssetWatcher, Reader,
717717
},
718718
loader::{AssetLoader, LoadContext},
719+
processor::{
720+
AssetProcessor, LogEntry, ProcessorTransactionLog, ProcessorTransactionLogFactory,
721+
},
719722
saver::AssetSaver,
720723
transformer::{AssetTransformer, TransformedAsset},
721724
Asset, AssetApp, AssetEvent, AssetId, AssetLoadError, AssetLoadFailedEvent, AssetMode,
@@ -741,6 +744,7 @@ mod tests {
741744
sync::Mutex,
742745
};
743746
use bevy_reflect::TypePath;
747+
use bevy_tasks::BoxedFuture;
744748
use core::{marker::PhantomData, time::Duration};
745749
use crossbeam_channel::Sender;
746750
use futures_lite::AsyncWriteExt;
@@ -2289,6 +2293,52 @@ mod tests {
22892293
},
22902294
));
22912295

2296+
/// A dummy transaction log factory that just creates [`FakeTransactionLog`].
2297+
struct FakeTransactionLogFactory;
2298+
2299+
impl ProcessorTransactionLogFactory for FakeTransactionLogFactory {
2300+
fn read(&self) -> BoxedFuture<'_, Result<Vec<LogEntry>, BevyError>> {
2301+
Box::pin(async move { Ok(vec![]) })
2302+
}
2303+
2304+
fn create_new_log(
2305+
&self,
2306+
) -> BoxedFuture<'_, Result<Box<dyn ProcessorTransactionLog>, BevyError>> {
2307+
Box::pin(async move { Ok(Box::new(FakeTransactionLog) as _) })
2308+
}
2309+
}
2310+
2311+
/// A dummy transaction log that just drops every log.
2312+
// TODO: In the future it's possible for us to have a test of the transaction log, so making
2313+
// this more complex may be necessary.
2314+
struct FakeTransactionLog;
2315+
2316+
impl ProcessorTransactionLog for FakeTransactionLog {
2317+
fn begin_processing<'a>(
2318+
&'a mut self,
2319+
asset: &'a AssetPath<'_>,
2320+
) -> BoxedFuture<'a, Result<(), BevyError>> {
2321+
Box::pin(async move { Ok(()) })
2322+
}
2323+
2324+
fn end_processing<'a>(
2325+
&'a mut self,
2326+
asset: &'a AssetPath<'_>,
2327+
) -> BoxedFuture<'a, Result<(), BevyError>> {
2328+
Box::pin(async move { Ok(()) })
2329+
}
2330+
2331+
fn unrecoverable(&mut self) -> BoxedFuture<'_, Result<(), BevyError>> {
2332+
Box::pin(async move { Ok(()) })
2333+
}
2334+
}
2335+
2336+
app.world()
2337+
.resource::<AssetProcessor>()
2338+
.data()
2339+
.set_log_factory(Box::new(FakeTransactionLogFactory))
2340+
.unwrap();
2341+
22922342
AppWithProcessor {
22932343
app,
22942344
source_dir,
@@ -2379,7 +2429,7 @@ mod tests {
23792429
}
23802430

23812431
#[cfg(feature = "multi_threaded")]
2382-
use crate::processor::{AssetProcessor, LoadTransformAndSave};
2432+
use crate::processor::LoadTransformAndSave;
23832433

23842434
// The asset processor currently requires multi_threaded.
23852435
#[cfg(feature = "multi_threaded")]

0 commit comments

Comments
 (0)