|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use spawned_rt::tasks::{self as rt, BroadcastStream, ReceiverStream}; |
| 4 | + |
| 5 | +use crate::tasks::{ |
| 6 | + stream::spawn_listener, CallResponse, CastResponse, GenServer, GenServerHandle, |
| 7 | +}; |
| 8 | + |
| 9 | +type SummatoryHandle = GenServerHandle<Summatory>; |
| 10 | + |
| 11 | +struct Summatory; |
| 12 | + |
| 13 | +type SummatoryState = u16; |
| 14 | + |
| 15 | +#[derive(Clone)] |
| 16 | +struct UpdateSumatory { |
| 17 | + added_value: u16, |
| 18 | +} |
| 19 | + |
| 20 | +impl Summatory { |
| 21 | + pub async fn get_value(server: &mut SummatoryHandle) -> Result<SummatoryState, ()> { |
| 22 | + server.call(()).await.map_err(|_| ()) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl GenServer for Summatory { |
| 27 | + type CallMsg = (); // We only handle one type of call, so there is no need for a specific message type. |
| 28 | + type CastMsg = UpdateSumatory; |
| 29 | + type OutMsg = SummatoryState; |
| 30 | + type State = SummatoryState; |
| 31 | + type Error = (); |
| 32 | + |
| 33 | + fn new() -> Self { |
| 34 | + Self |
| 35 | + } |
| 36 | + |
| 37 | + async fn handle_cast( |
| 38 | + &mut self, |
| 39 | + message: Self::CastMsg, |
| 40 | + _handle: &GenServerHandle<Self>, |
| 41 | + state: Self::State, |
| 42 | + ) -> CastResponse<Self> { |
| 43 | + let new_state = state + message.added_value; |
| 44 | + CastResponse::NoReply(new_state) |
| 45 | + } |
| 46 | + |
| 47 | + async fn handle_call( |
| 48 | + &mut self, |
| 49 | + _message: Self::CallMsg, |
| 50 | + _handle: &SummatoryHandle, |
| 51 | + state: Self::State, |
| 52 | + ) -> CallResponse<Self> { |
| 53 | + let current_value = state; |
| 54 | + CallResponse::Reply(state, current_value) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// In this example, the stream sends u8 values, which are converted to the type |
| 59 | +// supported by the GenServer (UpdateSumatory / u16). |
| 60 | +fn message_builder(value: u8) -> UpdateSumatory { |
| 61 | + UpdateSumatory { |
| 62 | + added_value: value as u16, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +pub fn test_sum_numbers_from_stream() { |
| 68 | + let runtime = rt::Runtime::new().unwrap(); |
| 69 | + runtime.block_on(async move { |
| 70 | + let mut summatory_handle = Summatory::start(0); |
| 71 | + let stream = tokio_stream::iter(vec![1u8, 2, 3, 4, 5].into_iter().map(Ok::<u8, ()>)); |
| 72 | + |
| 73 | + spawn_listener(summatory_handle.clone(), message_builder, stream); |
| 74 | + |
| 75 | + // Wait for 1 second so the whole stream is processed |
| 76 | + rt::sleep(Duration::from_secs(1)).await; |
| 77 | + |
| 78 | + let val = Summatory::get_value(&mut summatory_handle).await.unwrap(); |
| 79 | + assert_eq!(val, 15); |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +pub fn test_sum_numbers_from_channel() { |
| 85 | + let runtime = rt::Runtime::new().unwrap(); |
| 86 | + runtime.block_on(async move { |
| 87 | + let mut summatory_handle = Summatory::start(0); |
| 88 | + let (tx, rx) = spawned_rt::tasks::mpsc::channel::<Result<u8, ()>>(); |
| 89 | + |
| 90 | + // Spawn a task to send numbers to the channel |
| 91 | + spawned_rt::tasks::spawn(async move { |
| 92 | + for i in 1..=5 { |
| 93 | + tx.send(Ok(i)).unwrap(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + spawn_listener( |
| 98 | + summatory_handle.clone(), |
| 99 | + message_builder, |
| 100 | + ReceiverStream::new(rx), |
| 101 | + ); |
| 102 | + |
| 103 | + // Wait for 1 second so the whole stream is processed |
| 104 | + rt::sleep(Duration::from_secs(1)).await; |
| 105 | + |
| 106 | + let val = Summatory::get_value(&mut summatory_handle).await.unwrap(); |
| 107 | + assert_eq!(val, 15); |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +pub fn test_sum_numbers_from_broadcast_channel() { |
| 113 | + let runtime = rt::Runtime::new().unwrap(); |
| 114 | + runtime.block_on(async move { |
| 115 | + let mut summatory_handle = Summatory::start(0); |
| 116 | + let (tx, rx) = tokio::sync::broadcast::channel::<u8>(5); |
| 117 | + |
| 118 | + // Spawn a task to send numbers to the channel |
| 119 | + spawned_rt::tasks::spawn(async move { |
| 120 | + for i in 1u8..=5 { |
| 121 | + tx.send(i).unwrap(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + spawn_listener( |
| 126 | + summatory_handle.clone(), |
| 127 | + message_builder, |
| 128 | + BroadcastStream::new(rx), |
| 129 | + ); |
| 130 | + |
| 131 | + // Wait for 1 second so the whole stream is processed |
| 132 | + rt::sleep(Duration::from_secs(1)).await; |
| 133 | + |
| 134 | + let val = Summatory::get_value(&mut summatory_handle).await.unwrap(); |
| 135 | + assert_eq!(val, 15); |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +#[test] |
| 140 | +pub fn test_stream_cancellation() { |
| 141 | + const RUNNING_TIME: u64 = 1000; |
| 142 | + |
| 143 | + let runtime = rt::Runtime::new().unwrap(); |
| 144 | + runtime.block_on(async move { |
| 145 | + let mut summatory_handle = Summatory::start(0); |
| 146 | + let (tx, rx) = spawned_rt::tasks::mpsc::channel::<Result<u8, ()>>(); |
| 147 | + |
| 148 | + // Spawn a task to send numbers to the channel |
| 149 | + spawned_rt::tasks::spawn(async move { |
| 150 | + for i in 1..=5 { |
| 151 | + tx.send(Ok(i)).unwrap(); |
| 152 | + rt::sleep(Duration::from_millis(RUNNING_TIME / 4)).await; |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + let (_handle, cancellation_token) = spawn_listener( |
| 157 | + summatory_handle.clone(), |
| 158 | + message_builder, |
| 159 | + ReceiverStream::new(rx), |
| 160 | + ); |
| 161 | + |
| 162 | + // Wait for 1 second so the whole stream is processed |
| 163 | + rt::sleep(Duration::from_millis(RUNNING_TIME)).await; |
| 164 | + |
| 165 | + cancellation_token.cancel(); |
| 166 | + |
| 167 | + // The reasoning for this assertion is that each message takes a quarter of the total time |
| 168 | + // to be processed, so having a stream of 5 messages, the last one won't be processed. |
| 169 | + // We could safely assume that it will get to process 4 messages, but in case of any extenal |
| 170 | + // slowdown, it could process less. |
| 171 | + let val = Summatory::get_value(&mut summatory_handle).await.unwrap(); |
| 172 | + assert!(val > 0 && val < 15); |
| 173 | + }) |
| 174 | +} |
0 commit comments