File: test_protocol.py

package info (click to toggle)
nutcracker 0.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,192 kB
  • sloc: ansic: 15,221; sh: 5,284; python: 1,230; php: 300; makefile: 130
file content (94 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
from .common import *
from pprint import pprint

def get_conn():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((nc.host(), nc.port()))
    s.settimeout(.3)
    return s

def _test(req, resp, sleep=0):
    s = get_conn()

    # Send a single byte at a time
    for i in req:
        s.sendall(bytes([i]))
        time.sleep(sleep)

    s.settimeout(.3)

    data = s.recv(10000)
    assert_equal(resp, data)

def test_slow():
    req = b'*1\r\n$4\r\nPING\r\n'
    resp = b'+PONG\r\n'

    if large > 1000:
        sleep = 1
    else:
        sleep = .1

    _test(req, resp, sleep)

def test_pingpong():
    req = b'*1\r\n$4\r\nPING\r\n'
    resp = b'+PONG\r\n'
    _test(req, resp)
    # Sanity check there's no error
    info = nc._info_dict()
    assert_equal(0, info['ntest']['client_err'])

# twemproxy for redis doesn't appear to have any code to send +OK\r\n, it just disconnects.
def test_quit():
    req = b'*1\r\n$4\r\nQUIT\r\n'
    # NOTE: Nutcracker doesn't appear to have any code to send +OK\r\n, it just disconnects.
    # +OK\r\n would also be valid.
    resp = b''
    _test(req, resp)

# twemproxy for redis doesn't appear to have any code to send +OK\r\n, it just disconnects.
# If it doesn't try to send anything, there's no client_err.
def test_quit_without_recv():
    req = b'*1\r\n$4\r\nQUIT\r\n'
    resp = b'+OK\r\n'
    s = get_conn()

    s.sendall(req)
    s.close()
    time.sleep(0.1)
    info = nc._info_dict()
    assert_equal(0, info['ntest']['client_err'])

def _test_bad(req):
    s = get_conn()

    s.sendall(req)
    data = s.recv(10000)

    assert_equal(b'', s.recv(1000))  # peer is closed

def test_badreq():
    reqs = [
        # '*1\r\n$3\r\nPING\r\n',
        b'\r\n',
        # '*3abcdefg\r\n',
        b'*3\r\n*abcde\r\n',

        b'*4\r\n$4\r\nMSET\r\n$1\r\nA\r\n$1\r\nA\r\n$1\r\nA\r\n',
        b'*2\r\n$4\r\nMSET\r\n$1\r\nA\r\n',
        # '*3\r\n$abcde\r\n',
        # '*3\r\n$3abcde\r\n',
        # '*3\r\n$3\r\nabcde\r\n',
    ]

    for req in reqs:
        _test_bad(req)


def test_wrong_argc():
    s = get_conn()

    s.sendall(b'*1\r\n$3\r\nGET\r\n')
    assert_equal(b'', s.recv(1000))  # peer is closed