PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/web/test_main.py

https://bitbucket.org/prologic/circuits/
Python | 52 lines | 34 code | 17 blank | 1 comment | 3 complexity | 440737609c1b8b111469ff1c45f357bb MD5 | raw file
 1#!/usr/bin/env python
 2
 3from time import sleep
 4from threading import Thread
 5from errno import ECONNREFUSED
 6from subprocess import Popen, PIPE
 7
 8from .helpers import urlopen, URLError, HTTPError
 9
10SERVER_CMD = ["python", "-m", "circuits.web.main"]
11
12
13class Server(Thread):
14
15    def __init__(self):
16        super(Server, self).__init__()
17
18        self.setDaemon(True)
19
20    def run(self):
21        self.process = Popen(SERVER_CMD)
22
23    def stop(self):
24        self.process.terminate()
25        self.process.wait()
26
27
28def test():
29    server = Server()
30    server.start()
31
32    sleep(1)
33
34    f = None
35
36    for _ in range(3):
37        try:
38            f = urlopen("http://127.0.0.1:8000/hello")
39        except HTTPError as e:
40            raise AssertionError(e)
41        except URLError as e:
42            if e.args[0][0] == ECONNREFUSED:
43                sleep(1)
44            else:
45                raise AssertionError(e)
46
47    assert f
48
49    s = f.read()
50    assert s == b"Hello World!"
51
52    server.stop()