File: test_callback.py

package info (click to toggle)
python-pytest-xprocess 0.22.2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 288 kB
  • sloc: python: 698; makefile: 34; sh: 10
file content (47 lines) | stat: -rw-r--r-- 1,620 bytes parent folder | download | duplicates (2)
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
import socket
import sys
from pathlib import Path

import pytest

from xprocess import ProcessStarter

server_path = Path(__file__).parent.joinpath("server.py").absolute()


@pytest.mark.parametrize("proc_name", ["s1", "s2", "s3"])
def test_callback_success(xprocess, tcp_port, proc_name):
    class Starter(ProcessStarter):
        pattern = "started"
        args = [sys.executable, server_path, tcp_port, "--no-children"]

        def startup_check(self):
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                sock.connect(("localhost", tcp_port))
                sock.sendall(bytes("bacon\n", "utf-8"))
                received = str(sock.recv(1024), "utf-8")
                return received == "bacon\n".upper()

    xprocess.ensure(proc_name, Starter)
    info = xprocess.getinfo(proc_name)
    assert info.isrunning()
    info.terminate()


@pytest.mark.parametrize("proc_name", ["s1", "s2", "s3"])
def test_callback_fail(xprocess, tcp_port, proc_name):
    class Starter(ProcessStarter):
        timeout = 5
        pattern = "started"
        args = [sys.executable, server_path, tcp_port, "--no-children"]

        def startup_check(self):
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                sock.connect(("localhost", tcp_port))
                sock.sendall(bytes("bacon\n", "utf-8"))
                received = str(sock.recv(1024), "utf-8")
                return received == "wrong"  # this wont match

    with pytest.raises(TimeoutError):
        xprocess.ensure(proc_name, Starter)
    xprocess.getinfo(proc_name).terminate()