Total Complexity | 7 |
Total Lines | 48 |
Duplicated Lines | 35.42 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | from contextvars import ContextVar |
||
2 | |||
3 | from curio import Kernel, run, sleep |
||
4 | |||
5 | from libellule.control_flow.common import FAILURE |
||
6 | from libellule.control_flow.curiosity import parallele, run as run_curio |
||
7 | |||
8 | |||
9 | async def a_func(): |
||
10 | await sleep(1) |
||
11 | return "a" |
||
12 | |||
13 | |||
14 | async def b_func(): |
||
15 | await sleep(3) |
||
16 | return "b" |
||
17 | |||
18 | |||
19 | async def failure_func(): |
||
20 | await sleep(2) |
||
21 | return FAILURE |
||
22 | |||
23 | |||
24 | def test_parallele(): |
||
25 | assert run(parallele(children=[a_func])) |
||
26 | assert run(parallele(children=[a_func, b_func])) |
||
27 | assert not run(parallele(children=[a_func, b_func, failure_func])) |
||
28 | assert run(parallele(children=[a_func, b_func, failure_func], succes_threshold=2)) |
||
29 | |||
30 | |||
31 | View Code Duplication | def test_run_curio(): |
|
|
|||
32 | counter = ContextVar("counter", default=5) |
||
33 | |||
34 | async def reset_counter(): |
||
35 | if counter.get() == 5: |
||
36 | counter.set(0) |
||
37 | return 0 |
||
38 | return -1 |
||
39 | |||
40 | with Kernel() as kernel: |
||
41 | assert run_curio(kernel, reset_counter) == 0 |
||
42 | assert run_curio(kernel, reset_counter) == 0 |
||
43 | assert counter.get() == 5 |
||
44 | |||
45 | assert run(reset_counter) == 0 |
||
46 | assert run(reset_counter) == -1 |
||
47 | assert counter.get() == 0 |
||
48 |