~hudson-openstack/burrow/trunk

« back to all changes in this revision

Viewing changes to test/__init__.py

  • Committer: Tarmac
  • Author(s): Eric Day
  • Date: 2011-08-24 20:23:03 UTC
  • mfrom: (33.1.7 doc-updates)
  • Revision ID: tarmac-20110824202303-fc09r0aendi1tn6m
Added client API unit tests, moved exceptions to main burrow module, and added more WSGI frontend tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# limitations under the License.
14
14
 
15
15
'''Unittests for burrow.'''
 
16
 
 
17
import atexit
 
18
import os
 
19
import signal
 
20
import sys
 
21
import time
 
22
 
 
23
import burrow
 
24
 
 
25
 
 
26
def start_server():
 
27
    '''Fork and start the server, saving the pid in a file.'''
 
28
    kill_server()
 
29
    pid = os.fork()
 
30
    if pid == 0:
 
31
        try:
 
32
            import coverage
 
33
            cov = coverage.coverage(data_suffix=True)
 
34
            cov.start()
 
35
 
 
36
            def save_coverage(_signum, _frame):
 
37
                '''Callback for signal to save coverage info to file.'''
 
38
                cov.save()
 
39
 
 
40
            signal.signal(signal.SIGUSR1, save_coverage)
 
41
        except ImportError:
 
42
            pass
 
43
        server = burrow.Server(add_default_log_handler=False)
 
44
        server.frontends[0].default_ttl = 0
 
45
        server.run()
 
46
        sys.exit(0)
 
47
    pid_file = open('TestHTTP.pid', 'w')
 
48
    pid_file.write(str(pid))
 
49
    pid_file.close()
 
50
    atexit.register(kill_server)
 
51
    time.sleep(1)
 
52
 
 
53
 
 
54
def kill_server():
 
55
    '''Try killing the server if the pid file exists.'''
 
56
    try:
 
57
        pid_file = open('TestHTTP.pid', 'r')
 
58
        pid = pid_file.read()
 
59
        pid_file.close()
 
60
        try:
 
61
            os.kill(int(pid), signal.SIGUSR1)
 
62
            time.sleep(1)
 
63
            os.kill(int(pid), signal.SIGTERM)
 
64
        except OSError:
 
65
            pass
 
66
        os.unlink('TestHTTP.pid')
 
67
    except IOError:
 
68
        pass
 
69
 
 
70
start_server()