From 70e66d63301c4fcae1c763c44c63e88e0b409994 Mon Sep 17 00:00:00 2001 From: Faizal Kassamali Date: Thu, 13 Nov 2025 20:57:11 -0800 Subject: [PATCH 1/4] WIP: Fix floating point test error Avoid floating point conversion imprecision --- tests/unit/gapic/test_method.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 927902bf0..cdc6c36a7 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -200,7 +200,9 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): method, default_retry, default_timeout ) - result = wrapped_method(timeout=22) + # Using "result = wrapped_method(timeout=22)" fails since wrapped_method + # does floating point calculations that results in 21.987.. instead of 22 + result = wrapped_method(timeout=timeout.ConstantTimeout(22)) assert result == 42 From 004e845e024184e485d4c15004b9a5fb5398d14c Mon Sep 17 00:00:00 2001 From: Faizal Kassamali Date: Thu, 13 Nov 2025 21:23:57 -0800 Subject: [PATCH 2/4] Fix linter error with black Fix linter error with black --- tests/unit/gapic/test_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index cdc6c36a7..22f37c8aa 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -200,7 +200,7 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): method, default_retry, default_timeout ) - # Using "result = wrapped_method(timeout=22)" fails since wrapped_method + # Using "result = wrapped_method(timeout=22)" fails since wrapped_method # does floating point calculations that results in 21.987.. instead of 22 result = wrapped_method(timeout=timeout.ConstantTimeout(22)) From a9cb163b6b3d1d93a2ded9186909e527863c15b9 Mon Sep 17 00:00:00 2001 From: Faizal Kassamali Date: Thu, 13 Nov 2025 21:34:47 -0800 Subject: [PATCH 3/4] Fix flaky performance test Ensure URL encoding takes enough time so that cached version is measurably 10x faster --- tests/unit/gapic/test_routing_header.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/gapic/test_routing_header.py b/tests/unit/gapic/test_routing_header.py index 2c8c75469..f0ec82ec6 100644 --- a/tests/unit/gapic/test_routing_header.py +++ b/tests/unit/gapic/test_routing_header.py @@ -90,8 +90,8 @@ def test__urlencode_param(key, value, expected): def test__urlencode_param_caching_performance(): import time - key = "key" * 100 - value = "value" * 100 + key = "key" * 10000 + value = "value" * 10000 # time with empty cache start_time = time.perf_counter() routing_header._urlencode_param(key, value) From b555d80d6b1df06134b558cdd6f38bb66965bf52 Mon Sep 17 00:00:00 2001 From: Faizal Kassamali Date: Fri, 14 Nov 2025 16:58:36 -0800 Subject: [PATCH 4/4] Skip known flaky test Change code to skip flaky test and add a new test that uses a constant timeout instead of a number to check that override works --- tests/unit/gapic/test_method.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/unit/gapic/test_method.py b/tests/unit/gapic/test_method.py index 22f37c8aa..29e8fc217 100644 --- a/tests/unit/gapic/test_method.py +++ b/tests/unit/gapic/test_method.py @@ -192,6 +192,7 @@ def test_wrap_method_with_overriding_retry_timeout_compression(unused_sleep): ) +@pytest.mark.skip(reason="Known flaky due to floating point comparison. #866") def test_wrap_method_with_overriding_timeout_as_a_number(): method = mock.Mock(spec=["__call__"], return_value=42) default_retry = retry.Retry() @@ -202,7 +203,7 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): # Using "result = wrapped_method(timeout=22)" fails since wrapped_method # does floating point calculations that results in 21.987.. instead of 22 - result = wrapped_method(timeout=timeout.ConstantTimeout(22)) + result = wrapped_method(timeout=22) assert result == 42 @@ -212,6 +213,24 @@ def test_wrap_method_with_overriding_timeout_as_a_number(): assert actual_timeout == pytest.approx(22, abs=0.01) +def test_wrap_method_with_overriding_constant_timeout(): + method = mock.Mock(spec=["__call__"], return_value=42) + default_retry = retry.Retry() + default_timeout = timeout.ConstantTimeout(60) + wrapped_method = google.api_core.gapic_v1.method.wrap_method( + method, default_retry, default_timeout + ) + + result = wrapped_method(timeout=timeout.ConstantTimeout(22)) + + assert result == 42 + + actual_timeout = method.call_args[1]["timeout"] + metadata = method.call_args[1]["metadata"] + assert metadata == mock.ANY + assert actual_timeout == 22 + + def test_wrap_method_with_call(): method = mock.Mock() mock_call = mock.Mock()