~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 13:37:23 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120111133723-cv7qfdgpd4zpk561
Cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import httplib
6
6
import os
7
7
from signal import SIGKILL
 
8
import simplejson
8
9
from StringIO import StringIO
9
10
from unittest import TestCase
 
11
from urlparse import urlparse
10
12
 
11
13
from testtools import ExpectedException
12
14
 
13
15
from grackle.client import (
14
16
    GrackleClient,
15
 
    message_path,
16
17
    )
17
18
 
18
19
 
19
 
class Forked:
 
20
class ForkedFake:
20
21
 
21
 
    def __init__(self, func_or_method, *args):
22
 
        self.func_or_method = func_or_method
 
22
    def __init__(self, port, messages=None):
23
23
        self.pid = None
24
 
        self.args = args
 
24
        self.port = port
 
25
        self.messages = messages
 
26
        self.read_end, self.write_end = os.pipe()
 
27
 
 
28
    def is_ready(self):
 
29
        os.write(self.write_end, 'asdf')
25
30
 
26
31
    def __enter__(self):
27
32
        pid = os.fork()
28
 
        if pid != 0:
29
 
            self.pid = pid
30
 
            return
31
 
        self.func_or_method(*self.args)
 
33
        if pid == 0:
 
34
            self.start_server()
 
35
        self.pid = pid
 
36
        os.read(self.read_end, 1)
 
37
        return
32
38
 
 
39
    def start_server(self):
 
40
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
 
41
        service.messages = self.messages
 
42
        self.is_ready()
 
43
        service.serve_forever()
33
44
 
34
45
    def __exit__(self, exc_type, exc_val, traceback):
35
46
        os.kill(self.pid, SIGKILL)
37
48
 
38
49
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
39
50
 
40
 
    def do_PUT(self):
 
51
    def do_POST(self):
41
52
        message = self.rfile.read(int(self.headers['content-length']))
42
53
        if message == 'This is a message':
43
54
            self.send_response(httplib.CREATED)
46
57
        else:
47
58
            self.send_error(httplib.BAD_REQUEST)
48
59
 
49
 
 
50
 
def run_service(port):
51
 
    service = HTTPServer(('', port), FakeGrackleRequestHandler)
52
 
    service.serve_forever()
53
 
 
 
60
    def do_GET(self):
 
61
        scheme, netloc, path, params, query, fragments = urlparse(self.path)
 
62
        archive = os.path.split(path)[1]
 
63
        self.send_response(httplib.OK)
 
64
        self.end_headers()
 
65
        self.wfile.write(simplejson.dumps(self.server.messages[archive]))
 
66
 
 
67
 
 
68
def fake_grackle_service(client, messages=None):
 
69
    if messages is None:
 
70
        messages = {}
 
71
    return ForkedFake(client.port, messages)
54
72
 
55
73
 
56
74
class TestPutMessage(TestCase):
57
75
 
58
76
    def test_put_message(self):
59
 
        client = GrackleClient('localhost', 8435)
60
 
        with Forked(run_service, client.port):
 
77
        client = GrackleClient('localhost', 8436)
 
78
        with fake_grackle_service(client):
61
79
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
62
80
            with ExpectedException(Exception, 'wtf'):
63
81
                client.put_message('arch1', 'asdf',
64
82
                    StringIO('This is not a message'))
65
83
 
66
84
 
67
 
class TestMessagePath(TestCase):
 
85
class TestGetMessages(TestCase):
68
86
 
69
 
    def test_message_path(self):
70
 
        self.assertEqual('/my%20/%3C', message_path('my ', '<'))
 
87
    def test_get_messages(self):
 
88
        client = GrackleClient('localhost', 8435)
 
89
        with fake_grackle_service(client,
 
90
            {'baz':
 
91
            [{'message-id': 'foo'},
 
92
             {'message-id': 'bar'}]}):
 
93
            response = client.get_messages('baz', message_ids=['a'])
 
94
        self.assertEqual(['bar', 'foo'], sorted(m['message-id'] for m in
 
95
            response))