From b3c605c3844896c8d91a0a58c31f9647a8ec24e9 Mon Sep 17 00:00:00 2001 From: Awais Date: Wed, 28 Feb 2024 17:12:11 +0100 Subject: [PATCH 1/9] red test --- rele/worker.py | 3 +-- tests/test_worker.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/rele/worker.py b/rele/worker.py index 38a102c..25917d6 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -21,9 +21,8 @@ class NotConnectionError(BaseException): pass -def check_internet_connection(): +def check_internet_connection(remote_server="www.google.com"): logger.debug("Checking connection") - remote_server = "www.google.com" port = 80 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) diff --git a/tests/test_worker.py b/tests/test_worker.py index fa23d98..081462a 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -135,6 +135,16 @@ def test_raises_not_connection_error_during_start( with pytest.raises(NotConnectionError): worker.start() + mock_internet_connection.assert_called_once_with() + + def test_check_internet_connection_uses_api_endpoint_setting_when_present( + self, worker, mock_internet_connection + ): + mock_internet_connection.return_value = False + + with pytest.raises(NotConnectionError): + worker.start() + mock_internet_connection.assert_called_once_with(remote_server="custom-api.interconnect.example.com") @pytest.mark.usefixtures("mock_create_subscription") From 18630c326481e96a630c0177df2071fe794bc7c5 Mon Sep 17 00:00:00 2001 From: jbonora Date: Wed, 28 Feb 2024 17:29:01 +0100 Subject: [PATCH 2/9] add internet_check_endpoint attribute --- rele/worker.py | 5 +++-- tests/test_worker.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/rele/worker.py b/rele/worker.py index 25917d6..8630fca 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -21,7 +21,7 @@ class NotConnectionError(BaseException): pass -def check_internet_connection(remote_server="www.google.com"): +def check_internet_connection(remote_server): logger.debug("Checking connection") port = 80 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -66,6 +66,7 @@ def __init__( self._futures: Dict[str, Future] = {} self._subscriptions = subscriptions self.threads_per_subscription = threads_per_subscription + self.internet_check_endpoint = client_options.get("api_endpoint") if client_options is not None else "www.google.com" def setup(self): """Create the subscriptions on a Google PubSub topic. @@ -151,7 +152,7 @@ def _boostrap_consumption(self, subscription): "future cancelled and result" ) - if not check_internet_connection(): + if not check_internet_connection(self.internet_check_endpoint): logger.debug( f"Not internet " f"connection when boostrap a consumption for {subscription}" diff --git a/tests/test_worker.py b/tests/test_worker.py index 081462a..485b1be 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -135,7 +135,7 @@ def test_raises_not_connection_error_during_start( with pytest.raises(NotConnectionError): worker.start() - mock_internet_connection.assert_called_once_with() + mock_internet_connection.assert_called_once_with("www.google.com") def test_check_internet_connection_uses_api_endpoint_setting_when_present( self, worker, mock_internet_connection @@ -144,7 +144,7 @@ def test_check_internet_connection_uses_api_endpoint_setting_when_present( with pytest.raises(NotConnectionError): worker.start() - mock_internet_connection.assert_called_once_with(remote_server="custom-api.interconnect.example.com") + mock_internet_connection.assert_called_once_with("custom-api.interconnect.example.com") @pytest.mark.usefixtures("mock_create_subscription") From 2d19aa983247c2741b030ae65ed753c5104722fa Mon Sep 17 00:00:00 2001 From: Awais Date: Wed, 28 Feb 2024 17:32:22 +0100 Subject: [PATCH 3/9] green test --- tests/test_worker.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test_worker.py b/tests/test_worker.py index 485b1be..278b0f7 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -36,6 +36,21 @@ def worker(config): ) +@pytest.fixture +def worker_without_client_options(config): + subscriptions = (sub_stub,) + return Worker( + subscriptions, + None, + config.gc_project_id, + config.credentials, + config.gc_storage_region, + default_ack_deadline=60, + threads_per_subscription=10, + default_retry_policy=config.retry_policy, + ) + + @pytest.fixture def mock_consume(config): with patch.object(Subscriber, "consume") as m: @@ -129,12 +144,12 @@ def test_creates_subscription_with_custom_ack_deadline_from_environment( assert worker._subscriber._gc_project_id == "rele-test" def test_raises_not_connection_error_during_start( - self, worker, mock_internet_connection + self, worker_without_client_options, mock_internet_connection ): mock_internet_connection.return_value = False with pytest.raises(NotConnectionError): - worker.start() + worker_without_client_options.start() mock_internet_connection.assert_called_once_with("www.google.com") def test_check_internet_connection_uses_api_endpoint_setting_when_present( From a1201a0197edd27d8e9a5c5f75f51f6ceb899e99 Mon Sep 17 00:00:00 2001 From: Awais Date: Wed, 28 Feb 2024 17:34:36 +0100 Subject: [PATCH 4/9] green test --- rele/worker.py | 2 +- tests/test_worker.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rele/worker.py b/rele/worker.py index 8630fca..93e4311 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -181,7 +181,7 @@ def _wait_forever(self, sleep_interval): while True: logger.debug(f"[_wait_forever][0] Futures: {self._futures.values()}") - if datetime.now().timestamp() % 50 < 1 and not check_internet_connection(): + if datetime.now().timestamp() % 50 < 1 and not check_internet_connection(self.internet_check_endpoint): logger.debug("Not internet connection, raising an Exception") raise NotConnectionError diff --git a/tests/test_worker.py b/tests/test_worker.py index 278b0f7..a138230 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -67,7 +67,7 @@ def mock_create_subscription(): @pytest.fixture(autouse=True) def mock_internet_connection(): - with patch("rele.worker.check_internet_connection") as m: + with patch("rele.worker.check_internet_connection", autospec=True) as m: m.return_value = True yield m From fa4de9a8acdfd5bfc56d623dcb51d0421cae9cf1 Mon Sep 17 00:00:00 2001 From: Awais Date: Wed, 28 Feb 2024 17:38:14 +0100 Subject: [PATCH 5/9] red test --- tests/test_worker.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_worker.py b/tests/test_worker.py index a138230..fb11405 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -152,6 +152,27 @@ def test_raises_not_connection_error_during_start( worker_without_client_options.start() mock_internet_connection.assert_called_once_with("www.google.com") + def test_check_internet_connection_with_defult_endpoint_if_client_options_do_not_have_api_endpoint( + self, config, mock_internet_connection + ): + mock_internet_connection.return_value = False + subscriptions = (sub_stub,) + worker = Worker( + subscriptions, + {}, + config.gc_project_id, + config.credentials, + config.gc_storage_region, + default_ack_deadline=60, + threads_per_subscription=10, + default_retry_policy=config.retry_policy, + ) + + with pytest.raises(NotConnectionError): + worker.start() + mock_internet_connection.assert_called_once_with("www.google.com") + + def test_check_internet_connection_uses_api_endpoint_setting_when_present( self, worker, mock_internet_connection ): From 9ad6d502769e93e32e6463c1eeea97cd75e42073 Mon Sep 17 00:00:00 2001 From: jbonora Date: Wed, 28 Feb 2024 17:58:59 +0100 Subject: [PATCH 6/9] Get correct internet_check_endpoint attribute --- rele/worker.py | 2 +- tests/test_worker.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/rele/worker.py b/rele/worker.py index 93e4311..0d1597c 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -66,7 +66,7 @@ def __init__( self._futures: Dict[str, Future] = {} self._subscriptions = subscriptions self.threads_per_subscription = threads_per_subscription - self.internet_check_endpoint = client_options.get("api_endpoint") if client_options is not None else "www.google.com" + self.internet_check_endpoint = client_options.get("api_endpoint") if client_options is not None and client_options.get("api_endpoint") is not None else "www.google.com" def setup(self): """Create the subscriptions on a Google PubSub topic. diff --git a/tests/test_worker.py b/tests/test_worker.py index fb11405..e5709cd 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -152,7 +152,7 @@ def test_raises_not_connection_error_during_start( worker_without_client_options.start() mock_internet_connection.assert_called_once_with("www.google.com") - def test_check_internet_connection_with_defult_endpoint_if_client_options_do_not_have_api_endpoint( + def test_check_internet_connection_with_default_endpoint_if_client_options_do_not_have_api_endpoint( self, config, mock_internet_connection ): mock_internet_connection.return_value = False @@ -172,7 +172,6 @@ def test_check_internet_connection_with_defult_endpoint_if_client_options_do_not worker.start() mock_internet_connection.assert_called_once_with("www.google.com") - def test_check_internet_connection_uses_api_endpoint_setting_when_present( self, worker, mock_internet_connection ): From 90315b616b143dcf48b389c03ff92f95830bc3f3 Mon Sep 17 00:00:00 2001 From: jbonora Date: Wed, 28 Feb 2024 18:04:28 +0100 Subject: [PATCH 7/9] Refactor --- rele/worker.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rele/worker.py b/rele/worker.py index 0d1597c..33276ea 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -66,7 +66,12 @@ def __init__( self._futures: Dict[str, Future] = {} self._subscriptions = subscriptions self.threads_per_subscription = threads_per_subscription - self.internet_check_endpoint = client_options.get("api_endpoint") if client_options is not None and client_options.get("api_endpoint") is not None else "www.google.com" + self.internet_check_endpoint = self._get_internet_check_endpoint(client_options) + + def _get_internet_check_endpoint(self, client_options): + if client_options is not None and client_options.get("api_endpoint") is not None: + return client_options.get("api_endpoint") + return "www.google.com" def setup(self): """Create the subscriptions on a Google PubSub topic. From 7f4574279305105d6ecf0cff06768256dea08abf Mon Sep 17 00:00:00 2001 From: Awais Date: Thu, 29 Feb 2024 10:03:50 +0100 Subject: [PATCH 8/9] linting --- rele/worker.py | 9 +++++++-- tests/test_worker.py | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rele/worker.py b/rele/worker.py index 33276ea..3f0359f 100644 --- a/rele/worker.py +++ b/rele/worker.py @@ -69,7 +69,10 @@ def __init__( self.internet_check_endpoint = self._get_internet_check_endpoint(client_options) def _get_internet_check_endpoint(self, client_options): - if client_options is not None and client_options.get("api_endpoint") is not None: + if ( + client_options is not None + and client_options.get("api_endpoint") is not None + ): return client_options.get("api_endpoint") return "www.google.com" @@ -186,7 +189,9 @@ def _wait_forever(self, sleep_interval): while True: logger.debug(f"[_wait_forever][0] Futures: {self._futures.values()}") - if datetime.now().timestamp() % 50 < 1 and not check_internet_connection(self.internet_check_endpoint): + if datetime.now().timestamp() % 50 < 1 and not check_internet_connection( + self.internet_check_endpoint + ): logger.debug("Not internet connection, raising an Exception") raise NotConnectionError diff --git a/tests/test_worker.py b/tests/test_worker.py index e5709cd..a753787 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -179,7 +179,9 @@ def test_check_internet_connection_uses_api_endpoint_setting_when_present( with pytest.raises(NotConnectionError): worker.start() - mock_internet_connection.assert_called_once_with("custom-api.interconnect.example.com") + mock_internet_connection.assert_called_once_with( + "custom-api.interconnect.example.com" + ) @pytest.mark.usefixtures("mock_create_subscription") From 49e67cfdab07d729cbfa0b094a0edd9b32586d33 Mon Sep 17 00:00:00 2001 From: Awais Date: Thu, 29 Feb 2024 10:11:22 +0100 Subject: [PATCH 9/9] bump the version --- rele/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rele/__init__.py b/rele/__init__.py index 3d82b7d..40b912e 100644 --- a/rele/__init__.py +++ b/rele/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.15.0b0" +__version__ = "1.15.0b1" try: import django