11
11
from testtools import ExpectedException
13
from grackle import client
13
from grackle.client import (
18
def __init__(self, func_or_method):
20
def __init__(self, func_or_method, *args):
19
21
self.func_or_method = func_or_method
22
25
def __enter__(self):
30
self.func_or_method(*self.args)
30
33
def __exit__(self, exc_type, exc_val, traceback):
43
46
self.send_error(httplib.BAD_REQUEST)
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()
55
def fake_grackle_service(client, messages=None):
58
return Forked(run_service, client.port, messages)
52
61
class TestPutMessage(TestCase):
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'))
73
class TestGetMessages(TestCase):
75
def test_get_messages(self):
76
client = GrackleClient('localhost', 8435)
77
with fake_grackle_service(client,
79
[{'message-id': 'foo'},
80
{'message-id': 'bar'}]}):
81
response = client.get_messages('baz')
82
self.assertEqual(['bar', 'foo'], sorted(response.keys()))