github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/direct/test_direct_runner.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """Wrapper of Beam runners that's built for running and verifying e2e tests."""
    19  
    20  # pytype: skip-file
    21  
    22  from apache_beam.internal import pickler
    23  from apache_beam.options.pipeline_options import StandardOptions
    24  from apache_beam.options.pipeline_options import TestOptions
    25  from apache_beam.runners.direct.direct_runner import DirectRunner
    26  from apache_beam.runners.runner import PipelineState
    27  
    28  __all__ = ['TestDirectRunner']
    29  
    30  
    31  class TestDirectRunner(DirectRunner):
    32    def run_pipeline(self, pipeline, options):
    33      """Execute test pipeline and verify test matcher"""
    34      test_options = options.view_as(TestOptions)
    35      on_success_matcher = test_options.on_success_matcher
    36      is_streaming = options.view_as(StandardOptions).streaming
    37  
    38      # [BEAM-1889] Do not send this to remote workers also, there is no need to
    39      # send this option to remote executors.
    40      test_options.on_success_matcher = None
    41  
    42      self.result = super().run_pipeline(pipeline, options)
    43  
    44      try:
    45        if not is_streaming:
    46          self.result.wait_until_finish()
    47  
    48        if on_success_matcher:
    49          from hamcrest import assert_that as hc_assert_that
    50          hc_assert_that(self.result, pickler.loads(on_success_matcher))
    51      finally:
    52        if not PipelineState.is_terminal(self.result.state):
    53          self.result.cancel()
    54          self.result.wait_until_finish()
    55  
    56      return self.result