@@ -6,7 +6,7 @@ use util::*;
6
6
#[ cfg( feature = "test" ) ]
7
7
#[ cfg_attr( feature = "runtime-tokio" , tokio:: test) ]
8
8
#[ cfg_attr( feature = "runtime-async-std" , async_std:: test) ]
9
- async fn main ( ) -> anyhow:: Result < ( ) > {
9
+ async fn realtime_1 ( ) -> anyhow:: Result < ( ) > {
10
10
use sea_streamer_redis:: {
11
11
AutoStreamReset , RedisConnectOptions , RedisConsumerOptions , RedisStreamer ,
12
12
} ;
@@ -16,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
16
16
} ;
17
17
use std:: time:: Duration ;
18
18
19
- const TEST : & str = "realtime " ;
19
+ const TEST : & str = "realtime_1 " ;
20
20
env_logger:: init ( ) ;
21
21
test ( false ) . await ?;
22
22
test ( true ) . await ?;
@@ -134,3 +134,120 @@ async fn main() -> anyhow::Result<()> {
134
134
135
135
Ok ( ( ) )
136
136
}
137
+
138
+ #[ cfg( feature = "test" ) ]
139
+ #[ cfg_attr( feature = "runtime-tokio" , tokio:: test) ]
140
+ #[ cfg_attr( feature = "runtime-async-std" , async_std:: test) ]
141
+ async fn realtime_2 ( ) -> anyhow:: Result < ( ) > {
142
+ use sea_streamer_redis:: {
143
+ AutoStreamReset , RedisConnectOptions , RedisConsumerOptions , RedisResult , RedisStreamer ,
144
+ } ;
145
+ use sea_streamer_runtime:: sleep;
146
+ use sea_streamer_types:: {
147
+ export:: futures:: { Stream , StreamExt } ,
148
+ Buffer , ConsumerMode , ConsumerOptions , Message , Producer , ShardId , SharedMessage ,
149
+ StreamKey , Streamer , Timestamp ,
150
+ } ;
151
+ use std:: time:: Duration ;
152
+
153
+ const TEST : & str = "realtime_2" ;
154
+ env_logger:: init ( ) ;
155
+ test ( false ) . await ?;
156
+
157
+ async fn test ( enable_cluster : bool ) -> anyhow:: Result < ( ) > {
158
+ println ! ( "Enable Cluster = {enable_cluster} ..." ) ;
159
+
160
+ let mut options = RedisConnectOptions :: default ( ) ;
161
+ options. set_enable_cluster ( enable_cluster) ;
162
+ let streamer = RedisStreamer :: connect (
163
+ std:: env:: var ( "BROKERS_URL" )
164
+ . unwrap_or_else ( |_| "redis://localhost" . to_owned ( ) )
165
+ . parse ( )
166
+ . unwrap ( ) ,
167
+ options,
168
+ )
169
+ . await ?;
170
+ println ! ( "Connect Streamer ... ok" ) ;
171
+
172
+ let now = Timestamp :: now_utc ( ) ;
173
+ let stream_key = StreamKey :: new ( format ! (
174
+ "{}-{}a" ,
175
+ TEST ,
176
+ now. unix_timestamp_nanos( ) / 1_000_000
177
+ ) ) ?;
178
+ let zero = ShardId :: new ( 0 ) ;
179
+
180
+ let mut producer = streamer. create_generic_producer ( Default :: default ( ) ) . await ?;
181
+
182
+ println ! ( "Producing 0..5 ..." ) ;
183
+ let mut sequence = 0 ;
184
+ for i in 0 ..5 {
185
+ let message = format ! ( "{i}" ) ;
186
+ let receipt = producer. send_to ( & stream_key, message) ?. await ?;
187
+ assert_eq ! ( receipt. stream_key( ) , & stream_key) ;
188
+ // should always increase
189
+ assert ! ( receipt. sequence( ) > & sequence) ;
190
+ sequence = * receipt. sequence ( ) ;
191
+ assert_eq ! ( receipt. shard_id( ) , & zero) ;
192
+ }
193
+
194
+ let mut options = RedisConsumerOptions :: new ( ConsumerMode :: RealTime ) ;
195
+ options. set_auto_stream_reset ( AutoStreamReset :: Latest ) ;
196
+
197
+ let mut half = streamer
198
+ . create_consumer ( & [ stream_key. clone ( ) ] , options. clone ( ) )
199
+ . await ?
200
+ . into_stream ( ) ;
201
+
202
+ // Why do we have to wait? We want consumer to have started reading
203
+ // before producing any messages. While after `create` returns the consumer
204
+ // is ready (connection opened), there is still a small delay to send an `XREAD`
205
+ // operation to the server.
206
+ sleep ( Duration :: from_millis ( 5 ) ) . await ;
207
+
208
+ println ! ( "Producing 5..10 ..." ) ;
209
+ for i in 5 ..10 {
210
+ let message = format ! ( "{i}" ) ;
211
+ producer. send_to ( & stream_key, message) ?;
212
+ }
213
+
214
+ println ! ( "Flush producer ..." ) ;
215
+ producer. flush ( ) . await ?;
216
+
217
+ options. set_auto_stream_reset ( AutoStreamReset :: Earliest ) ;
218
+ let mut full = streamer
219
+ . create_consumer ( & [ stream_key. clone ( ) ] , options)
220
+ . await ?
221
+ . into_stream ( ) ;
222
+
223
+ let seq = stream_n ( & mut half, 5 ) . await ?;
224
+ assert_eq ! ( seq, [ 5 , 6 , 7 , 8 , 9 ] ) ;
225
+ println ! ( "Stream latest ... ok" ) ;
226
+
227
+ let seq = stream_n ( & mut full, 10 ) . await ?;
228
+ assert_eq ! ( seq, [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) ;
229
+ println ! ( "Stream all ... ok" ) ;
230
+
231
+ println ! ( "End test case." ) ;
232
+ Ok ( ( ) )
233
+ }
234
+
235
+ async fn stream_n < S : Stream < Item = RedisResult < SharedMessage > > + std:: marker:: Unpin > (
236
+ stream : & mut S ,
237
+ num : usize ,
238
+ ) -> anyhow:: Result < Vec < usize > > {
239
+ let mut numbers = Vec :: new ( ) ;
240
+ for _ in 0 ..num {
241
+ match stream. next ( ) . await {
242
+ Some ( mess) => {
243
+ let mess = mess?;
244
+ numbers. push ( mess. message ( ) . as_str ( ) . unwrap ( ) . parse :: < usize > ( ) . unwrap ( ) ) ;
245
+ }
246
+ None => panic ! ( "Stream ended?" ) ,
247
+ }
248
+ }
249
+ Ok ( numbers)
250
+ }
251
+
252
+ Ok ( ( ) )
253
+ }
0 commit comments