Passed
Pull Request — master (#3)
by Guibert
53s
created

test_curiosity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 35.42 %

Importance

Changes 0
Metric Value
wmc 7
eloc 33
dl 17
loc 48
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_run_curio() 17 17 3
A failure_func() 0 3 1
A b_func() 0 3 1
A test_parallele() 0 5 1
A a_func() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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