~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 11:18:15 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120111111815-0ecv9fihdao2u68o
Start working on GET.

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):
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, messages):
 
50
    service = HTTPServer(('', port), FakeGrackleRequestHandler)
 
51
    service.messages = messages
48
52
    service.serve_forever()
49
53
 
50
54
 
 
55
def fake_grackle_service(client, messages=None):
 
56
    if messages is None:
 
57
        messages = {}
 
58
    return Forked(run_service, client.port, messages)
 
59
 
51
60
 
52
61
class TestPutMessage(TestCase):
53
62
 
54
63
    def test_put_message(self):
55
 
        with Forked(run_service):
56
 
            client.put_message('arch1', StringIO('This is a message'))
 
64
        client = GrackleClient('localhost', 8435)
 
65
        with fake_grackle_service(client):
 
66
            import time; time.sleep(0.5)
 
67
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
68
            with ExpectedException(Exception, 'wtf'):
58
 
                client.put_message('arch1', StringIO('This is not a message'))
 
69
                client.put_message('arch1', 'asdf',
 
70
                    StringIO('This is not a message'))
 
71
 
 
72
 
 
73
class TestGetMessages(TestCase):
 
74
 
 
75
    def test_get_messages(self):
 
76
        client = GrackleClient('localhost', 8435)
 
77
        with fake_grackle_service(client,
 
78
            {'baz':
 
79
            [{'message-id': 'foo'},
 
80
             {'message-id': 'bar'}]}):
 
81
            response = client.get_messages('baz')
 
82
        self.assertEqual(['bar', 'foo'], sorted(response.keys()))