-
Notifications
You must be signed in to change notification settings - Fork 55
Allow running jobs from the job queue in tests #4775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4227341
Make the task State::clock() return a &dyn Clock instead of a BoxClock
sandhose f69855e
Remove the duplicate clock and rng in QueueWorker
sandhose 45f15e1
Allow setting a custom clock on the QueueWorker & add one to the
sandhose 388bfc2
Test helper to run all tests in the job queue
sandhose 3728d1e
Update the deactivate admin API test to run the deactivation job
sandhose e667822
Ignore clippy lint
sandhose e955ecc
Use an async-aware mutex for the test queue worker
sandhose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ use mas_axum_utils::{ | |
}; | ||
use mas_config::RateLimitingConfig; | ||
use mas_data_model::SiteConfig; | ||
use mas_email::{MailTransport, Mailer}; | ||
use mas_i18n::Translator; | ||
use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey}; | ||
use mas_matrix::{HomeserverConnection, MockHomeserverConnection}; | ||
|
@@ -39,6 +40,7 @@ use mas_storage::{ | |
clock::MockClock, | ||
}; | ||
use mas_storage_pg::PgRepositoryFactory; | ||
use mas_tasks::QueueWorker; | ||
use mas_templates::{SiteConfigExt, Templates}; | ||
use oauth2_types::{registration::ClientRegistrationResponse, requests::AccessTokenResponse}; | ||
use rand::SeedableRng; | ||
|
@@ -113,6 +115,7 @@ pub(crate) struct TestState { | |
pub rng: Arc<Mutex<ChaChaRng>>, | ||
pub http_client: reqwest::Client, | ||
pub task_tracker: TaskTracker, | ||
queue_worker: Arc<tokio::sync::Mutex<QueueWorker>>, | ||
|
||
#[allow(dead_code)] // It is used, as it will cancel the CancellationToken when dropped | ||
cancellation_drop_guard: Arc<DropGuard>, | ||
|
@@ -235,6 +238,27 @@ impl TestState { | |
shutdown_token.child_token(), | ||
); | ||
|
||
let mailer = Mailer::new( | ||
templates.clone(), | ||
MailTransport::blackhole(), | ||
"[email protected]".parse().unwrap(), | ||
"[email protected]".parse().unwrap(), | ||
); | ||
|
||
let queue_worker = mas_tasks::init( | ||
PgRepositoryFactory::new(pool.clone()), | ||
Arc::clone(&clock), | ||
&mailer, | ||
homeserver_connection.clone(), | ||
url_builder.clone(), | ||
&site_config, | ||
shutdown_token.child_token(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let queue_worker = Arc::new(tokio::sync::Mutex::new(queue_worker)); | ||
|
||
Ok(Self { | ||
repository_factory: PgRepositoryFactory::new(pool), | ||
templates, | ||
|
@@ -254,10 +278,19 @@ impl TestState { | |
rng, | ||
http_client, | ||
task_tracker, | ||
queue_worker, | ||
cancellation_drop_guard: Arc::new(shutdown_token.drop_guard()), | ||
}) | ||
} | ||
|
||
/// Run all the available jobs in the queue. | ||
/// | ||
/// Panics if it fails to run the jobs (but not on job failures!) | ||
pub async fn run_jobs_in_queue(&self) { | ||
let mut queue = self.queue_worker.lock().await; | ||
queue.process_all_jobs_in_tests().await.unwrap(); | ||
} | ||
|
||
/// Reset the test utils to a fresh state, with the same configuration. | ||
pub async fn reset(self) -> Self { | ||
let site_config = self.site_config.clone(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.