3535import apache_beam as beam
3636from apache_beam .options .pipeline_options import DebugOptions
3737from apache_beam .options .pipeline_options import PortableOptions
38+ from apache_beam .options .pipeline_options import StandardOptions
3839from apache_beam .runners .portability import portable_runner_test
3940from apache_beam .runners .portability import prism_runner
4041from apache_beam .testing .util import assert_that
4142from apache_beam .testing .util import equal_to
43+ from apache_beam .transforms import trigger
44+ from apache_beam .transforms import window
4245from apache_beam .utils import shared
4346
4447# Run as
@@ -64,6 +67,7 @@ def __init__(self, *args, **kwargs):
6467 self .environment_type = None
6568 self .environment_config = None
6669 self .enable_commit = False
70+ self .streaming = False
6771
6872 def setUp (self ):
6973 self .enable_commit = False
@@ -175,6 +179,7 @@ def create_options(self):
175179 options .view_as (
176180 PortableOptions ).environment_options = self .environment_options
177181
182+ options .view_as (StandardOptions ).streaming = self .streaming
178183 return options
179184
180185 # Can't read host files from within docker, read a "local" file there.
@@ -225,7 +230,57 @@ def test_custom_window_type(self):
225230 def test_metrics (self ):
226231 super ().test_metrics (check_bounded_trie = False )
227232
228- # Inherits all other tests.
233+ def construct_timestamped (k , t ):
234+ return window .TimestampedValue ((k , t ), t )
235+
236+ def test_after_count_trigger_batch (self ):
237+ with self .create_pipeline () as p :
238+ result = (
239+ p
240+ | beam .Create ([1 , 2 , 3 , 4 , 5 , 10 , 11 ])
241+ | beam .FlatMap (lambda t : [('A' , t ), ('B' , t + 5 )])
242+ #A1, A2, A3, A4, A5, A10, A11, B6, B7, B8, B9, B10, B15, B16
243+ | beam .MapTuple (PrismRunnerTest .construct_timestamped )
244+ | beam .WindowInto (
245+ window .FixedWindows (10 ),
246+ trigger = trigger .AfterCount (3 ),
247+ accumulation_mode = trigger .AccumulationMode .DISCARDING ,
248+ )
249+ | beam .GroupByKey ())
250+ # yapf: disable
251+ assert_that (
252+ result ,
253+ equal_to ([('A' , [1 , 2 , 3 , 4 , 5 ],
254+ ('A' , [10 , 11 ]),
255+ ('B' , [6 , 7 , 8 , 9 ]),
256+ ('B' , [10 , 15 , 16 ]))]))
257+ # yapf: enable
258+
259+ def test_after_count_trigger_streaming (self ):
260+ self .streaming = True
261+ with self .create_pipeline () as p :
262+ result = (
263+ p
264+ | beam .Create ([1 , 2 , 3 , 4 , 5 , 10 , 11 ])
265+ | beam .FlatMap (lambda t : [('A' , t ), ('B' , t + 5 )])
266+ #A1, A2, A3, A4, A5, A10, A11, B6, B7, B8, B9, B10, B15, B16
267+ | beam .MapTuple (PrismRunnerTest .construct_timestamped )
268+ | beam .WindowInto (
269+ window .FixedWindows (10 ),
270+ trigger = trigger .AfterCount (3 ),
271+ accumulation_mode = trigger .AccumulationMode .DISCARDING ,
272+ )
273+ | beam .GroupByKey ())
274+ # yapf: disable
275+ assert_that (
276+ result ,
277+ equal_to ([('A' , [1 , 2 , 3 ],
278+ ('A' , [4 , 5 ]),
279+ ('A' , [10 , 11 ]),
280+ ('B' , [6 , 7 , 8 ]),
281+ ('B' , [9 ,]),
282+ ('B' , [10 , 15 , 16 ]))]))
283+ # yapf: enable
229284
230285
231286class PrismJobServerTest (unittest .TestCase ):
0 commit comments