use std::sync::Arc;
use reifydb_client::{SubscriptionConfig, WireFormat, WsClient};
use tokio::runtime::Runtime;
use crate::{
common::{cleanup_server, create_server_instance, start_server_and_get_ws_port},
ws::subscription::{create_test_table, recv_with_timeout, unique_table_name},
};
#[test]
fn test_no_changes_after_unsubscribe() {
let runtime = Arc::new(Runtime::new().unwrap());
let _guard = runtime.enter();
let mut server = create_server_instance(&runtime);
let port = start_server_and_get_ws_port(&runtime, &mut server).unwrap();
runtime.block_on(async {
let mut client = WsClient::connect(&format!("ws://[::1]:{}", port), WireFormat::Frames).await.unwrap();
client.authenticate("mysecrettoken").await.unwrap();
let table = unique_table_name("sub_after_unsub");
create_test_table(&client, &table, &[("id", "int4")]).await.unwrap();
let sub_id = client
.subscribe(&format!("from test::{}", table), SubscriptionConfig::default())
.await
.unwrap();
client.unsubscribe(&sub_id).await.unwrap();
client.command(&format!("INSERT test::{} [{{ id: 1 }}]", table), None).await.unwrap();
let change = recv_with_timeout(&mut client, 500).await;
assert!(change.is_none(), "Should NOT receive changes after unsubscribe");
client.close().await.unwrap();
});
cleanup_server(Some(server));
}
#[test]
fn test_close_cleans_up_subscriptions() {
let runtime = Arc::new(Runtime::new().unwrap());
let _guard = runtime.enter();
let mut server = create_server_instance(&runtime);
let port = start_server_and_get_ws_port(&runtime, &mut server).unwrap();
runtime.block_on(async {
let mut client = WsClient::connect(&format!("ws://[::1]:{}", port), WireFormat::Frames).await.unwrap();
client.authenticate("mysecrettoken").await.unwrap();
let table = unique_table_name("sub_close");
create_test_table(&client, &table, &[("id", "int4")]).await.unwrap();
let _sub_id = client
.subscribe(&format!("from test::{}", table), SubscriptionConfig::default())
.await
.unwrap();
client.close().await.unwrap();
});
cleanup_server(Some(server));
}
#[test]
fn test_rapid_subscribe_unsubscribe() {
let runtime = Arc::new(Runtime::new().unwrap());
let _guard = runtime.enter();
let mut server = create_server_instance(&runtime);
let port = start_server_and_get_ws_port(&runtime, &mut server).unwrap();
runtime.block_on(async {
let mut client = WsClient::connect(&format!("ws://[::1]:{}", port), WireFormat::Frames).await.unwrap();
client.authenticate("mysecrettoken").await.unwrap();
let table = unique_table_name("sub_rapid");
create_test_table(&client, &table, &[("id", "int4")]).await.unwrap();
for _ in 0..10 {
let sub_id = client
.subscribe(&format!("from test::{}", table), SubscriptionConfig::default())
.await
.unwrap();
client.unsubscribe(&sub_id).await.unwrap();
}
let sub_id = client
.subscribe(&format!("from test::{}", table), SubscriptionConfig::default())
.await
.unwrap();
assert!(!sub_id.is_empty());
client.command(&format!("INSERT test::{} [{{ id: 999 }}]", table), None).await.unwrap();
let change = recv_with_timeout(&mut client, 5000).await;
assert!(change.is_some(), "Should still receive changes after rapid cycles");
client.unsubscribe(&sub_id).await.unwrap();
client.close().await.unwrap();
});
cleanup_server(Some(server));
}