@@ -163,3 +163,124 @@ impl<S: StateStore> Executor for NowExecutor<S> {
163163 self . identity . as_str ( )
164164 }
165165}
166+
167+ #[ cfg( test) ]
168+ mod tests {
169+ use std:: str:: FromStr ;
170+
171+ use chrono:: NaiveDateTime ;
172+ use futures:: StreamExt ;
173+ use risingwave_common:: array:: StreamChunk ;
174+ use risingwave_common:: catalog:: { ColumnDesc , ColumnId , TableId } ;
175+ use risingwave_common:: test_prelude:: StreamChunkTestExt ;
176+ use risingwave_common:: types:: { DataType , NaiveDateTimeWrapper , ScalarImpl } ;
177+ use risingwave_common:: util:: sort_util:: OrderType ;
178+ use risingwave_storage:: memory:: MemoryStateStore ;
179+ use risingwave_storage:: table:: streaming_table:: state_table:: StateTable ;
180+ use tokio:: sync:: mpsc:: { unbounded_channel, UnboundedSender } ;
181+
182+ use super :: NowExecutor ;
183+ use crate :: executor:: { Barrier , BoxedMessageStream , Executor , Message , PkIndices , Watermark } ;
184+
185+ #[ tokio:: test]
186+ async fn test_now ( ) {
187+ let state_table = create_state_table ( ) . await ;
188+ let ( tx, mut now_executor) = create_executor ( state_table) ;
189+
190+ // Init barrier
191+ tx. send ( Barrier :: new_test_barrier ( 1 ) ) . unwrap ( ) ;
192+
193+ // Consume the barrier
194+ now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
195+
196+ tx. send ( Barrier :: new_test_barrier_with_prev_epoch ( 1 << 16 , 1 ) )
197+ . unwrap ( ) ;
198+
199+ // Consume the data chunk
200+ let chunk_msg = now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
201+
202+ assert_eq ! (
203+ chunk_msg. into_chunk( ) . unwrap( ) . compact( ) ,
204+ StreamChunk :: from_pretty(
205+ " TS
206+ + 2021-04-01T00:00:00.001"
207+ )
208+ ) ;
209+
210+ // Consume the watermark
211+ let watermark = now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
212+
213+ assert_eq ! (
214+ watermark,
215+ Message :: Watermark ( Watermark :: new(
216+ 0 ,
217+ ScalarImpl :: NaiveDateTime ( NaiveDateTimeWrapper :: new(
218+ NaiveDateTime :: from_str( "2021-04-01T00:00:00.001" ) . unwrap( )
219+ ) )
220+ ) )
221+ ) ;
222+
223+ // Consume the barrier
224+ now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
225+
226+ tx. send ( Barrier :: new_test_barrier_with_prev_epoch ( 2 << 16 , 1 << 16 ) )
227+ . unwrap ( ) ;
228+
229+ // Consume the data chunk
230+ let chunk_msg = now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
231+
232+ assert_eq ! (
233+ chunk_msg. into_chunk( ) . unwrap( ) . compact( ) ,
234+ StreamChunk :: from_pretty(
235+ " TS
236+ - 2021-04-01T00:00:00.001
237+ + 2021-04-01T00:00:00.002"
238+ )
239+ ) ;
240+
241+ // Consume the watermark
242+ let watermark = now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
243+
244+ assert_eq ! (
245+ watermark,
246+ Message :: Watermark ( Watermark :: new(
247+ 0 ,
248+ ScalarImpl :: NaiveDateTime ( NaiveDateTimeWrapper :: new(
249+ NaiveDateTime :: from_str( "2021-04-01T00:00:00.002" ) . unwrap( )
250+ ) )
251+ ) )
252+ ) ;
253+
254+ // Consume the barrier
255+ now_executor. next ( ) . await . unwrap ( ) . unwrap ( ) ;
256+ }
257+
258+ #[ inline]
259+ fn create_pk_indices ( ) -> PkIndices {
260+ vec ! [ 0 ]
261+ }
262+
263+ async fn create_state_table ( ) -> StateTable < MemoryStateStore > {
264+ let memory_state_store = MemoryStateStore :: new ( ) ;
265+ let table_id = TableId :: new ( 1 ) ;
266+ let column_descs = vec ! [ ColumnDesc :: unnamed( ColumnId :: new( 0 ) , DataType :: Timestamp ) ] ;
267+ let order_types = vec ! [ OrderType :: Ascending ] ;
268+ let pk_indices = create_pk_indices ( ) ;
269+ StateTable :: new_without_distribution (
270+ memory_state_store,
271+ table_id,
272+ column_descs,
273+ order_types,
274+ pk_indices,
275+ )
276+ . await
277+ }
278+
279+ fn create_executor (
280+ state_table : StateTable < MemoryStateStore > ,
281+ ) -> ( UnboundedSender < Barrier > , BoxedMessageStream ) {
282+ let ( sender, barrier_receiver) = unbounded_channel ( ) ;
283+ let now_executor = NowExecutor :: new ( barrier_receiver, 1 , state_table) ;
284+ ( sender, Box :: new ( now_executor) . execute ( ) )
285+ }
286+ }
0 commit comments