TestSynchronousMeasurementConsumer.test_collect_deadline failures in CI
I've started seeing failures in this test today in CI (pypy / windows), since this is asserting a mock I guess there's something wrong in the test.
__________ TestSynchronousMeasurementConsumer.test_collect_deadline ___________
self = <test_measurement_consumer.TestSynchronousMeasurementConsumer testMethod=test_collect_deadline>
mock_callback_options = <MagicMock name='CallbackOptions' id='1661901429728'>
MockMetricReaderStorage = <MagicMock name='MetricReaderStorage' id='1661901430400'>
@patch(
"opentelemetry.sdk.metrics._internal."
"measurement_consumer.CallbackOptions"
)
def test_collect_deadline(
self, mock_callback_options, MockMetricReaderStorage
):
reader_mock = Mock()
reader_storage_mock = Mock()
MockMetricReaderStorage.return_value = reader_storage_mock
consumer = SynchronousMeasurementConsumer(
SdkConfiguration(
exemplar_filter=Mock(),
resource=Mock(),
metric_readers=[reader_mock],
views=Mock(),
)
)
def sleep_1(*args, **kwargs):
sleep(1)
return []
consumer.register_asynchronous_instrument(
Mock(**{"callback.side_effect": sleep_1})
)
consumer.register_asynchronous_instrument(
Mock(**{"callback.side_effect": sleep_1})
)
consumer.collect(reader_mock)
> callback_options_time_call = mock_callback_options.mock_calls[
-1
].kwargs["timeout_millis"]
E KeyError: 'timeout_millis'
def sleep_1(*args, **kwargs): sleep(1) return []
Is someone's fixing up this test, maybe we can reduce or modify the sleep here to speed things up
Hi, I'd like to work on a fix for this if it's still available.
After looking at the source, I think the root cause is a race condition in the test itself. The test calls consumer.collect(reader_mock) with the default timeout_millis=10_000, then registers two callbacks that each sleep(1). The intent is that after callback 1 runs, remaining_time drops below default_timeout_ns (10s), triggering CallbackOptions(timeout_millis=...) before callback 2.
On slow platforms like PyPy/Windows, callback 1 alone can consume enough time that time_ns() >= deadline_ns after it returns, causing MetricsTimeoutError to be raised before the loop ever constructs a CallbackOptions with timeout_millis. This means mock_calls[-1] ends up being the initial no-arg CallbackOptions() call at the top of collect(), and .kwargs['timeout_millis'] raises KeyError.
My proposed fix is to refactor the test to mock time_ns with controlled return values instead of relying on sleep(1), making the timing deterministic across platforms. I'd also consider simplifying the production logic by removing the default_timeout_ns threshold and always constructing CallbackOptions(timeout_millis=remaining/1e6) per iteration.
Happy to submit a PR if this approach sounds good.
@ravitheja4531-cell Could you please throw your bot to one issue at a time? thanks