use laravel::Application;
use laravel::config::{Env, Section};
use laravel::container::Inject;
use laravel::queue::Queueable;
use laravel::queue::{self, Batch, Callback, callback, job};
use crate::queue::Records;
#[derive(Queueable)]
struct Work(bool);
#[job]
impl Work {
async fn handle(self) -> laravel::Result<()> {
if self.0 {
return Err(laravel::abort_with(500, "import failed"));
}
Ok(())
}
}
mod callbacks {
use super::*;
#[callback(name = "tests.import.finished")]
pub async fn record(label: String, batch: &Batch, records: Inject<Records>, id: i64) -> laravel::Result<()> {
records.push(format!("{label}:{id}:{}", batch.name().unwrap()));
Ok(())
}
}
#[callback]
async fn finished(records: Inject<Records>) {
records.push("finished");
}
#[callback]
#[cfg(any())]
async fn disabled(value: TypeThatDoesNotExist) {}
#[callback]
async fn batch_failed(label: String, error: &laravel::Error, batch: &Batch, records: Inject<Records>) {
records.push(format!("{label}:{}:{error}", batch.name().unwrap()));
}
struct Settings {
prefix: String,
}
#[callback]
async fn chain_failed(
records: Inject<Records>,
label: String,
settings: Section<Settings>,
error: &laravel::Error,
id: i64,
) -> laravel::Result<()> {
records.push(format!("{}:{label}:{id}:{error}", settings.prefix));
Ok(())
}
#[test]
fn function_callbacks_preserve_explicit_names_and_default_to_the_function_path() {
assert_eq!(callbacks::record::NAME, "tests.import.finished");
assert_eq!(finished::NAME, "integration::queue_callbacks::finished");
}
#[cfg(feature = "database-sqlite")]
#[tokio::test]
async fn function_callbacks_capture_only_owned_arguments_and_use_the_attached_lifecycle() {
let directory = crate::support::TempDir::new("queue-function-callbacks");
let url = format!("sqlite://{}?mode=rwc", directory.join("callbacks.sqlite").display());
let app = crate::queue_batches::application(url, "sync").await;
app.scope(async || {
queue::batch([Work(false)])
.name("imports")
.then(callbacks::record("then".into(), 9))
.finally(callbacks::record("finally".into(), 10))
.finally(finished())
.await
})
.await
.unwrap();
assert_eq!(
app.resolve::<Records>().await.all(),
["then:9:imports", "finally:10:imports", "finished"]
);
}
#[cfg(feature = "database-sqlite")]
#[tokio::test]
async fn error_callbacks_receive_the_failed_batch() {
let directory = crate::support::TempDir::new("queue-function-catch");
let url = format!("sqlite://{}?mode=rwc", directory.join("callbacks.sqlite").display());
let app = crate::queue_batches::application(url, "sync").await;
app.scope(async || {
queue::batch([Work(true)])
.name("imports")
.then(callbacks::record("not called".into(), 1))
.catch(batch_failed("catch".into()))
.finally(finished())
.await
})
.await
.unwrap();
assert_eq!(
app.resolve::<Records>().await.all(),
["catch:imports:import failed", "finished"]
);
}
#[tokio::test]
async fn error_callbacks_without_a_batch_can_catch_chain_failures_and_resolve_sections() {
let app = Application::configure()
.replace_env(Env::default())
.with_config(|config, _| {
config.queue.default = "sync".into();
config.set(Settings {
prefix: "notice".into(),
});
})
.with_services(|services| {
services.instance(Records::default());
})
.create();
let error = app
.scope(async || queue::chain(Work(true)).catch(chain_failed("catch".into(), 12)).await)
.await
.unwrap_err();
assert_eq!(error.to_string(), "import failed");
assert_eq!(app.resolve::<Records>().await.all(), ["notice:catch:12:import failed"]);
}