[ https://issues.apache.org/jira/browse/BEAM-3377?focusedWorklogId=103221&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-103221 ]
ASF GitHub Bot logged work on BEAM-3377: ---------------------------------------- Author: ASF GitHub Bot Created on: 18/May/18 05:10 Start Date: 18/May/18 05:10 Worklog Time Spent: 10m Work Description: aaltay commented on a change in pull request #5384: [BEAM-3377] Add validation for streaming wordcount with assert_that URL: https://github.com/apache/beam/pull/5384#discussion_r189163882 ########## File path: sdks/python/apache_beam/examples/streaming_wordcount_debugging.py ########## @@ -0,0 +1,179 @@ +# +# 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. +# + +"""An example to use assert_that to validate streaming wordcount. + +It includes: + - PrintFn (DoFn) to inspect element, window, and timestamp. + - AddTimestampFn (DoFn) to modify timestamps. + - assert_that via check_gbk_format and equal_to_per_window (matchers). +""" + +from __future__ import absolute_import + +import argparse +import logging +import re + +import six + +import apache_beam as beam +import apache_beam.transforms.window as window +from apache_beam.examples.wordcount import WordExtractingDoFn +from apache_beam.options.pipeline_options import PipelineOptions +from apache_beam.options.pipeline_options import SetupOptions +from apache_beam.options.pipeline_options import StandardOptions +from apache_beam.testing.util import assert_that +from apache_beam.testing.util import equal_to_per_window +from apache_beam.transforms.core import ParDo + + +class PrintFn(beam.DoFn): + """A DoFn that prints label, element, its window, and its timstamp. """ + def __init__(self, label): + self.label = label + + def process(self, element, timestamp=beam.DoFn.TimestampParam, + window=beam.DoFn.WindowParam): + # Log at INFO level each element processed. When executing this pipeline + # using the Dataflow service, these log lines will appear in the Cloud + # Logging UI. + logging.info('[%s]: %s %s %s', self.label, element, window, timestamp) + yield element + + +class AddTimestampFn(beam.DoFn): + """A DoFn that attaches timestamps to its elements. + + It takes a string of integers and it attaches to each of them + a timestamp of its same value.""" + def process(self, element): + for elem in element.split(' '): + logging.info('Adding timestamp to: %s', element) + yield beam.window.TimestampedValue(elem, int(elem)) + + +def run(argv=None): + """Build and run the pipeline.""" + parser = argparse.ArgumentParser() + parser.add_argument( + '--output_topic', required=True, + help=('Output PubSub topic of the form ' + '"projects/<PROJECT>/topic/<TOPIC>".')) + parser.add_argument( + '--use_assert_that', action='store_true', Review comment: `--use_assert_that` option is not used, please remove it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 103221) Time Spent: 4h 10m (was: 4h) > assert_that not working for streaming > ------------------------------------- > > Key: BEAM-3377 > URL: https://issues.apache.org/jira/browse/BEAM-3377 > Project: Beam > Issue Type: Bug > Components: sdk-py-core > Affects Versions: 2.2.0 > Reporter: MarĂa GH > Priority: Major > Labels: starter > Time Spent: 4h 10m > Remaining Estimate: 0h > > assert_that does not work for AfterWatermark timers. > Easy way to reproduce: modify test_gbk_execution [1] in this form: > > {code:java} > def test_this(self): > test_stream = (TestStream() > .add_elements(['a', 'b', 'c']) > .advance_watermark_to(20)) > def fnc(x): > print 'fired_elem:', x > return x > options = PipelineOptions() > options.view_as(StandardOptions).streaming = True > p = TestPipeline(options=options) > records = (p > | test_stream > | beam.WindowInto( > FixedWindows(15), > > trigger=trigger.AfterWatermark(early=trigger.AfterCount(2)), > accumulation_mode=trigger.AccumulationMode.ACCUMULATING) > | beam.Map(lambda x: ('k', x)) > | beam.GroupByKey()) > assert_that(records, equal_to([ > ('k', ['a', 'b', 'c'])])) > p.run() > {code} > This test will pass, but if the .advance_watermark_to(20) is removed, the > test will fail. However, both cases fire the same elements: > fired_elem: ('k', ['a', 'b', 'c']) > fired_elem: ('k', ['a', 'b', 'c']) > In the passing case, they correspond to the sorted_actual inside the > assert_that. In the failing case: > sorted_actual: [('k', ['a', 'b', 'c']), ('k', ['a', 'b', 'c'])] > sorted_actual: [] > [1] > https://github.com/mariapython/incubator-beam/blob/direct-timers-show/sdks/python/apache_beam/testing/test_stream_test.py#L120 -- This message was sent by Atlassian JIRA (v7.6.3#76005)