assertions()   F
last analyzed

Complexity

Conditions 25

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 25
c 4
b 0
f 2
dl 0
loc 33
rs 2.6361

How to fix   Complexity   

Complexity

Complex classes like assertions() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import os, re, time, inspect
2
import psutil, requests
3
from process_tests import dump_on_error, setup_coverage, TestProcess, TestSocket, wait_for_strings
4
import django
5
6
TIMEOUT = int(os.getenv('UWSGICACHE_TEST_TIMEOUT', 3))
7
8
9
def assertions(url):
10
    assert requests.get(url + '/get/1').text == 'None'
11
    assert requests.get(url + '/set/1/a').text == 'ok'
12
    assert requests.get(url + '/get/1').text == 'a'
13
    assert requests.get(url + '/clear').text == 'ok'
14
    assert requests.get(url + '/get/1').text == 'None'
15
    assert requests.get(url + '/set/2/b?timeout=3').text == 'ok'
16
    time.sleep(2) # 2 * cache-expire-freq
17
    assert requests.get(url + '/get/2').text == 'b'
18
    time.sleep(3) # 1 + 2 * cache-expire-freq
19
    assert requests.get(url + '/get/2').text == 'None'
20
    assert requests.get(url + '/set/3/c?timeout=0').text == 'ok'
21
    time.sleep(2) # 2 * cache-expire-freq
22
    assert requests.get(url + '/get/3').text == 'None'
23
    assert requests.get(url + '/set/4/d?timeout=None').text == 'ok'
24
    time.sleep(2) # 2 * cache-expire-freq
25
    assert requests.get(url + '/get/4').text == 'd'
26
    # skipping this test for Django 1.6 in test_locmem (Django 1.6 issue?)
27
    if django.VERSION[:2] == (1, 6) and inspect.stack()[1][3] != 'test_locmem':
28
        assert requests.get(url + '/add/4/e').text == 'False'
29
    assert requests.get(url + '/add/5/e').text == 'True'
30
    assert requests.get(url + '/get/5').text == 'e'
31
    assert requests.get(url + '/add/6/f?timeout=3').text == 'True'
32
    time.sleep(2) # 2 * cache-expire-freq
33
    assert requests.get(url + '/get/6').text == 'f'
34
    time.sleep(3) # 1 + 2 * cache-expire-freq
35
    assert requests.get(url + '/get/6').text == 'None'
36
    assert requests.get(url + '/add/7/g?timeout=0').text == 'True'
37
    time.sleep(2) # 2 * cache-expire-freq
38
    assert requests.get(url + '/get/7').text == 'None'
39
    assert requests.get(url + '/add/8/h?timeout=None').text == 'True'
40
    time.sleep(2) # 2 * cache-expire-freq
41
    assert requests.get(url + '/get/8').text == 'h'
42
43
44
def test_uwsgi():
45
    with TestProcess('uwsgi',
46
                     '--http-socket', '127.0.0.1:0',
47
                     '--module', 'test_project.wsgi',
48
                     '--master',
49
                     '--cache-expire-freq', '1',
50
                     '--cache2', 'name=foobar,items=20') as proc:
51
        with dump_on_error(proc.read):
52
            wait_for_strings(proc.read, TIMEOUT, 'bound to TCP address 127.0.0.1')
53
            url, = re.findall(r"bound to TCP address (127.0.0.1:\d+) ", proc.read())
54
            url = "http://" + url
55
            assertions(url)
56
57
58
def get_ports(pid):
59
    def get(proc):
60
        return sum(
61
            (get(p) for p in proc.children()),
62
            [c.laddr[1] for c in proc.connections() if c.status == 'LISTEN' and c.raddr == ()]
63
        )
64
    process = psutil.Process(pid)
65
    for _ in range(50):
66
        sockets = get(process)
67
        if sockets:
68
            return sockets[0]
69
        time.sleep(0.05)
70
71
def test_locmem():
72
    with TestProcess('django-admin.py',
73
                     'runserver', '127.0.0.1:0', '--traceback', '--noreload', '--nothreading',
74
                     env=dict(os.environ, UWSGI_CACHE_FALLBACK='y')) as proc:
75
        with dump_on_error(proc.read):
76
            wait_for_strings(proc.read, TIMEOUT, '127.0.0.1')
77
            port = get_ports(proc.proc.pid)
78
            url = "http://127.0.0.1:%s" % port
79
            assertions(url)
80