2023-11-13 17:19:47 +00:00
|
|
|
// Tests for running live queries
|
|
|
|
// Supported by the storage engines and the WS protocol
|
|
|
|
|
2024-04-18 08:45:24 +00:00
|
|
|
use futures::Stream;
|
2023-11-13 17:19:47 +00:00
|
|
|
use futures::StreamExt;
|
|
|
|
use futures::TryStreamExt;
|
2024-04-18 08:45:24 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
use surrealdb::method::QueryStream;
|
2023-11-13 17:19:47 +00:00
|
|
|
use surrealdb::Action;
|
|
|
|
use surrealdb::Notification;
|
2024-04-18 08:45:24 +00:00
|
|
|
use surrealdb_core::sql::Object;
|
|
|
|
use tokio::sync::mpsc::{channel, Receiver, Sender};
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
use tracing::info;
|
2023-11-13 17:19:47 +00:00
|
|
|
|
2024-04-18 08:45:24 +00:00
|
|
|
const LQ_TIMEOUT: Duration = Duration::from_secs(1);
|
|
|
|
const MAX_NOTIFICATIONS: usize = 100;
|
2024-03-26 13:23:46 +00:00
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn live_select_table() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db.select(&table).live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
let created: Vec<RecordId> = db.create(table).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should match the created record
|
|
|
|
assert_eq!(created, vec![notification.data.clone()]);
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
|
|
|
|
// Update the record
|
|
|
|
let _: Option<RecordId> =
|
|
|
|
db.update(¬ification.data.id).content(json!({"foo": "bar"})).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// It should be updated
|
|
|
|
assert_eq!(notification.action, Action::Update);
|
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
let _: Option<RecordId> = db.delete(¬ification.data.id).await.unwrap();
|
|
|
|
// Pull the notification
|
|
|
|
let notification: Notification<RecordId> = users.next().await.unwrap().unwrap();
|
|
|
|
// It should be deleted
|
|
|
|
assert_eq!(notification.action, Action::Delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db.select(Resource::from(&table)).live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
db.create(Resource::from(&table)).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification = tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should be an object
|
|
|
|
assert!(notification.data.is_object());
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn live_select_record_id() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
|
|
|
|
{
|
2024-04-18 08:45:24 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
|
|
|
let record_id = Thing::from((table, "john".to_owned()));
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db.select(&record_id).live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
let created: Option<RecordId> = db.create(record_id).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should match the created record
|
|
|
|
assert_eq!(created, Some(notification.data.clone()));
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
|
|
|
|
// Update the record
|
|
|
|
let _: Option<RecordId> =
|
|
|
|
db.update(¬ification.data.id).content(json!({"foo": "bar"})).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// It should be updated
|
|
|
|
assert_eq!(notification.action, Action::Update);
|
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
let _: Option<RecordId> = db.delete(¬ification.data.id).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// It should be deleted
|
|
|
|
assert_eq!(notification.action, Action::Delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-18 08:45:24 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
|
|
|
let record_id = Thing::from((table, "john".to_owned()));
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db.select(Resource::from(&record_id)).live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
db.create(Resource::from(record_id)).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<Value> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should be an object
|
|
|
|
assert!(notification.data.is_object());
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn live_select_record_ranges() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db.select(&table).range("jane".."john").live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
let created: Option<RecordId> = db.create((table, "jane")).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should match the created record
|
|
|
|
assert_eq!(created, Some(notification.data.clone()));
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
|
|
|
|
// Update the record
|
|
|
|
let _: Option<RecordId> =
|
|
|
|
db.update(¬ification.data.id).content(json!({"foo": "bar"})).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// It should be updated
|
|
|
|
assert_eq!(notification.action, Action::Update);
|
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
let _: Option<RecordId> = db.delete(¬ification.data.id).await.unwrap();
|
2024-05-13 17:07:39 +00:00
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2024-05-13 17:07:39 +00:00
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
// It should be deleted
|
|
|
|
assert_eq!(notification.action, Action::Delete);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
2023-11-13 17:19:47 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users =
|
|
|
|
db.select(Resource::from(&table)).range("jane".."john").live().await.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
2024-05-13 17:07:39 +00:00
|
|
|
let created_value = match db.create(Resource::from((table, "job"))).await.unwrap() {
|
|
|
|
Value::Object(created_value) => created_value,
|
|
|
|
_ => panic!("Expected an object"),
|
|
|
|
};
|
|
|
|
|
2023-11-13 17:19:47 +00:00
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<Value> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
2023-11-13 17:19:47 +00:00
|
|
|
// The returned record should be an object
|
|
|
|
assert!(notification.data.is_object());
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
2024-05-13 17:07:39 +00:00
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
let thing = match created_value.0.get("id").unwrap() {
|
|
|
|
Value::Thing(thing) => thing,
|
|
|
|
_ => panic!("Expected a thing"),
|
|
|
|
};
|
|
|
|
db.query("DELETE $item").bind(("item", thing.clone())).await.unwrap();
|
|
|
|
|
|
|
|
// Pull the notification
|
|
|
|
let notification: Notification<Value> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
|
|
|
|
|
|
|
// It should be deleted
|
|
|
|
assert_eq!(notification.action, Action::Delete);
|
|
|
|
let notification = match notification.data {
|
|
|
|
Value::Object(notification) => notification,
|
|
|
|
_ => panic!("Expected an object"),
|
|
|
|
};
|
|
|
|
assert_eq!(notification.0, created_value.0);
|
2023-11-13 17:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
}
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
#[test_log::test(tokio::test)]
|
|
|
|
async fn live_select_query() {
|
|
|
|
let (permit, db) = new_db().await;
|
|
|
|
|
|
|
|
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL"))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
db.query(format!("DEFINE TABLE {table}")).await.unwrap();
|
|
|
|
}
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Start listening
|
2024-04-18 08:45:24 +00:00
|
|
|
info!("Starting live query");
|
|
|
|
let users: QueryStream<Notification<RecordId>> = db
|
2024-01-16 11:48:29 +00:00
|
|
|
.query(format!("LIVE SELECT * FROM {table}"))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.stream::<Notification<_>>(0)
|
|
|
|
.unwrap();
|
2024-04-18 08:45:24 +00:00
|
|
|
let users = Arc::new(RwLock::new(users));
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Create a record
|
2024-04-18 08:45:24 +00:00
|
|
|
info!("Creating record");
|
2024-01-16 11:48:29 +00:00
|
|
|
let created: Vec<RecordId> = db.create(table).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-04-18 08:45:24 +00:00
|
|
|
let notifications = receive_all_pending_notifications(users.clone(), LQ_TIMEOUT).await;
|
2024-01-16 11:48:29 +00:00
|
|
|
// It should be newly created
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
notifications.iter().map(|n| n.action).collect::<Vec<_>>(),
|
|
|
|
vec![Action::Create],
|
|
|
|
"{:?}",
|
|
|
|
notifications
|
|
|
|
);
|
|
|
|
// The returned record should match the created record
|
|
|
|
assert_eq!(created, vec![notifications[0].data.clone()]);
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Update the record
|
2024-04-18 08:45:24 +00:00
|
|
|
info!("Updating record");
|
2024-01-16 11:48:29 +00:00
|
|
|
let _: Option<RecordId> =
|
2024-04-18 08:45:24 +00:00
|
|
|
db.update(¬ifications[0].data.id).content(json!({"foo": "bar"})).await.unwrap();
|
|
|
|
let notifications = receive_all_pending_notifications(users.clone(), LQ_TIMEOUT).await;
|
2024-03-26 13:23:46 +00:00
|
|
|
|
2024-01-16 11:48:29 +00:00
|
|
|
// It should be updated
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
notifications.iter().map(|n| n.action).collect::<Vec<_>>(),
|
|
|
|
[Action::Update],
|
|
|
|
"{:?}",
|
|
|
|
notifications
|
|
|
|
);
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Delete the record
|
2024-04-18 08:45:24 +00:00
|
|
|
info!("Deleting record");
|
|
|
|
let _: Option<RecordId> = db.delete(¬ifications[0].data.id).await.unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
// Pull the notification
|
2024-04-18 08:45:24 +00:00
|
|
|
let notifications = receive_all_pending_notifications(users.clone(), LQ_TIMEOUT).await;
|
2024-01-16 11:48:29 +00:00
|
|
|
// It should be deleted
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(
|
|
|
|
notifications.iter().map(|n| n.action).collect::<Vec<_>>(),
|
|
|
|
[Action::Delete],
|
|
|
|
"{:?}",
|
|
|
|
notifications
|
|
|
|
);
|
2024-01-16 11:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL")).await.unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db
|
|
|
|
.query(format!("LIVE SELECT * FROM {table}"))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.stream::<Value>(0)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
db.create(Resource::from(&table)).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification = tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
|
|
|
|
2024-01-16 11:48:29 +00:00
|
|
|
// The returned record should be an object
|
|
|
|
assert!(notification.data.is_object());
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL")).await.unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db
|
|
|
|
.query(format!("LIVE SELECT * FROM {table}"))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.stream::<Notification<_>>(())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
let created: Vec<RecordId> = db.create(table).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
// The returned record should match the created record
|
|
|
|
assert_eq!(created, vec![notification.data.clone()]);
|
|
|
|
// It should be newly created
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(notification.action, Action::Create, "{:?}", notification);
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Update the record
|
|
|
|
let _: Option<RecordId> =
|
|
|
|
db.update(¬ification.data.id).content(json!({"foo": "bar"})).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
// It should be updated
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(notification.action, Action::Update, "{:?}", notification);
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
let _: Option<RecordId> = db.delete(¬ification.data.id).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification: Notification<RecordId> =
|
|
|
|
tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap().unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
// It should be deleted
|
2024-04-18 08:45:24 +00:00
|
|
|
assert_eq!(notification.action, Action::Delete, "{:?}", notification);
|
2024-01-16 11:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-04-05 16:05:22 +00:00
|
|
|
let table = format!("table_{}", Ulid::new());
|
2024-04-18 08:45:24 +00:00
|
|
|
db.query(format!("DEFINE TABLE {table} CHANGEFEED 10m INCLUDE ORIGINAL")).await.unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
|
|
|
|
// Start listening
|
|
|
|
let mut users = db
|
|
|
|
.query("BEGIN")
|
|
|
|
.query(format!("LIVE SELECT * FROM {table}"))
|
|
|
|
.query("COMMIT")
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.stream::<Value>(())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Create a record
|
|
|
|
db.create(Resource::from(&table)).await.unwrap();
|
|
|
|
// Pull the notification
|
2024-03-26 13:23:46 +00:00
|
|
|
let notification = tokio::time::timeout(LQ_TIMEOUT, users.next()).await.unwrap().unwrap();
|
2024-01-16 11:48:29 +00:00
|
|
|
// The returned record should be an object
|
|
|
|
assert!(notification.data.is_object());
|
|
|
|
// It should be newly created
|
|
|
|
assert_eq!(notification.action, Action::Create);
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(permit);
|
|
|
|
}
|
2024-04-18 08:45:24 +00:00
|
|
|
|
|
|
|
async fn receive_all_pending_notifications<
|
|
|
|
S: Stream<Item = Result<Notification<I>, Error>> + Unpin,
|
|
|
|
I,
|
|
|
|
>(
|
2024-05-09 21:14:03 +00:00
|
|
|
stream: Arc<RwLock<S>>,
|
2024-04-18 08:45:24 +00:00
|
|
|
timeout: Duration,
|
|
|
|
) -> Vec<Notification<I>> {
|
|
|
|
let (send, mut recv) = channel::<Notification<I>>(MAX_NOTIFICATIONS);
|
|
|
|
let we_expect_timeout = tokio::time::timeout(timeout, async move {
|
|
|
|
while let Some(notification) = stream.write().await.next().await {
|
|
|
|
send.send(notification.unwrap()).await.unwrap();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
assert!(we_expect_timeout.is_err());
|
|
|
|
let mut results = Vec::new();
|
|
|
|
while let Ok(notification) = recv.try_recv() {
|
|
|
|
results.push(notification);
|
|
|
|
}
|
|
|
|
results
|
|
|
|
}
|