Skip to content

Commit bf16250

Browse files
committed
Add a test
1 parent 0597b73 commit bf16250

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

sdks/python/apache_beam/runners/worker/sdk_worker_test.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from apache_beam.portability.api import beam_fn_api_pb2_grpc
3838
from apache_beam.portability.api import beam_runner_api_pb2
3939
from apache_beam.portability.api import metrics_pb2
40+
from apache_beam.runners.worker import data_plane
4041
from apache_beam.runners.worker import sdk_worker
4142
from apache_beam.runners.worker import statecache
4243
from apache_beam.runners.worker.sdk_worker import BundleProcessorCache
@@ -126,7 +127,10 @@ def test_fn_registration(self):
126127

127128
def test_inactive_bundle_processor_returns_empty_progress_response(self):
128129
bundle_processor = mock.MagicMock()
129-
bundle_processor_cache = BundleProcessorCache(None, None, None, {})
130+
data_channel_factory = mock.create_autospec(
131+
data_plane.GrpcClientDataChannelFactory)
132+
bundle_processor_cache = BundleProcessorCache(
133+
None, None, data_channel_factory, {})
130134
bundle_processor_cache.activate('instruction_id')
131135
worker = SdkWorker(bundle_processor_cache)
132136
split_request = beam_fn_api_pb2.InstructionRequest(
@@ -153,7 +157,10 @@ def test_inactive_bundle_processor_returns_empty_progress_response(self):
153157

154158
def test_failed_bundle_processor_returns_failed_progress_response(self):
155159
bundle_processor = mock.MagicMock()
156-
bundle_processor_cache = BundleProcessorCache(None, None, None, {})
160+
data_channel_factory = mock.create_autospec(
161+
data_plane.GrpcClientDataChannelFactory)
162+
bundle_processor_cache = BundleProcessorCache(
163+
None, None, data_channel_factory, {})
157164
bundle_processor_cache.activate('instruction_id')
158165
worker = SdkWorker(bundle_processor_cache)
159166

@@ -176,7 +183,10 @@ def test_failed_bundle_processor_returns_failed_progress_response(self):
176183

177184
def test_inactive_bundle_processor_returns_empty_split_response(self):
178185
bundle_processor = mock.MagicMock()
179-
bundle_processor_cache = BundleProcessorCache(None, None, None, {})
186+
data_channel_factory = mock.create_autospec(
187+
data_plane.GrpcClientDataChannelFactory)
188+
bundle_processor_cache = BundleProcessorCache(
189+
None, None, data_channel_factory, {})
180190
bundle_processor_cache.activate('instruction_id')
181191
worker = SdkWorker(bundle_processor_cache)
182192
split_request = beam_fn_api_pb2.InstructionRequest(
@@ -262,7 +272,10 @@ def test_harness_monitoring_infos_and_metadata(self):
262272

263273
def test_failed_bundle_processor_returns_failed_split_response(self):
264274
bundle_processor = mock.MagicMock()
265-
bundle_processor_cache = BundleProcessorCache(None, None, None, {})
275+
data_channel_factory = mock.create_autospec(
276+
data_plane.GrpcClientDataChannelFactory)
277+
bundle_processor_cache = BundleProcessorCache(
278+
None, None, data_channel_factory, {})
266279
bundle_processor_cache.activate('instruction_id')
267280
worker = SdkWorker(bundle_processor_cache)
268281

@@ -338,6 +351,29 @@ def stop(self):
338351

339352
self.assertEqual(response, expected_response)
340353

354+
def test_bundle_processor_creation_failure_cleans_up_grpc_data_channel(self):
355+
data_channel_factory = data_plane.GrpcClientDataChannelFactory()
356+
channel = data_channel_factory.create_data_channel_from_url('some_url')
357+
state_handler_factory = mock.create_autospec(
358+
sdk_worker.GrpcStateHandlerFactory)
359+
bundle_processor_cache = BundleProcessorCache(
360+
frozenset(), state_handler_factory, data_channel_factory, {})
361+
if bundle_processor_cache.periodic_shutdown:
362+
bundle_processor_cache.periodic_shutdown.cancel()
363+
364+
bundle_processor_cache.get = mock.MagicMock(
365+
side_effect=RuntimeError('test error'))
366+
367+
worker = SdkWorker(bundle_processor_cache)
368+
instruction_id = 'instruction_id'
369+
request = beam_fn_api_pb2.ProcessBundleRequest(
370+
process_bundle_descriptor_id='descriptor_id')
371+
372+
with self.assertRaises(RuntimeError):
373+
worker.process_bundle(request, instruction_id)
374+
375+
self.assertIn(instruction_id, channel._cleaned_instruction_ids)
376+
341377

342378
class CachingStateHandlerTest(unittest.TestCase):
343379
def test_caching(self):

0 commit comments

Comments
 (0)