refactor: repo-actor: RepoActorLog: replace Mutex with RwLock

This commit is contained in:
Paul Campbell 2024-06-30 12:40:17 +01:00
parent db90280641
commit 3e137c6480
11 changed files with 27 additions and 27 deletions

View file

@ -16,9 +16,9 @@ use kxio::network::Network;
use tracing::{info, warn, Instrument}; use tracing::{info, warn, Instrument};
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct RepoActorLog(std::sync::Arc<std::sync::Mutex<Vec<String>>>); pub struct RepoActorLog(std::sync::Arc<std::sync::RwLock<Vec<String>>>);
impl Deref for RepoActorLog { impl Deref for RepoActorLog {
type Target = std::sync::Arc<std::sync::Mutex<Vec<String>>>; type Target = std::sync::Arc<std::sync::RwLock<Vec<String>>>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
@ -135,6 +135,6 @@ pub fn logger(log: &Option<crate::RepoActorLog>, message: impl Into<String>) {
if let Some(log) = log { if let Some(log) = log {
let message: String = message.into(); let message: String = message.into();
tracing::debug!(message); tracing::debug!(message);
let _ = log.lock().map(|mut l| l.push(message)); let _ = log.write().map(|mut l| l.push(message));
} }
} }

View file

@ -38,7 +38,7 @@ async fn when_repo_config_should_fetch_then_push_then_revalidate() -> TestResult
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: LoadConfigFromRepo"))) .any(|message| message.contains("send: LoadConfigFromRepo")))
@ -83,7 +83,7 @@ async fn when_server_config_should_fetch_then_push_then_revalidate() -> TestResu
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: ValidateRepo"))) .any(|message| message.contains("send: ValidateRepo")))

View file

@ -40,7 +40,7 @@ async fn should_fetch_then_push_then_revalidate() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: ValidateRepo"))) .any(|message| message.contains("send: ValidateRepo")))

View file

@ -26,7 +26,7 @@ async fn should_passthrough_to_receive_ci_status() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: ReceiveCIStatus"))) .any(|message| message.contains("send: ReceiveCIStatus")))

View file

@ -231,7 +231,7 @@ async fn valid_repo_with_default_remotes_should_assess_branch_positions() -> Tes
System::current().stop(); System::current().stop();
//then //then
log.lock() log.read()
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map(|l| assert!(l.iter().any(|l| l.contains("send: ValidateRepo"))))?; .map(|l| assert!(l.iter().any(|l| l.contains("send: ValidateRepo"))))?;

View file

@ -45,7 +45,7 @@ async fn when_read_file_ok_should_send_config_loaded() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: LoadedConfig"))) .any(|message| message.contains("send: LoadedConfig")))
@ -80,7 +80,7 @@ async fn when_read_file_err_should_notify_user() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l.iter().any(|message| message.contains("send: NotifyUser"))); assert!(l.iter().any(|message| message.contains("send: NotifyUser")));
assert!( assert!(
!l.iter() !l.iter()

View file

@ -54,7 +54,7 @@ async fn should_validate_repo() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: ValidateRepo"))) .any(|message| message.contains("send: ValidateRepo")))
@ -84,7 +84,7 @@ async fn should_register_webhook() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: RegisterWebhook"))) .any(|message| message.contains("send: RegisterWebhook")))

View file

@ -23,7 +23,7 @@ async fn when_pass_should_advance_main_to_next() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
let expected = format!("send: AdvanceMain({:?})", next_commit); let expected = format!("send: AdvanceMain({:?})", next_commit);
tracing::debug!(%expected,""); tracing::debug!(%expected,"");
assert!(l.iter().any(|message| message.contains(&expected))) assert!(l.iter().any(|message| message.contains(&expected)))
@ -53,7 +53,7 @@ async fn when_pending_should_recheck_ci_status() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: ValidateRepo"))) .any(|message| message.contains("send: ValidateRepo")))
@ -84,7 +84,7 @@ async fn when_fail_should_notify_user() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock() log.read()
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map(|l| assert!(l.iter().any(|message| message.contains("send: NotifyUser"))))?; .map(|l| assert!(l.iter().any(|message| message.contains("send: NotifyUser"))))?;
Ok(()) Ok(())

View file

@ -27,7 +27,7 @@ async fn when_registered_ok_should_send_webhook_registered() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("send: WebhookRegistered"))) .any(|message| message.contains("send: WebhookRegistered")))
@ -63,7 +63,7 @@ async fn when_registered_error_should_send_notify_user() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock() log.read()
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map(|l| assert!(l.iter().any(|message| message.contains("send: NotifyUser"))))?; .map(|l| assert!(l.iter().any(|message| message.contains("send: NotifyUser"))))?;
Ok(()) Ok(())

View file

@ -44,7 +44,7 @@ async fn repo_with_next_not_an_ancestor_of_dev_should_be_reset() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("NextBranchResetRequired"))) .any(|message| message.contains("NextBranchResetRequired")))
@ -95,7 +95,7 @@ async fn repo_with_next_not_on_or_near_main_should_be_reset() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("NextBranchResetRequired"))) .any(|message| message.contains("NextBranchResetRequired")))
@ -146,7 +146,7 @@ async fn repo_with_next_not_based_on_main_should_be_reset() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("NextBranchResetRequired"))) .any(|message| message.contains("NextBranchResetRequired")))
@ -196,7 +196,7 @@ async fn repo_with_next_ahead_of_main_should_check_ci_status() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
let expected = format!("send: CheckCIStatus({next_commit:?})"); let expected = format!("send: CheckCIStatus({next_commit:?})");
assert!(l.iter().any(|message| message.contains(&expected))) assert!(l.iter().any(|message| message.contains(&expected)))
})?; })?;
@ -246,7 +246,7 @@ async fn repo_with_dev_and_next_on_main_should_do_nothing() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock() log.read()
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map(|l| assert!(!l.iter().any(|message| message.contains("send:"))))?; .map(|l| assert!(!l.iter().any(|message| message.contains("send:"))))?;
Ok(()) Ok(())
@ -296,7 +296,7 @@ async fn repo_with_dev_ahead_of_next_should_advance_next() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
let expected = format!("send: AdvanceNext(({next_commit:?}, {dev_branch_log:?}))"); let expected = format!("send: AdvanceNext(({next_commit:?}, {dev_branch_log:?}))");
assert!(l.iter().any(|message| message.contains(&expected))) assert!(l.iter().any(|message| message.contains(&expected)))
})?; })?;
@ -326,7 +326,7 @@ async fn should_accept_message_with_current_token() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("accepted token: 2"))) .any(|message| message.contains("accepted token: 2")))
@ -355,7 +355,7 @@ async fn should_accept_message_with_new_token() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock().map_err(|e| e.to_string()).map(|l| { log.read().map_err(|e| e.to_string()).map(|l| {
assert!(l assert!(l
.iter() .iter()
.any(|message| message.contains("accepted token: 3"))) .any(|message| message.contains("accepted token: 3")))
@ -384,7 +384,7 @@ async fn should_reject_message_with_expired_token() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
log.lock() log.read()
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.map(|l| assert!(!l.iter().any(|message| message.contains("accepted token"))))?; .map(|l| assert!(!l.iter().any(|message| message.contains("accepted token"))))?;
Ok(()) Ok(())

View file

@ -59,7 +59,7 @@ impl RepoActorLog {
needle: impl AsRef<str>, needle: impl AsRef<str>,
) -> Result<bool, Box<dyn std::error::Error>> { ) -> Result<bool, Box<dyn std::error::Error>> {
let found = self let found = self
.lock() .read()
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
.iter() .iter()
.any(|message| message.contains(needle.as_ref())); .any(|message| message.contains(needle.as_ref()));