shunping commented on code in PR #36575:
URL: https://github.com/apache/beam/pull/36575#discussion_r2453717054


##########
sdks/python/apache_beam/examples/cookbook/ordered_window_elements/streaming_test.py:
##########
@@ -0,0 +1,358 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import logging
+import shutil
+import sys
+import unittest
+
+from parameterized import param
+from parameterized import parameterized
+from parameterized import parameterized_class
+
+import apache_beam as beam
+from apache_beam.examples.cookbook.ordered_window_elements.streaming import 
BufferStateType
+from apache_beam.examples.cookbook.ordered_window_elements.streaming import 
OrderedWindowElements
+from apache_beam.options.pipeline_options import PipelineOptions
+from apache_beam.testing.test_pipeline import TestPipeline
+from apache_beam.testing.test_stream import TestStream
+from apache_beam.testing.util import assert_that
+from apache_beam.testing.util import equal_to
+from apache_beam.transforms.periodicsequence import PeriodicImpulse
+from apache_beam.transforms.periodicsequence import RebaseMode
+from apache_beam.utils.timestamp import Timestamp
+
+logging.basicConfig(level=logging.WARNING)
+
+ENABLE_LOGGING = False
+WINDOW_SIZE = 3
+FIRE_INTERVAL = 0.5
+
+
+def _maybe_log_elements(pcoll, prefix="result="):
+  if ENABLE_LOGGING:
+    return pcoll | beam.LogElements(
+        prefix=prefix,
+        level=logging.WARNING,
+        with_timestamp=True,
+        with_window=True,
+        use_epoch_time=True)
+  else:
+    return pcoll
+
+
+# Creates an unbounded source via `PeriodicImpulse`, simulating a continuous
+# stream of elements fired at a fixed interval. This method is closer to
+# real-world streaming but is sensitive to system load and can cause test
+# flakiness.
+# If the test runner is slow or under heavy load, elements may be delayed and
+# processed in a single large bundle. This can defeat the purpose of testing
+# time-based logic, as the elements will not arrive distributed over time as
+# intended.
+def _create_periodic_impulse_stream(elements: list[int]):
+  now = Timestamp.now()
+  length = len(elements)
+  fire_interval = FIRE_INTERVAL
+  return PeriodicImpulse(
+      data=[(Timestamp.of(e), e) for e in elements],
+      fire_interval=fire_interval,
+      start_timestamp=now,
+      stop_timestamp=now + length * fire_interval,
+      rebase=RebaseMode.REBASE_ALL,
+  )
+
+
+# Creates an unbounded source via `TestStream`, allowing precise control over
+# watermarks and element emission for deterministic testing scenarios. However,
+# it is an instantaneous data stream and it is less realistic than the stream
+# from `PeriodicImpulse`.
+def _create_test_stream(elements: list[int]):
+  test_stream = TestStream()
+  wm = None
+  for e in elements:
+    test_stream.add_elements([e], event_timestamp=e)
+    if wm is None or wm < e:
+      wm = e
+      test_stream.advance_watermark_to(wm)
+
+  test_stream.advance_watermark_to_infinity()
+  return test_stream
+
+
+_go_installed = shutil.which('go') is not None
+_in_windows = sys.platform == "win32"
+
+
[email protected](_go_installed, 'Go is not installed.')
+# TODO: Go environments is not configured correctly on Windows test boxes.
[email protected](_in_windows, reason="Not supported on Windows")
+@parameterized_class(
+    'buffer_state_type',
+    [
+        (BufferStateType.ORDERED_LIST, ),
+        (BufferStateType.BAG, ),
+        (BufferStateType.VALUE, ),
+    ])
+class OrderedWindowElementsTest(unittest.TestCase):
+  def setUp(self) -> None:
+    self.options = PipelineOptions([
+        "--streaming",
+        "--environment_type=LOOPBACK",
+        "--runner=PrismRunner",
+        "--prism_log_kind=dev",
+        # # run on an external Portable Runner for debugging
+        # "--runner=PortableRunner",
+        # "--job_endpoint=localhost:8073",
+    ])
+
+    # # dataflow runner option
+    # self.options = PipelineOptions([
+    #     "--streaming",
+    #     "--runner=DataflowRunner",
+    #     "--temp_location=gs://shunping-test/anomaly-temp",
+    #     "--staging_location=gs://shunping-test/anomaly-temp",
+    #     "--project=apache-beam-testing",
+    #     "--region=us-central1",
+    #     "--sdk_location=dist/apache_beam-2.69.0.dev0.tar.gz",
+    #     #"--pickle_library=dill",
+    #     #"--save_main_session",
+    # ])

Review Comment:
   Ack. We will keep this for future reference as this is an example.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to