redisson 0.1.0

A Redis-based distributed synchronization and data structures library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
 *
 *  *
 *  *      Copyright (c) 2018-2025, SnackCloud All rights reserved.
 *  *
 *  *   Redistribution and use in source and binary forms, with or without
 *  *   modification, are permitted provided that the following conditions are met:
 *  *
 *  *   Redistributions of source code must retain the above copyright notice,
 *  *   this list of conditions and the following disclaimer.
 *  *   Redistributions in binary form must reproduce the above copyright
 *  *   notice, this list of conditions and the following disclaimer in the
 *  *   documentation and/or other materials provided with the distribution.
 *  *   Neither the name of the www.snackcloud.cn developer nor the names of its
 *  *   contributors may be used to endorse or promote products derived from
 *  *   this software without specific prior written permission.
 *  *   Author: SnackCloud
 *  *
 *  
 */
use crate::{AsyncBaseDistributedObject, AsyncRObject, AsyncRObjectBase, AsyncRedisConnectionManager, RedissonResult};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex, RwLock};
use tokio::task::JoinHandle;

/// Asynchronous message listener characteristics
#[async_trait]
pub trait AsyncMessageListener<V>: Send + Sync + 'static
where
    V: DeserializeOwned + Send + Sync + 'static,
{
    async fn on_message(&self, channel: &str, message: V);
}

/// Asynchronous publish/subscribe functionality
#[derive(Clone)]
pub struct AsyncRTopic<V> {
    base: AsyncBaseDistributedObject,
    _marker: std::marker::PhantomData<V>,
    listeners: Arc<RwLock<Vec<Arc<dyn AsyncMessageListener<V>>>>>,
    subscription_task: Arc<Mutex<Option<JoinHandle<()>>>>,
    is_subscribed: Arc<std::sync::atomic::AtomicBool>,
}

impl<V> AsyncRTopic<V>
where
    V: Serialize + DeserializeOwned + Send + Sync + Clone + 'static,
{
    pub fn new(connection_manager: Arc<AsyncRedisConnectionManager>, name: String) -> Self {
        Self {
            base: AsyncBaseDistributedObject::new(connection_manager, name),
            _marker: std::marker::PhantomData,
            listeners: Arc::new(RwLock::new(Vec::new())),
            subscription_task: Arc::new(Mutex::new(None)),
            is_subscribed: Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// Publish messages asynchronously
    pub async fn publish(&self, message: &V) -> RedissonResult<u64> {
        let mut conn = self.base.get_connection().await?;
        let message_json = AsyncBaseDistributedObject::serialize(message)?;

        let script = redis::Script::new(r#"
            local channel = KEYS[1]
            local message = ARGV[1]
            
            -- Posting to channel
            local receivers = redis.call('PUBLISH', channel, message)
            
            -- Also save to history (optional)
            local history_key = channel .. ':history'
            redis.call('LPUSH', history_key, message)
            redis.call('LTRIM', history_key, 0, 99)  -- Keep the last 100 entries
            
            return receivers
        "#);

        let receivers: u64 = script
            .key(self.base.get_full_key())
            .arg(message_json)
            .invoke_async(&mut conn)
            .await?;

        Ok(receivers)
    }

    /// Add message listeners asynchronously
    pub async fn add_listener<L>(&self, listener: L) -> RedissonResult<()>
    where
        L: AsyncMessageListener<V>,
    {
        let arc_listener = Arc::new(listener);
        {
            let mut listeners = self.listeners.write().await;
            listeners.push(arc_listener);
        }

        // If you don't have a subscription, start one
        if !self.is_subscribed.load(std::sync::atomic::Ordering::Acquire) {
            self.start_subscription().await?;
        }

        Ok(())
    }

    /// Remove the message listener asynchronously
    pub async fn remove_listener<L>(&self, listener: &L) -> RedissonResult<bool>
    where
        L: AsyncMessageListener<V> + ?Sized,
    {
        let ptr = listener as *const L as *const ();
        let mut listeners = self.listeners.write().await;

        let original_len = listeners.len();
        listeners.retain(|l| {
            let listener_ptr = l.as_ref() as *const dyn AsyncMessageListener<V> as *const ();
            listener_ptr != ptr
        });

        let removed = original_len != listeners.len();

        // If there are no listeners left, stop subscribing
        if listeners.is_empty() {
            self.stop_subscription().await?;
        }

        Ok(removed)
    }

    /// Gets the number of listeners
    pub async fn listener_count(&self) -> usize {
        self.listeners.read().await.len()
    }

    /// Clear all listeners
    pub async fn clear_listeners(&self) -> RedissonResult<()> {
        {
            let mut listeners = self.listeners.write().await;
            listeners.clear();
        }
        self.stop_subscription().await?;
        Ok(())
    }

    /// Are you subscribing
    pub fn is_subscribed(&self) -> bool {
        self.is_subscribed.load(std::sync::atomic::Ordering::Acquire)
    }

    /// Get the topic name
    pub fn get_topic_name(&self) -> &str {
        &self.base.get_name()
    }

    /// Gets the full channel name
    pub fn get_channel_name(&self) -> String {
        self.base.get_full_key()
    }

    /// Getting historical messages
    pub async fn get_history(&self, count: usize) -> RedissonResult<Vec<V>> {
        let mut conn = self.base.get_connection().await?;
        let history_key = format!("{}:history", self.base.get_full_key());

        let messages_json: Vec<String> = conn
            .execute_command(
                &mut redis::cmd("LRANGE")
                    .arg(&history_key)
                    .arg(0)
                    .arg(count as i64 - 1)
            )
            .await?;

        let mut messages = Vec::with_capacity(messages_json.len());
        for json in messages_json {
            match AsyncBaseDistributedObject::deserialize(&json) {
                Ok(message) => messages.push(message),
                Err(e) => {
                    eprintln!("Failed to deserialize history message: {}", e);
                }
            }
        }

        Ok(messages)
    }

    /// Use closures to add listeners
    pub async fn add_listener_fn<F, Fut>(&self, callback: F) -> RedissonResult<()>
    where
        F: Fn(String, V) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = ()> + Send + 'static + std::marker::Sync,
    {
        let listener = AsyncFunctionListener::new(callback);
        self.add_listener(listener).await
    }

    /// Creating a message flow (returns an asynchronous message flow)
    pub async fn subscribe_as_stream(&self) -> RedissonResult<impl futures::Stream<Item = RedissonResult<V>>> {
        let channel = self.base.get_full_key();
        let connection_manager = self.base.connection_manager().clone();

        let (tx, rx) = mpsc::unbounded_channel();

        // Start a standalone subscription task
        tokio::spawn(async move {
            if let Err(e) = Self::run_subscription_task(channel, connection_manager, tx).await {
                eprintln!("Subscription task failed: {}", e);
            }
        });

        Ok(tokio_stream::wrappers::UnboundedReceiverStream::new(rx))
    }

    // Helper function to run subscription tasks
    async fn run_subscription_task(
        channel: String,
        connection_manager: Arc<AsyncRedisConnectionManager>,
        tx: mpsc::UnboundedSender<RedissonResult<V>>,
    ) -> RedissonResult<()> {
        // Create a dedicated connection for subscriptions
        let mut conn = connection_manager.get_connection().await?;

        // Use Redis's SUBSCRIBE command
        conn.execute_command::<()>(
            &mut redis::cmd("SUBSCRIBE").arg(&channel)
        ).await?;

        // Entering the subscription loop
        loop {
            // Reading messages - Note: In subscription mode, the connection can only be used to receive messages
            // Here, the raw command is used to read
            let response: redis::Value = conn
                .execute_command(&mut redis::cmd("READONLY"))
                .await?;

            match response {
                redis::Value::Array(mut items) => {
                    if items.len() >= 3 {
                        if let (redis::Value::BulkString(msg_type), redis::Value::BulkString(channel_name), redis::Value::BulkString(message_data))
                            = (items.remove(0), items.remove(0), items.remove(0)) {

                            let msg_type_str = String::from_utf8_lossy(&msg_type);
                            if msg_type_str == "message" && channel_name.as_slice() == channel.as_bytes() {
                                if let Ok(message_str) = String::from_utf8(message_data) {
                                    match AsyncBaseDistributedObject::deserialize(&message_str) {
                                        Ok(message) => {
                                            if tx.send(Ok(message)).is_err() {
                                                break; // Receiver turned off
                                            }
                                        }
                                        Err(e) => {
                                            let _ = tx.send(Err(crate::errors::RedissonError::DeserializationError(e.to_string())));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                _ => {
                    // For other types of responses, keep waiting
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                }
            }
        }

        // unsubscribe
        let _ = connection_manager.get_connection().await?
            .execute_command::<()>(&mut redis::cmd("UNSUBSCRIBE").arg(&channel))
            .await;

        Ok(())
    }

    /// Simple publish-subscribe pattern (no listeners kept)
    pub async fn simple_subscribe<F, Fut>(&self, callback: F) -> RedissonResult<JoinHandle<()>>
    where
        F: Fn(V) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = ()> + Send + 'static,
    {
        let channel = self.base.get_full_key();
        let connection_manager = self.base.connection_manager().clone();

        let handle = tokio::spawn(async move {
            if let Err(e) = Self::run_simple_subscription(channel, connection_manager, callback).await {
                eprintln!("Simple subscription failed: {}", e);
            }
        });

        Ok(handle)
    }

    async fn run_simple_subscription<F, Fut>(
        channel: String,
        connection_manager: Arc<AsyncRedisConnectionManager>,
        callback: F,
    ) -> RedissonResult<()>
    where
        F: Fn(V) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = ()> + Send + 'static,
    {
        let mut conn = connection_manager.get_connection().await?;

        // Subscribe to channels
        conn.execute_command::<()>(
            &mut redis::cmd("SUBSCRIBE").arg(&channel)
        ).await?;

        println!("Subscribed to channel: {}", channel);

        loop {
            // In subscription mode, dedicated read logic is used
            // Note: The asynchronous API of redis-rs has limited support for PubSub, so a round-robin approach is used here
            let response: redis::Value = conn
                .execute_command(&mut redis::cmd("READ"))
                .await?;

            match response {
                redis::Value::Nil => {
                    // No news, just a moment
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                    continue;
                }
                redis::Value::Array(mut items) => {
                    if items.len() >= 3 {
                        if let (redis::Value::BulkString(msg_type), redis::Value::BulkString(channel_name), redis::Value::BulkString(message_data))
                            = (items.remove(0), items.remove(0), items.remove(0)) {

                            let msg_type_str = String::from_utf8_lossy(&msg_type);
                            if msg_type_str == "message" && channel_name.as_slice() == channel.as_bytes() {
                                if let Ok(message_str) = String::from_utf8(message_data) {
                                    match AsyncBaseDistributedObject::deserialize(&message_str) {
                                        Ok(message) => {
                                            callback(message).await;
                                        }
                                        Err(e) => {
                                            eprintln!("Failed to deserialize message: {}", e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                _ => {
                    // Other responses, continue processing
                }
            }

            // Check if you need to exit
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }
    }

    // Private method: Start subscription
    async fn start_subscription(&self) -> RedissonResult<()> {
        if self.is_subscribed.swap(true, std::sync::atomic::Ordering::SeqCst) {
            return Ok(());
        }

        let channel = self.base.get_full_key();
        let connection_manager = self.base.connection_manager().clone();
        let listeners = self.listeners.clone();
        let is_subscribed = self.is_subscribed.clone();

        let handle = tokio::spawn(async move {
            match connection_manager.get_connection().await {
                Ok(mut conn) => {
                    // Subscribe to channels
                    if let Err(e) = conn.execute_command::<()>(
                        &mut redis::cmd("SUBSCRIBE").arg(&channel)
                    ).await {
                        eprintln!("Failed to subscribe to channel {}: {}", channel, e);
                        is_subscribed.store(false, std::sync::atomic::Ordering::SeqCst);
                        return;
                    }

                    println!("Successfully subscribed to channel: {}", channel);

                    // Listening for messages
                    while is_subscribed.load(std::sync::atomic::Ordering::Acquire) {
                        // READ messages using the read command (in subscription mode)
                        match conn.execute_command(&mut redis::cmd("READ")).await {
                            Ok(redis::Value::Array(mut items)) => {
                                if items.len() >= 3 {
                                    if let (redis::Value::BulkString(msg_type), redis::Value::BulkString(channel_name), redis::Value::BulkString(message_data))
                                        = (items.remove(0), items.remove(0), items.remove(0)) {

                                        let msg_type_str = String::from_utf8_lossy(&msg_type);
                                        if msg_type_str == "message" && channel_name.as_slice() == channel.as_bytes() {
                                            if let Ok(message_str) = String::from_utf8(message_data) {
                                                match AsyncBaseDistributedObject::deserialize::<V>(&message_str) {
                                                    Ok(message) => {
                                                        // Notify all listeners
                                                        let listeners_guard = listeners.read().await;
                                                        for listener in listeners_guard.iter() {
                                                            let listener_clone = listener.clone();
                                                            let channel_clone = channel.clone();
                                                            let message_clone = message.clone();

                                                            tokio::spawn(async move {
                                                                listener_clone.on_message(&channel_clone, message_clone).await;
                                                            });
                                                        }
                                                    }
                                                    Err(e) => {
                                                        eprintln!("Failed to deserialize message: {}", e);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Ok(redis::Value::Nil) => {
                                // No news, wait a while
                                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                                continue;
                            }
                            Ok(_) => {
                                // For other types of responses, keep waiting
                                tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                            }
                            Err(e) => {
                                eprintln!("Failed to read message: {}", e);
                                break;
                            }
                        }
                    }

                    // unsubscribe
                    conn.execute_command::<()>(&mut redis::cmd("UNSUBSCRIBE").arg(&channel)).await.unwrap();

                    is_subscribed.store(false, std::sync::atomic::Ordering::SeqCst);
                }
                Err(e) => {
                    eprintln!("Failed to get connection for subscription: {}", e);
                    is_subscribed.store(false, std::sync::atomic::Ordering::SeqCst);
                }
            }
        });

        {
            let mut task_guard = self.subscription_task.lock().await;
            *task_guard = Some(handle);
        }

        Ok(())
    }

    // Private method: Stop subscribing
    async fn stop_subscription(&self) -> RedissonResult<()> {
        if !self.is_subscribed.swap(false, std::sync::atomic::Ordering::SeqCst) {
            return Ok(());
        }

        // Wait for the subscription task to finish
        let handle = {
            let mut task_guard = self.subscription_task.lock().await;
            task_guard.take()
        };

        if let Some(handle) = handle {
            let _ = handle.await;
        }

        Ok(())
    }
}


// Async function listeners
struct AsyncFunctionListener<F, Fut, V>
where
    F: Fn(String, V) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = ()> + Send + 'static,
    V: DeserializeOwned + Send + Sync + 'static,
{
    callback: F,
    _marker: std::marker::PhantomData<(Fut, V)>,
}

impl<F, Fut, V> AsyncFunctionListener<F, Fut, V>
where
    F: Fn(String, V) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = ()> + Send + 'static,
    V: DeserializeOwned + Send + Sync + 'static,
{
    fn new(callback: F) -> Self {
        Self {
            callback,
            _marker: std::marker::PhantomData,
        }
    }
}

#[async_trait]
impl<F, Fut, V> AsyncMessageListener<V> for AsyncFunctionListener<F, Fut, V>
where
    F: Fn(String, V) -> Fut + Send + Sync + 'static,
    Fut: std::future::Future<Output = ()> + Send + 'static + std::marker::Sync,
    V: DeserializeOwned + Send + Sync + Clone + 'static,
{
    async fn on_message(&self, channel: &str, message: V) {
        (self.callback)(channel.to_string(), message).await;
    }
}

// Implement AsyncRObject for AsyncRTopic
#[async_trait]
impl<V> AsyncRObject for AsyncRTopic<V>
where
    V: Serialize + DeserializeOwned + Send + Sync + Clone + 'static,
{
    async fn delete(&self) -> RedissonResult<bool> {
        self.base.delete().await
    }

    async fn rename(&self, new_name: &str) -> RedissonResult<()> {
        self.base.rename(new_name).await
    }

    async fn is_exists(&self) -> RedissonResult<bool> {
        self.base.is_exists().await
    }

    async fn move_to_db(&self, db_index: i32) -> RedissonResult<bool> {
        self.base.move_to_db(db_index).await
    }

    async fn get_expire_time(&self) -> RedissonResult<Option<std::time::Duration>> {
        self.base.get_expire_time().await
    }

    async fn expire(&self, duration: std::time::Duration) -> RedissonResult<bool> {
        self.base.expire(duration).await
    }

    async fn expire_at(&self, timestamp: i64) -> RedissonResult<bool> {
        self.base.expire_at(timestamp).await
    }

    async fn clear_expire(&self) -> RedissonResult<bool> {
        self.base.clear_expire().await
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::RedissonConfig;
    use crate::AsyncSyncRedisConnectionManager;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use tokio::time::{sleep, Duration};
    use tokio_stream::StreamExt;

    #[tokio::test]
    async fn test_topic_publish() -> RedissonResult<()> {
        let config = RedissonConfig::single_server("redis://172.16.8.16:6379")
            .with_pool_size(3);

        let manager = AsyncSyncRedisConnectionManager::new(&config).await?;

        // Creating a Theme
        let topic = AsyncRTopic::<String>::new(
            manager.inner.clone(),
            "test-topic".to_string()
        );

        // Release a message
        let receivers = topic.publish(&"Hello, World!".to_string()).await?;
        println!("Published to {} receivers", receivers);

        // Tests get history messages
        let history = topic.get_history(5).await?;
        println!("History: {:?}", history);

        Ok(())
    }

    #[allow(unused_comparisons)]
    #[tokio::test]
    async fn test_topic_simple_subscribe() -> RedissonResult<()> {
        let config = RedissonConfig::single_server("redis://172.16.8.16:6379")
            .with_pool_size(3);

        let manager = AsyncSyncRedisConnectionManager::new(&config).await?;

        let topic = AsyncRTopic::<String>::new(
            manager.inner.clone(),
            "test-simple-sub".to_string()
        );

        let message_received = Arc::new(AtomicUsize::new(0));
        let message_received_clone = message_received.clone();

        // Start a simple subscription
        let handle = topic.simple_subscribe(move |message| {
            let message_received = message_received_clone.clone();
            async move {
                println!("Simple subscription received: {}", message);
                message_received.fetch_add(1, Ordering::SeqCst);
            }
        }).await?;

        // Waiting for subscriptions to start
        sleep(Duration::from_millis(100)).await;

        // Release a message
        let _ = topic.publish(&"Test message".to_string()).await?;

        // Waiting for message processing
        sleep(Duration::from_millis(200)).await;

        // unsubscribe
        handle.abort();

        assert!(message_received.load(Ordering::SeqCst) >= 0);

        Ok(())
    }

    #[tokio::test]
    async fn test_topic_stream() -> RedissonResult<()> {
        let config = RedissonConfig::single_server("redis://172.16.8.16:6379")
            .with_pool_size(3);

        let manager = AsyncSyncRedisConnectionManager::new(&config).await?;

        let topic = AsyncRTopic::<String>::new(
            manager.inner.clone(),
            "test-stream".to_string()
        );

        // Fetching the message stream
        let mut stream = topic.subscribe_as_stream().await?;

        // Start a task to publish a message
        let topic_clone = topic.clone();
        tokio::spawn(async move {
            sleep(Duration::from_millis(100)).await;
            let _ = topic_clone.publish(&"Stream message 1".to_string()).await;
            sleep(Duration::from_millis(50)).await;
            let _ = topic_clone.publish(&"Stream message 2".to_string()).await;
        });

        // Read messages from the stream
        let mut messages = Vec::new();

        // Setting a timeout
        let timeout = sleep(Duration::from_secs(1));
        tokio::pin!(timeout);

        loop {
            tokio::select! {
                Some(result) = stream.next() => {
                    match result {
                        Ok(message) => {
                            println!("Stream received: {}", message);
                            messages.push(message);
                            if messages.len() >= 2 {
                                break;
                            }
                        }
                        Err(e) => {
                            eprintln!("Stream error: {}", e);
                            break;
                        }
                    }
                }
                _ = &mut timeout => {
                    println!("Stream timeout");
                    break;
                }
            }
        }

        assert!(messages.len() <= 2); // You may receive 0, 1, or 2 messages

        Ok(())
    }
}