surrealcs 0.4.4

The SurrealCS client code for SurrealDB
Documentation
//! Register the transaction handle with the router actor.
use surrealcs_kernel::messages::client::message::TransactionMessage;
use tokio::sync::mpsc;

/// Registers the transaction handle with the router actor.
///
/// # Arguments
/// * `sender`: The sender to the transaction actor
/// * `router_tx`: The sender to the router actor that will do the registering
pub async fn register(
	sender: mpsc::Sender<TransactionMessage>,
	router_tx: &mpsc::Sender<TransactionMessage>,
) {
	let continue_message = TransactionMessage::Register(sender);
	match router_tx.send(continue_message).await {
		Ok(_) => {}
		Err(e) => {
			tracing::error!("Error sending register message to the router actor: {}", e);
		}
	}
}

#[cfg(test)]
mod tests {

	use super::*;
	use tokio::sync::mpsc;
	use tokio::time::{timeout, Duration};

	#[tokio::test]
	async fn test_register() {
		let (sender, mut rx) = mpsc::channel(1);
		let (router_tx, mut router_rx) = mpsc::channel(1);
		register(sender, &router_tx).await;

		let message = timeout(Duration::from_secs(1), router_rx.recv()).await.unwrap().unwrap();
		match message {
			TransactionMessage::Register(unwrapped_sender) => {
				unwrapped_sender.send(TransactionMessage::CloseConnection).await.unwrap();
				let message = timeout(Duration::from_secs(1), rx.recv()).await.unwrap().unwrap();
				match message {
					TransactionMessage::CloseConnection => {}
					_ => panic!("Expected a close connection message"),
				}
			}
			_ => panic!("Expected a register message"),
		}
	}
}