-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
375 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Helpers for Jenkins-agent-k8s-operator charm integration tests.""" | ||
import asyncio | ||
import inspect | ||
import time | ||
import typing | ||
|
||
|
||
async def wait_for( | ||
func: typing.Callable[[], typing.Union[typing.Awaitable, typing.Any]], | ||
timeout: int = 300, | ||
check_interval: int = 10, | ||
) -> typing.Any: | ||
"""Wait for function execution to become truthy. | ||
Args: | ||
func: A callback function to wait to return a truthy value. | ||
timeout: Time in seconds to wait for function result to become truthy. | ||
check_interval: Time in seconds to wait between ready checks. | ||
Raises: | ||
TimeoutError: if the callback function did not return a truthy value within timeout. | ||
Returns: | ||
The result of the function if any. | ||
""" | ||
deadline = time.time() + timeout | ||
is_awaitable = inspect.iscoroutinefunction(func) | ||
while time.time() < deadline: | ||
if is_awaitable: | ||
if result := await func(): | ||
return result | ||
else: | ||
if result := func(): | ||
return result | ||
await asyncio.sleep(check_interval) | ||
|
||
# final check before raising TimeoutError. | ||
if is_awaitable: | ||
if result := await func(): | ||
return result | ||
else: | ||
if result := func(): | ||
return result | ||
raise TimeoutError() |
Oops, something went wrong.