googleapis / python-pubsub

Apache License 2.0
392 stars 205 forks source link

Fix flaky tests in test_publisher_client caused due to non-deterministic number of spans exported by OpenTelemetry #1290

Closed mukund-ananthu closed 2 weeks ago

mukund-ananthu commented 3 weeks ago

Fix error:

=================================== FAILURES ===================================
__________________ test_opentelemetry_flow_control_exception ___________________

creds = 
span_exporter = 

    @pytest.mark.skipif(
        sys.version_info < (3, 8),
        reason="Open Telemetry not supported below Python version 3.8",
    )
    def test_opentelemetry_flow_control_exception(creds, span_exporter):
        publisher_options = types.PublisherOptions(
            flow_control=types.PublishFlowControl(
                message_limit=10,
                byte_limit=150,
                limit_exceeded_behavior=types.LimitExceededBehavior.ERROR,
            ),
            enable_open_telemetry_tracing=True,
        )
        client = publisher.Client(credentials=creds, publisher_options=publisher_options)

        mock_batch = mock.Mock(spec=client._batch_class)
        topic = "projects/projectID/topics/topicID"
        client._set_batch(topic, mock_batch)

        future1 = client.publish(topic, b"a" * 60)
        future2 = client.publish(topic, b"b" * 100)

        future1.result()  # no error, still within flow control limits
        with pytest.raises(exceptions.FlowControlLimitError):
            future2.result()

        spans = span_exporter.get_finished_spans()
        # Span 1 = Publisher Flow Control Span of first publish
        # Span 2 = Publisher Batching Span of first publish
        # Span 3 = Publisher Flow Control Span of second publish(raises FlowControlLimitError)
        # Span 4 = Publish Create Span of second publish(raises FlowControlLimitError)
>       assert len(spans) == 4
E       assert 5 == 4
E        +  where 5 = len((, , ))

tests/unit/pubsub_v1/publisher/test_publisher_client.py:294: AssertionError
- generated xml file: /tmpfs/src/github/python-pubsub/unit_3.12_sponge_log.xml -
=========================== short test summary info ============================