Skip to content

Commit

Permalink
fix(timeout): Fix bug in using .seconds of timedelta instead of .tota…
Browse files Browse the repository at this point in the history
…l_seconds()
  • Loading branch information
thejaminator committed Dec 20, 2022
1 parent f511c69 commit a368ab4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool]
[tool.poetry]
name = "typesafe_parmap"
version = "1.0.5"
version = "1.0.6"
homepage = "https://github.com/thejaminator/typesafe_parmap"
description = "Run functions in parallel safely with typesafe parmap!."
authors = ["James Chua <[email protected]>"]
Expand Down
20 changes: 20 additions & 0 deletions tests/test_par_map_timeout_n.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ def test_timeout_overall_2():
assert str_result_2 is None


def test_timeout_overall_ms():
# The timeout should be applied overall
# We limit the timeout to 4 ms
# Each function takes 3 ms
# So the parmap should timeout after 4 ms, not 8

executor = ThreadPoolExecutor(1)
start_time = time.time()
str_result_1, str_result_2 = par_map_timeout_2(
lambda: short_running_str("test 1", sleep_seconds=0.003),
lambda: short_running_str("test 2", sleep_seconds=0.003),
executor=executor,
timeout=timedelta(milliseconds=4),
)
end_time = time.time()
total_time = end_time - start_time
assert total_time < 0.005, "The parmap should have timed out after 4 ms"
assert str_result_1 == "test 1"
assert str_result_2 is None

def test_timeout_overall():
# The timeout should be applied to each function
# We limit the timeout to 5 seconds
Expand Down
4 changes: 2 additions & 2 deletions typesafe_parmap/parmap_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def try_future_result(
func_name: str,
func_number: int,
) -> Optional[A]:
seconds_left = timeout_left.seconds
overall_seconds = overall_timeout.seconds
seconds_left = timeout_left.total_seconds()
overall_seconds = overall_timeout.total_seconds()

try:
# Use the seconds_left as the timeout for the future result, not the overall timeout
Expand Down

0 comments on commit a368ab4

Please sign in to comment.