| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use crate::error::ChatError; |
| 22 |
use crate::event::ChatEvent; |
| 23 |
use crate::hub::{Hub, Subscription}; |
| 24 |
use crate::ids::{MessageId, RoomId, UserId}; |
| 25 |
use crate::message::Message; |
| 26 |
|
| 27 |
|
| 28 |
pub struct ChatStream { |
| 29 |
backlog: std::vec::IntoIter<Message>, |
| 30 |
live: Subscription, |
| 31 |
|
| 32 |
|
| 33 |
high_water: Option<MessageId>, |
| 34 |
} |
| 35 |
|
| 36 |
impl ChatStream { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pub async fn open<F, Fut>( |
| 54 |
hub: &Hub, |
| 55 |
room: RoomId, |
| 56 |
user: UserId, |
| 57 |
after: Option<MessageId>, |
| 58 |
fetch: F, |
| 59 |
) -> Result<Self, ChatError> |
| 60 |
where |
| 61 |
F: FnOnce(Option<MessageId>) -> Fut, |
| 62 |
Fut: Future<Output = Result<Vec<Message>, ChatError>>, |
| 63 |
{ |
| 64 |
|
| 65 |
|
| 66 |
let live = hub.subscribe(room, user)?; |
| 67 |
|
| 68 |
let backlog = fetch(after).await?; |
| 69 |
let high_water = backlog.iter().map(|m| m.id).max(); |
| 70 |
|
| 71 |
Ok(Self { |
| 72 |
backlog: backlog.into_iter(), |
| 73 |
live, |
| 74 |
high_water, |
| 75 |
}) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pub async fn next(&mut self) -> Option<ChatEvent> { |
| 82 |
if let Some(message) = self.backlog.next() { |
| 83 |
return Some(ChatEvent::Message(message)); |
| 84 |
} |
| 85 |
|
| 86 |
loop { |
| 87 |
let event = self.live.recv().await?; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if let ChatEvent::Message(m) = &event |
| 94 |
&& let Some(high) = self.high_water |
| 95 |
&& m.id <= high |
| 96 |
{ |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
return Some(event); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
pub fn room(&self) -> RoomId { |
| 106 |
self.live.room() |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(test)] |
| 111 |
mod tests { |
| 112 |
use super::*; |
| 113 |
use crate::hub::HubLimits; |
| 114 |
use crate::ids::Nonce; |
| 115 |
use std::sync::Arc; |
| 116 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 117 |
use uuid::Uuid; |
| 118 |
|
| 119 |
fn message(room: RoomId, id: i64) -> Message { |
| 120 |
Message { |
| 121 |
id: MessageId(id), |
| 122 |
room_id: room, |
| 123 |
author_id: UserId(Uuid::nil()), |
| 124 |
body_html: format!("m{id}"), |
| 125 |
created_at: 0, |
| 126 |
nonce: None, |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn ids(events: &[ChatEvent]) -> Vec<i64> { |
| 131 |
events |
| 132 |
.iter() |
| 133 |
.filter_map(|e| match e { |
| 134 |
ChatEvent::Message(m) => Some(m.id.0), |
| 135 |
_ => None, |
| 136 |
}) |
| 137 |
.collect() |
| 138 |
} |
| 139 |
|
| 140 |
async fn drain(stream: &mut ChatStream, n: usize) -> Vec<ChatEvent> { |
| 141 |
let mut out = Vec::new(); |
| 142 |
for _ in 0..n { |
| 143 |
match tokio::time::timeout(std::time::Duration::from_secs(5), stream.next()).await { |
| 144 |
Ok(Some(e)) => out.push(e), |
| 145 |
_ => break, |
| 146 |
} |
| 147 |
} |
| 148 |
out |
| 149 |
} |
| 150 |
|
| 151 |
#[tokio::test] |
| 152 |
async fn backlog_is_served_before_live() { |
| 153 |
let hub = Hub::new(HubLimits::default()); |
| 154 |
let room = RoomId(Uuid::new_v4()); |
| 155 |
|
| 156 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async { |
| 157 |
Ok(vec![message(room, 1), message(room, 2)]) |
| 158 |
}) |
| 159 |
.await |
| 160 |
.unwrap(); |
| 161 |
|
| 162 |
hub.publish(room, ChatEvent::Message(message(room, 3))); |
| 163 |
|
| 164 |
assert_eq!(ids(&drain(&mut stream, 3).await), vec![1, 2, 3]); |
| 165 |
} |
| 166 |
|
| 167 |
#[tokio::test] |
| 168 |
async fn a_message_published_during_the_fetch_is_not_lost() { |
| 169 |
|
| 170 |
|
| 171 |
let hub = Hub::new(HubLimits::default()); |
| 172 |
let room = RoomId(Uuid::new_v4()); |
| 173 |
let hub_for_fetch = hub.clone(); |
| 174 |
|
| 175 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async move { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
hub_for_fetch.publish(room, ChatEvent::Message(message(room, 7))); |
| 180 |
Ok(vec![message(room, 5), message(room, 6)]) |
| 181 |
}) |
| 182 |
.await |
| 183 |
.unwrap(); |
| 184 |
|
| 185 |
assert_eq!( |
| 186 |
ids(&drain(&mut stream, 3).await), |
| 187 |
vec![5, 6, 7], |
| 188 |
"the message sent during the fetch must still arrive" |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
#[tokio::test] |
| 193 |
async fn the_overlap_does_not_duplicate() { |
| 194 |
|
| 195 |
|
| 196 |
let hub = Hub::new(HubLimits::default()); |
| 197 |
let room = RoomId(Uuid::new_v4()); |
| 198 |
let hub_for_fetch = hub.clone(); |
| 199 |
|
| 200 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async move { |
| 201 |
hub_for_fetch.publish(room, ChatEvent::Message(message(room, 2))); |
| 202 |
hub_for_fetch.publish(room, ChatEvent::Message(message(room, 3))); |
| 203 |
|
| 204 |
Ok(vec![message(room, 1), message(room, 2), message(room, 3)]) |
| 205 |
}) |
| 206 |
.await |
| 207 |
.unwrap(); |
| 208 |
|
| 209 |
hub.publish(room, ChatEvent::Message(message(room, 4))); |
| 210 |
|
| 211 |
assert_eq!(ids(&drain(&mut stream, 4).await), vec![1, 2, 3, 4]); |
| 212 |
} |
| 213 |
|
| 214 |
#[tokio::test] |
| 215 |
async fn deletes_in_the_overlap_are_not_suppressed() { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
let hub = Hub::new(HubLimits::default()); |
| 220 |
let room = RoomId(Uuid::new_v4()); |
| 221 |
let hub_for_fetch = hub.clone(); |
| 222 |
|
| 223 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async move { |
| 224 |
hub_for_fetch.publish(room, ChatEvent::Delete { id: MessageId(1) }); |
| 225 |
Ok(vec![message(room, 1), message(room, 2)]) |
| 226 |
}) |
| 227 |
.await |
| 228 |
.unwrap(); |
| 229 |
|
| 230 |
let events = drain(&mut stream, 3).await; |
| 231 |
assert!( |
| 232 |
events |
| 233 |
.iter() |
| 234 |
.any(|e| matches!(e, ChatEvent::Delete { id: MessageId(1) })), |
| 235 |
"delete during the overlap must pass through: {events:?}" |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
#[tokio::test] |
| 240 |
async fn the_cursor_reaches_the_fetch() { |
| 241 |
let hub = Hub::new(HubLimits::default()); |
| 242 |
let room = RoomId(Uuid::new_v4()); |
| 243 |
let seen = Arc::new(AtomicUsize::new(0)); |
| 244 |
let seen_clone = seen.clone(); |
| 245 |
|
| 246 |
let _stream = ChatStream::open( |
| 247 |
&hub, |
| 248 |
room, |
| 249 |
UserId(Uuid::nil()), |
| 250 |
Some(MessageId(42)), |
| 251 |
move |after| async move { |
| 252 |
assert_eq!(after, Some(MessageId(42))); |
| 253 |
seen_clone.fetch_add(1, Ordering::SeqCst); |
| 254 |
Ok(vec![]) |
| 255 |
}, |
| 256 |
) |
| 257 |
.await |
| 258 |
.unwrap(); |
| 259 |
|
| 260 |
assert_eq!(seen.load(Ordering::SeqCst), 1); |
| 261 |
} |
| 262 |
|
| 263 |
#[tokio::test] |
| 264 |
async fn an_empty_backlog_goes_straight_to_live() { |
| 265 |
let hub = Hub::new(HubLimits::default()); |
| 266 |
let room = RoomId(Uuid::new_v4()); |
| 267 |
|
| 268 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async { |
| 269 |
Ok(vec![]) |
| 270 |
}) |
| 271 |
.await |
| 272 |
.unwrap(); |
| 273 |
|
| 274 |
hub.publish(room, ChatEvent::Message(message(room, 1))); |
| 275 |
assert_eq!(ids(&drain(&mut stream, 1).await), vec![1]); |
| 276 |
} |
| 277 |
|
| 278 |
#[tokio::test] |
| 279 |
async fn a_failing_fetch_releases_the_connection_slot() { |
| 280 |
let hub = Hub::new(HubLimits::default()); |
| 281 |
let room = RoomId(Uuid::new_v4()); |
| 282 |
|
| 283 |
let result = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async { |
| 284 |
Err(ChatError::MessageEmpty) |
| 285 |
}) |
| 286 |
.await; |
| 287 |
|
| 288 |
assert!(result.is_err()); |
| 289 |
assert_eq!( |
| 290 |
hub.connection_count(), |
| 291 |
0, |
| 292 |
"the subscription taken before the fetch must be released when it fails" |
| 293 |
); |
| 294 |
assert_eq!(hub.room_count(), 0); |
| 295 |
} |
| 296 |
|
| 297 |
#[tokio::test] |
| 298 |
async fn the_nonce_survives_the_seam() { |
| 299 |
|
| 300 |
|
| 301 |
let hub = Hub::new(HubLimits::default()); |
| 302 |
let room = RoomId(Uuid::new_v4()); |
| 303 |
|
| 304 |
let mut stream = ChatStream::open(&hub, room, UserId(Uuid::nil()), None, |_| async { |
| 305 |
Ok(vec![]) |
| 306 |
}) |
| 307 |
.await |
| 308 |
.unwrap(); |
| 309 |
|
| 310 |
let mut m = message(room, 1); |
| 311 |
m.nonce = Nonce::parse("abc"); |
| 312 |
hub.publish(room, ChatEvent::Message(m)); |
| 313 |
|
| 314 |
match drain(&mut stream, 1).await.first() { |
| 315 |
Some(ChatEvent::Message(m)) => assert_eq!(m.nonce, Nonce::parse("abc")), |
| 316 |
other => panic!("expected a message carrying its nonce, got {other:?}"), |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|