Skip to content

Commit a146463

Browse files
committed
Add test cases to test code paths
1 parent ed86d99 commit a146463

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121
import logging
2222
import time
2323
import unittest
24+
from unittest.mock import Mock
2425

2526
from tenacity import retry
2627
from tenacity import stop_after_attempt
2728

2829
from apache_beam.runners.worker import statesampler
2930
from apache_beam.utils.counters import CounterFactory
3031
from apache_beam.utils.counters import CounterName
32+
from apache_beam.runners.worker import operation_specs
33+
from apache_beam.runners.worker import operations
34+
from apache_beam.internal import pickler
35+
from apache_beam.transforms import core
3136

3237
_LOGGER = logging.getLogger(__name__)
3338

@@ -213,6 +218,59 @@ def test_process_timers_metric_is_recorded(self):
213218
expected_value * (1.0 + margin_of_error),
214219
"The timer metric was higher than expected.")
215220

221+
def test_do_operation_with_sampler(self):
222+
"""
223+
Tests that a DoOperation with an active state_sampler correctly
224+
creates a real ScopedState object for timer processing.
225+
"""
226+
mock_spec = operation_specs.WorkerDoFn(
227+
serialized_fn=pickler.dumps((core.DoFn(), None, None, None, None)),
228+
output_tags=[],
229+
input=None,
230+
side_inputs=[],
231+
output_coders=[])
232+
233+
sampler = statesampler.StateSampler(
234+
'test_stage', CounterFactory(), sampling_period_ms=1)
235+
236+
# 1. Create the operation WITHOUT the unexpected keyword argument
237+
op = operations.create_operation(
238+
name_context='test_op',
239+
spec=mock_spec,
240+
counter_factory=CounterFactory(),
241+
state_sampler=sampler)
242+
243+
# 2. Set the user_state_context attribute AFTER creation
244+
op.user_state_context = Mock()
245+
246+
self.assertIsNot(
247+
op.scoped_timer_processing_state, statesampler.NOOP_SCOPED_STATE)
248+
249+
def test_do_operation_without_sampler(self):
250+
"""
251+
Tests that a DoOperation without a state_sampler correctly uses the
252+
NOOP_SCOPED_STATE for timer processing.
253+
"""
254+
mock_spec = operation_specs.WorkerDoFn(
255+
serialized_fn=pickler.dumps((core.DoFn(), None, None, None, None)),
256+
output_tags=[],
257+
input=None,
258+
side_inputs=[],
259+
output_coders=[])
260+
261+
# 1. Create the operation WITHOUT the unexpected keyword argument
262+
op = operations.create_operation(
263+
name_context='test_op',
264+
spec=mock_spec,
265+
counter_factory=CounterFactory(),
266+
state_sampler=None)
267+
268+
# 2. Set the user_state_context attribute AFTER creation
269+
op.user_state_context = Mock()
270+
271+
self.assertIs(
272+
op.scoped_timer_processing_state, statesampler.NOOP_SCOPED_STATE)
273+
216274

217275
if __name__ == '__main__':
218276
logging.getLogger().setLevel(logging.INFO)

0 commit comments

Comments
 (0)