~unity-2d-team/unity-2d/Shell-MultiMonitor

« back to all changes in this revision

Viewing changes to grackle/tests/test_client.py

  • Committer: Aaron Bentley
  • Date: 2012-01-11 08:06:03 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120111080603-fxgo006hthzc89kq
check is PHONY

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
from testtools import ExpectedException
12
12
 
13
 
from grackle import client
 
13
from grackle.client import (
 
14
    GrackleClient,
 
15
    )
14
16
 
15
17
 
16
18
class Forked:
17
19
 
18
 
    def __init__(self, func_or_method):
 
20
    def __init__(self, func_or_method, *args):
19
21
        self.func_or_method = func_or_method
20
22
        self.pid = None
 
23
        self.args = args
21
24
 
22
25
    def __enter__(self):
23
26
        pid = os.fork()
24
27
        if pid != 0:
25
28
            self.pid = pid
26
29
            return
27
 
        self.func_or_method()
 
30
        self.func_or_method(*self.args)
28
31
 
29
32
 
30
33
    def __exit__(self, exc_type, exc_val, traceback):
33
36
 
34
37
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
35
38
 
36
 
    def do_POST(self):
 
39
    def do_PUT(self):
37
40
        message = self.rfile.read(int(self.headers['content-length']))
38
41
        if message == 'This is a message':
39
42
            self.send_response(httplib.CREATED)
43
46
            self.send_error(httplib.BAD_REQUEST)
44
47
 
45
48
 
46
 
def run_service():
47
 
    service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
 
49
def run_service(port):
 
50
    service = HTTPServer(('', port), FakeGrackleRequestHandler)
48
51
    service.serve_forever()
49
52
 
50
53
 
52
55
class TestPutMessage(TestCase):
53
56
 
54
57
    def test_put_message(self):
55
 
        with Forked(run_service):
56
 
            client.put_message('arch1', StringIO('This is a message'))
 
58
        client = GrackleClient('localhost', 8435)
 
59
        with Forked(run_service, client.port):
 
60
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
61
            with ExpectedException(Exception, 'wtf'):
58
 
                client.put_message('arch1', StringIO('This is not a message'))
 
62
                client.put_message('arch1', 'asdf',
 
63
                    StringIO('This is not a message'))