open-telemetry / opentelemetry-python

OpenTelemetry Python API and SDK
https://opentelemetry.io
Apache License 2.0
1.8k stars 625 forks source link

flaky test for opentelemetry-sdk in CI #4070

Open emdneto opened 3 months ago

emdneto commented 3 months ago

Describe your environment

OS: Windows Python version: Python3.8 SDK version: main API version: main

What happened?

================================== FAILURES ===================================
_______ TestBatchSpanProcessor.test_batch_span_processor_reset_timeout ________

self = <tests.trace.export.test_export.TestBatchSpanProcessor testMethod=test_batch_span_processor_reset_timeout>

    @mark.skipif(
        python_implementation() == "PyPy" and system() == "Windows",
        reason="This test randomly fails in Windows with PyPy",
    )
    def test_batch_span_processor_reset_timeout(self):
        """Test that the scheduled timeout is reset on cycles without spans"""
        spans_names_list = []

        export_event = threading.Event()
        my_exporter = MySpanExporter(
            destination=spans_names_list,
            export_event=export_event,
            export_timeout_millis=50,
        )

        span_processor = export.BatchSpanProcessor(
            my_exporter,
            schedule_delay_millis=50,
        )

        with mock.patch.object(span_processor.condition, "wait") as mock_wait:
            resource = Resource.create({})
            _create_start_and_end_span("foo", span_processor, resource)
            self.assertTrue(export_event.wait(2))

            # give some time for exporter to loop
            # since wait is mocked it should return immediately
            time.sleep(0.1)
            mock_wait_calls = list(mock_wait.mock_calls)

            # find the index of the call that processed the singular span
            for idx, wait_call in enumerate(mock_wait_calls):
                _, args, __ = wait_call
                if args[0] <= 0:
                    after_calls = mock_wait_calls[idx + 1 :]
                    break

            self.assertTrue(
>               all(args[0] >= 0.05 for _, args, __ in after_calls)
            )
E           UnboundLocalError: local variable 'after_calls' referenced before assignment

Steps to Reproduce

Run in CI https://github.com/open-telemetry/opentelemetry-python/actions/runs/10041807138/job/27750740610

Expected Result

Test pass every time

Actual Result

Some runs test don't pass

Additional context

No response

Would you like to implement a fix?

None

arunk1988 commented 3 months ago

Hi @emdneto , I am looking into this, will open a PR shortly.

Ali-Alnosairi commented 2 months ago

Hi @emdneto , Can I work on this?

emdneto commented 2 months ago

@Ali-Alnosairi, I think there is an open PR. Feel free to improve it