Skip to content

Testing

UseInMemoryBroker() replaces RabbitMQ with queues in the test’s process. Jobs are published, routed, retried and delayed as on RabbitMQ, but nothing leaves the process.

var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IOrderStore>(orders); // whatever your jobs need
services.AddMassiveJobs()
.WithJobsFromAssemblyOf<SendInvoice>()
.UseInMemoryBroker();
await using var provider = services.BuildServiceProvider();
var jobs = MassiveJobsMediator.Initialize(provider);
await jobs.StartAsync();
try
{
await jobs.PublishAsync<SendInvoice, int>(42);
await WaitUntil(() => orders.Get(42).InvoiceSent);
}
finally
{
await jobs.StopAsync();
MassiveJobsMediator.Deinitialize();
}

MassiveJobsMediator is an IJobPublisher too, so tests can publish without a scope. The jobs run on other threads: wait for their effect with a timeout (a loop that checks a condition), not a fixed sleep.

MassiveJobsMediator.Initialize sets the static instance the static PublishAsync/Publish methods use, so tests that start MassiveJobs this way can’t run in parallel with each other.

MassiveJobs reads the clock and waits only through the TimeProvider in DI: scheduled jobs, retries, timeouts, periodic runs, the in-memory broker’s delays. Register a FakeTimeProvider (package Microsoft.Extensions.TimeProvider.Testing) and move time yourself:

var time = new FakeTimeProvider(new DateTimeOffset(2026, 1, 5, 9, 0, 0, TimeSpan.Zero));
services.AddSingleton<TimeProvider>(time);
services.AddMassiveJobs().WithJobsFromAssemblyOf<RemindAbandonedCart>().UseInMemoryBroker();
// ...start as above
await jobs.PublishAsync<RemindAbandonedCart, int>(7, TimeSpan.FromMinutes(15));
time.Advance(TimeSpan.FromMinutes(14));
Assert.IsFalse(reminders.Sent(7)); // not due yet
time.Advance(TimeSpan.FromMinutes(1));
await WaitUntil(() => reminders.Sent(7)); // due: it runs

A delay starts when its message reaches the broker. Advance the clock only after that: for a job you published, once PublishAsync returned; for a retry, once the failed run is over (wait for it, e.g. for the job’s own record of the attempt). Time advanced before a timer exists doesn’t count for it.

The same works for retries (advance past the backoff), job timeouts and periodic jobs (advance to the next run time).

A job is a class: construct it with fakes and call Perform. That covers its logic; the in-memory broker covers its registration, arguments and wiring.