summaryrefslogtreecommitdiff
path: root/plac/doc/test_server.py
blob: a9904eea35fa0d995d1db584477b0025c380ee74 (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
import multiprocessing, subprocess, time, random
import plac
from ishelve2 import ShelveInterface

i = plac.Interpreter(ShelveInterface(configfile=None))

COMMANDS = ['''\
.help
set a 1
''',
'''\
set b 1
wrong command
showall
''']

def client_send(commands, port):
    time.sleep(.5) # wait a bit for the server to start
    po = subprocess.Popen(['telnet', 'localhost', str(port)],
                          stdin=subprocess.PIPE)
    for cmd in commands.splitlines():
        po.stdin.write(cmd + '\n')
        time.sleep(.1) # wait a bit for the server to answer

def test():
    port = random.choice(range(2000, 20000))
    clients = []
    for cmds in COMMANDS:
        cl = multiprocessing.Process(target=client_send, args=(cmds, port))
        clients.append(cl)
        cl.start()
    i.stop_server(wait=1)
    i.start_server(port, timeout=.1)
    for cl in clients:
        cl.join()