Skip to content

Commit 3c97c40

Browse files
authored
chore: eager connection should fail fast (#445)
* chore: eager connection should fail fast * add some tests
1 parent 3b14375 commit 3c97c40

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/momento/internal/aio/_scs_grpc_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ async def eagerly_connect(self, timeout_seconds: float) -> None:
9191
await asyncio.wait_for(self.wait_for_ready(), timeout_seconds)
9292
except Exception as error:
9393
self._logger.debug(f"Failed to connect to the server within the given timeout. {error}")
94+
raise RuntimeError(
95+
f"Failed to connect to Momento's server within given eager connection timeout {error}"
96+
) from error
9497

9598
async def wait_for_ready(self) -> None:
9699
latest_state = self._secure_channel.get_state(True) # try_to_connect

src/momento/internal/synchronous/_scs_grpc_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def on_timeout() -> None:
9797
)
9898
# the subscription is no longer needed; it was only meant to watch if we could connect eagerly
9999
self._secure_channel.unsubscribe(on_state_change)
100+
raise RuntimeError("Failed to connect to Momento's server within given eager connection timeout")
100101

101102
"""
102103
A callback that is triggered whenever a connection's state changes. We explicitly subscribe to

tests/momento/cache_client/test_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,12 @@ def test_init_throws_exception_when_client_uses_zero_request_timeout_ms(
4343
with pytest.raises(InvalidArgumentException, match="Request timeout must be a positive amount of time."):
4444
configuration = configuration.with_client_timeout(timedelta(seconds=0))
4545
CacheClient(configuration, credential_provider, default_ttl_seconds)
46+
47+
48+
def test_init_eagerly_connecting_cache_client(
49+
configuration: Configuration, credential_provider: CredentialProvider, default_ttl_seconds: timedelta
50+
) -> None:
51+
client = CacheClient.create(
52+
configuration, credential_provider, default_ttl_seconds, eager_connection_timeout=timedelta(seconds=30)
53+
)
54+
assert client

tests/momento/cache_client/test_init_async.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ async def test_init_throws_exception_when_client_uses_zero_request_timeout_ms(
4545
with pytest.raises(InvalidArgumentException, match="Request timeout must be a positive amount of time."):
4646
configuration = configuration.with_client_timeout(timedelta(seconds=0))
4747
CacheClientAsync(configuration, credential_provider, default_ttl_seconds)
48+
49+
50+
async def test_init_eagerly_connecting_cache_client(
51+
configuration: Configuration, credential_provider: CredentialProvider, default_ttl_seconds: timedelta
52+
) -> None:
53+
client = await CacheClientAsync.create(
54+
configuration, credential_provider, default_ttl_seconds, eager_connection_timeout=timedelta(seconds=30)
55+
)
56+
assert client

0 commit comments

Comments
 (0)