summaryrefslogtreecommitdiff
path: root/tests/isolated/hub_fork_simple.py
blob: 96389de4b48812b880a4cbbad7ae15165df858a3 (plain)
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
import os
import signal
import sys
import tempfile
__test__ = False


def parent(signal_path, pid):
    eventlet.Timeout(5)
    port = None
    while True:
        try:
            contents = open(signal_path, 'rb').read()
            port = int(contents.strip())
            break
        except Exception:
            eventlet.sleep(0.1)
    eventlet.connect(('127.0.0.1', port))
    while True:
        try:
            contents = open(signal_path, 'rb').read()
            result = contents.split()[1]
            break
        except Exception:
            eventlet.sleep(0.1)
    assert result == b'done', repr(result)
    print('pass')


def child(signal_path):
    eventlet.Timeout(5)
    s = eventlet.listen(('127.0.0.1', 0))
    with open(signal_path, 'wb') as f:
        f.write(str(s.getsockname()[1]).encode() + b'\n')
        f.flush()
        s.accept()
        f.write(b'done\n')
        f.flush()


if __name__ == '__main__':
    import eventlet

    with tempfile.NamedTemporaryFile() as signal_file:
        signal_path = signal_file.name

    pid = os.fork()
    if pid < 0:
        sys.stderr.write('fork error\n')
        sys.exit(1)
    elif pid == 0:
        child(signal_path)
        sys.exit(0)
    elif pid > 0:
        try:
            parent(signal_path, pid)
        except Exception:
            os.kill(pid, signal.SIGTERM)