Repository: leshchenko1979/fast_bitrix24
Pull request #193 (2022-07-31 18:24:54)

Fix timeout errors

sourcery-ai[bot]: # Sourcery Code Quality Report

❌ &nbsp;Merging this PR will **decrease** code quality in the affected files by **0.34%**.

| Quality metrics | Before | After | Change |
| --- | :---: | :---: | :---: |
| [Complexity](https://docs.sourcery.ai/GitHub/Quality-Report/#complexity) | 2.56 ⭐ | 2.56 ⭐ | 0.00  |
| [Method Length](https://docs.sourcery.ai/GitHub/Quality-Report/#method-length) | 60.00 ⭐ | 60.38 ⭐ | 0.38 👎 |
| [Working memory](https://docs.sourcery.ai/GitHub/Quality-Report/#working-memory) | 5.64 ⭐ | 5.75 ⭐ | 0.11 👎 |
| **[Quality](https://docs.sourcery.ai/GitHub/Quality-Report/#quality-score)** | **80.82%** ⭐| **80.48%** ⭐| **-0.34%** 👎 |

| Other metrics | Before | After | Change |
| --- | :---: | :---: | :---: |
| Lines | 234 | 240 | 6 |

| Changed files | Quality Before | Quality After | Quality Change |
| --- | :---: | :---: | :---: |
| fast_bitrix24/srh.py | 80.82% ⭐ | 80.48% ⭐ | -0.34% 👎|

Here are some functions in these files that still need a tune-up:

| File | Function | Complexity | Length | Working Memory | Quality | Recommendation |
| --- | --- | :---: | :---: | :---: | :---: | --- |

## Legend and Explanation

The emojis denote the absolute quality of the code:

 - ⭐ excellent
 - 🙂 good
 - 😞 poor
 - ⛔ very poor


The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.

***

Please see our documentation [here](https://docs.sourcery.ai/GitHub/Quality-Report/) for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us [improve](https://research.typeform.com/to/j06Spdfr?type=quality_report&github_login=leshchenko1979&base_repo=https%3A%2F%2Fgithub.com%2Fleshchenko1979%2Ffast_bitrix24.git&base_remote_ref=refs%2Fpull%2F193%2Fhead&base_ref=fix-timeout-exception&base_sha=9cd829e647eecae3a6c7d7142f966f5745505926&head_repo=https%3A%2F%2Fgithub.com%2Fleshchenko1979%2Ffast_bitrix24.git&head_ref=sourcery%2Ffix-timeout-exception&base_pr_number=193&base_pr_state=open) this quality report!
codecov[bot]: # [Codecov](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko) Report
> Merging [#193](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko) (a6eefbc) into [master](https://codecov.io/gh/leshchenko1979/fast_bitrix24/commit/234954a9a948e5b3d5d71023e12b1df1e018aa11?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko) (234954a) will **increase** coverage by `0.80%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #193      +/-   ##
==========================================
+ Coverage   88.17%   88.97%   +0.80%     
==========================================
  Files           9        9              
  Lines         499      499              
==========================================
+ Hits          440      444       +4     
+ Misses         59       55       -4     
```


| [Impacted Files](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko) | Coverage Δ | |
|---|---|---|
| [fast\_bitrix24/srh.py](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko#diff-ZmFzdF9iaXRyaXgyNC9zcmgucHk=) | `87.12% <100.00%> (+3.78%)` | :arrow_up: |
| [fast\_bitrix24/correct\_asyncio.py](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko#diff-ZmFzdF9iaXRyaXgyNC9jb3JyZWN0X2FzeW5jaW8ucHk=) | `75.00% <0.00%> (-25.00%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko). Last update [234954a...a6eefbc](https://codecov.io/gh/leshchenko1979/fast_bitrix24/pull/193?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alexey+Leshchenko).


Patch:
diff --git a/fast_bitrix24/srh.py b/fast_bitrix24/srh.py
index a15ce6a..032fa01 100644
--- a/fast_bitrix24/srh.py
+++ b/fast_bitrix24/srh.py
@@ -1,17 +1,18 @@
 import time
-from asyncio import Event, sleep
+from asyncio import Event, sleep, TimeoutError
 from collections import deque
 from contextlib import asynccontextmanager
 
 import aiohttp
 from aiohttp.client_exceptions import (
+    ClientConnectionError,
     ClientPayloadError,
     ClientResponseError,
-    ServerDisconnectedError,
+    ServerTimeoutError,
 )
 
-from .utils import _url_valid
 from .logger import logger
+from .utils import _url_valid
 
 BITRIX_POOL_SIZE = 50
 BITRIX_RPS = 2.0
@@ -130,7 +131,12 @@ class ServerRequestHandler:
                 self.success()
                 return result
 
-            except (ClientPayloadError, ServerDisconnectedError, ServerError) as err:
+            except (
+                ClientPayloadError,
+                ClientConnectionError,
+                ServerError,
+                TimeoutError,
+            ) as err:
                 self.failure(err)
 
     async def request_attempt(self, method, params=None) -> dict:


Test patch:
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
new file mode 100644
index 0000000..429872b
--- /dev/null
+++ b/tests/test_exceptions.py
@@ -0,0 +1,22 @@
+import asyncio
+import sys
+
+import aiohttp.client_exceptions as exc
+import pytest
+from fast_bitrix24 import Bitrix
+
+
+@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
+@pytest.mark.parametrize("exception", [exc.ServerConnectionError, asyncio.TimeoutError])
+@pytest.mark.asyncio
+async def test_retries(exception):
+    from unittest.mock import AsyncMock
+
+    bitrix = Bitrix("https://google.com/path")
+    srh = bitrix.srh
+
+    srh.request_attempt = AsyncMock(spec=srh.request_attempt, side_effect=exception)
+
+    # должна исчерпать все попытки и выдать RuntimeError
+    with pytest.raises(RuntimeError):
+        await srh.single_request(None)