use tokio::sync::mpsc;
use surrealcs_kernel::messages::server::wrapper::WrappedServerMessage;
use surrealcs_kernel::{allocator::Allocator, messages::client::message::TransactionMessage};
pub async fn send_transaction_operation(
unwrapped_message: WrappedServerMessage,
allocator: &mut Allocator<mpsc::Sender<TransactionMessage>>,
) {
let sender = match allocator.extract_connection(unwrapped_message.client_id) {
Ok(unwrapped_sender) => unwrapped_sender.0,
Err(e) => {
tracing::error!("{:?}", e);
return ();
}
};
let message = TransactionMessage::TransactionOperation(unwrapped_message);
match sender.send(message).await {
Ok(_) => {}
Err(e) => {
tracing::error!(
"error when sending message back to client for transaction operation: {}",
e
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use surrealcs_kernel::messages::server::interface::ServerMessage;
use surrealcs_kernel::messages::server::interface::ServerTransactionMessage;
use tokio::time::{timeout, Duration};
#[tokio::test]
async fn test_send_transaction_operation() {
let (tx, mut rx) = mpsc::channel(1);
let mut allocator = Allocator::new();
let id = 0;
allocator.allocate(tx);
let message = WrappedServerMessage {
client_id: id,
message: ServerMessage::SendOperation(ServerTransactionMessage::Commit),
server_id: Some(0),
connection_id: "some fake test ID".to_string(),
transaction_id: Some("some fake transaction ID".to_string()),
};
send_transaction_operation(message, &mut allocator).await;
let message = timeout(Duration::from_secs(1), rx.recv()).await.unwrap().unwrap();
match message {
TransactionMessage::TransactionOperation(_) => {}
_ => panic!("expected a transaction operation message"),
}
}
}