~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-13 11:46:14 UTC
  • mto: (6.1.30 trunk)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: aaron@canonical.com-20120113114614-lw5t8rcn4cidlb3o
Support thread_newest threading.

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
 
12
from urlparse import parse_qs
10
13
 
11
14
from testtools import ExpectedException
12
15
 
13
 
from grackle import client
14
 
 
15
 
 
16
 
class Forked:
17
 
 
18
 
    def __init__(self, func_or_method):
19
 
        self.func_or_method = func_or_method
 
16
from grackle.client import (
 
17
    GrackleClient,
 
18
    UnsupportedOrder,
 
19
    )
 
20
 
 
21
 
 
22
def threaded_messages(messages):
 
23
    threads = {}
 
24
    count = 0
 
25
    pending = []
 
26
    for message in messages:
 
27
        if message.get('in_reply_to') is None:
 
28
            threads[message['message_id']] = [message]
 
29
            count += 1
 
30
        else:
 
31
            pending.append(message)
 
32
    for message in pending:
 
33
        threads[message['in_reply_to']].append(message)
 
34
    return threads.values()
 
35
 
 
36
 
 
37
class ForkedFake:
 
38
 
 
39
    def __init__(self, port, messages=None):
20
40
        self.pid = None
 
41
        self.port = port
 
42
        self.messages = messages
 
43
        self.read_end, self.write_end = os.pipe()
 
44
 
 
45
    def is_ready(self):
 
46
        os.write(self.write_end, 'asdf')
21
47
 
22
48
    def __enter__(self):
23
49
        pid = os.fork()
24
 
        if pid != 0:
25
 
            self.pid = pid
26
 
            return
27
 
        self.func_or_method()
 
50
        if pid == 0:
 
51
            self.start_server()
 
52
        self.pid = pid
 
53
        os.read(self.read_end, 1)
 
54
        return
28
55
 
 
56
    def start_server(self):
 
57
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
 
58
        service.messages = self.messages
 
59
        self.is_ready()
 
60
        service.serve_forever()
29
61
 
30
62
    def __exit__(self, exc_type, exc_val, traceback):
31
63
        os.kill(self.pid, SIGKILL)
32
64
 
33
65
 
 
66
SUPPORTED_ORDERS = set(
 
67
    ['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
 
68
     'thread_subject'])
 
69
 
 
70
 
34
71
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
35
72
 
36
73
    def do_POST(self):
42
79
        else:
43
80
            self.send_error(httplib.BAD_REQUEST)
44
81
 
45
 
 
46
 
def run_service():
47
 
    service = HTTPServer(('', 8435), FakeGrackleRequestHandler)
48
 
    service.serve_forever()
49
 
 
 
82
    def do_GET(self):
 
83
        scheme, netloc, path, params, query_string, fragments = (
 
84
            urlparse(self.path))
 
85
        archive = os.path.split(path)[1]
 
86
        query = parse_qs(query_string)
 
87
        parameters = simplejson.loads(query['parameters'][0])
 
88
        order = parameters.get('order')
 
89
        messages = self.server.messages[archive]
 
90
        if order is not None :
 
91
            if order not in SUPPORTED_ORDERS:
 
92
                self.send_response(httplib.BAD_REQUEST)
 
93
                self.wfile.write('Unsupported order')
 
94
                return
 
95
            elif order.startswith('thread_'):
 
96
                threaded = threaded_messages(messages)
 
97
                messages = []
 
98
                if order == 'thread_subject':
 
99
                    threaded.sort(key=lambda t: t[0]['subject'])
 
100
                if order == 'thread_oldest':
 
101
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
 
102
                if order == 'thread_newest':
 
103
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
 
104
                for thread in threaded:
 
105
                    messages.extend(thread)
 
106
            else:
 
107
                messages.sort(key=lambda m: m[order])
 
108
        messages = [m for m in messages
 
109
                    if 'message_ids' not in parameters or
 
110
                    m['message_id'] in parameters['message_ids']]
 
111
        self.send_response(httplib.OK)
 
112
        self.end_headers()
 
113
        limit = parameters.get('limit', 100)
 
114
        memo = parameters.get('memo')
 
115
        message_id_indices = dict(
 
116
            (m['message_id'], idx) for idx, m in enumerate(messages))
 
117
        if memo is None:
 
118
            start = 0
 
119
        else:
 
120
            start = message_id_indices[memo.encode('rot13')]
 
121
        if start > 0:
 
122
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
 
123
        else:
 
124
            previous_memo = None
 
125
        end = min(start + limit, len(messages))
 
126
        if end < len(messages):
 
127
            next_memo = messages[end]['message_id'].encode('rot13')
 
128
        else:
 
129
            next_memo = None
 
130
        messages = messages[start:end]
 
131
        response = {
 
132
            'messages': messages,
 
133
            'next_memo': next_memo,
 
134
            'previous_memo': previous_memo
 
135
            }
 
136
        self.wfile.write(simplejson.dumps(response))
 
137
 
 
138
 
 
139
def fake_grackle_service(client, messages=None):
 
140
    if messages is None:
 
141
        messages = {}
 
142
    return ForkedFake(client.port, messages)
50
143
 
51
144
 
52
145
class TestPutMessage(TestCase):
53
146
 
54
147
    def test_put_message(self):
55
 
        with Forked(run_service):
56
 
            client.put_message('arch1', StringIO('This is a message'))
 
148
        client = GrackleClient('localhost', 8436)
 
149
        with fake_grackle_service(client):
 
150
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
57
151
            with ExpectedException(Exception, 'wtf'):
58
 
                client.put_message('arch1', StringIO('This is not a message'))
 
152
                client.put_message('arch1', 'asdf',
 
153
                    StringIO('This is not a message'))
 
154
 
 
155
 
 
156
class TestGetMessages(TestCase):
 
157
 
 
158
    def assertIDOrder(self, ids, messages):
 
159
        self.assertEqual(ids, [m['message_id'] for m in messages])
 
160
 
 
161
    def assertMessageIDs(self, ids, messages):
 
162
        self.assertIDOrder(
 
163
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))
 
164
 
 
165
    def test_get_messages(self):
 
166
        client = GrackleClient('localhost', 8435)
 
167
        with fake_grackle_service(client,
 
168
            {'baz':
 
169
            [{'message_id': 'foo'},
 
170
             {'message_id': 'bar'}]}):
 
171
            response = client.get_messages('baz')
 
172
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
 
173
            response['messages']))
 
174
        self.assertIs(None, response['next_memo'])
 
175
        self.assertIs(None, response['previous_memo'])
 
176
 
 
177
    def test_get_messages_by_id(self):
 
178
        client = GrackleClient('localhost', 8437)
 
179
        with fake_grackle_service(client,
 
180
            {'baz':
 
181
            [{'message_id': 'foo'},
 
182
             {'message_id': 'bar'}]}):
 
183
            response = client.get_messages('baz', message_ids=['foo'])
 
184
        message, = response['messages']
 
185
        self.assertEqual('foo', message['message_id'])
 
186
 
 
187
    def test_get_messages_batching(self):
 
188
        client = GrackleClient('localhost', 8438)
 
189
        with fake_grackle_service(client,
 
190
            {'baz':
 
191
            [{'message_id': 'foo'},
 
192
             {'message_id': 'bar'}]}):
 
193
            response = client.get_messages('baz', limit=1)
 
194
            self.assertEqual(1, len(response['messages']))
 
195
            messages = response['messages']
 
196
            response = client.get_messages(
 
197
                'baz', limit=1, memo=response['next_memo'])
 
198
            self.assertEqual(1, len(response['messages']))
 
199
            messages.extend(response['messages'])
 
200
            self.assertMessageIDs(['foo', 'bar'], messages)
 
201
 
 
202
    def get_messages_member_order_test(self, key):
 
203
        client = GrackleClient('localhost', 8439)
 
204
        with fake_grackle_service(client,
 
205
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
 
206
                 {'message_id': 'bar', key: '2011-03-24'}]}):
 
207
            response = client.get_messages('baz')
 
208
            self.assertIDOrder(['foo', 'bar'], response['messages'])
 
209
            response = client.get_messages('baz', order=key)
 
210
            self.assertIDOrder(['bar', 'foo'], response['messages'])
 
211
 
 
212
    def test_get_messages_date_order(self):
 
213
        self.get_messages_member_order_test('date')
 
214
 
 
215
    def test_get_messages_author_order(self):
 
216
        self.get_messages_member_order_test('author')
 
217
 
 
218
    def test_get_messages_subject_order(self):
 
219
        self.get_messages_member_order_test('subject')
 
220
 
 
221
    def test_get_messages_thread_subject_order(self):
 
222
        client = GrackleClient('localhost', 8439)
 
223
        with fake_grackle_service(client, {'baz': [
 
224
            {'message_id': 'bar', 'subject': 'y'},
 
225
            {'message_id': 'qux', 'subject': 'z'},
 
226
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
 
227
            ]}):
 
228
            response = client.get_messages('baz')
 
229
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
 
230
            response = client.get_messages('baz', order='subject')
 
231
            self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
 
232
            response = client.get_messages('baz', order='thread_subject')
 
233
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
 
234
 
 
235
    def test_get_messages_thread_oldest_order(self):
 
236
        client = GrackleClient('localhost', 8439)
 
237
        with fake_grackle_service(client, {'baz': [
 
238
            {'message_id': 'bar', 'date': 'x'},
 
239
            {'message_id': 'qux', 'date': 'z'},
 
240
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
 
241
            ]}):
 
242
            response = client.get_messages('baz')
 
243
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
 
244
            response = client.get_messages('baz', order='date')
 
245
            self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
 
246
            response = client.get_messages('baz', order='thread_oldest')
 
247
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
 
248
 
 
249
    def test_get_messages_thread_newest_order(self):
 
250
        client = GrackleClient('localhost', 8439)
 
251
        with fake_grackle_service(client, {'baz': [
 
252
            {'message_id': 'bar', 'date': 'x'},
 
253
            {'message_id': 'qux', 'date': 'w'},
 
254
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
 
255
            {'message_id': 'baz', 'date': 'z', 'in_reply_to': 'qux'},
 
256
            ]}):
 
257
            response = client.get_messages('baz', order='date')
 
258
            self.assertIDOrder(
 
259
                ['qux', 'bar', 'foo', 'baz'], response['messages'])
 
260
            response = client.get_messages('baz', order='thread_newest')
 
261
            self.assertIDOrder(
 
262
                ['bar', 'foo', 'qux', 'baz'], response['messages'])
 
263
 
 
264
    def test_get_messages_unsupported_order(self):
 
265
        client = GrackleClient('localhost', 8439)
 
266
        with fake_grackle_service(client,
 
267
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
 
268
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
 
269
            with ExpectedException(UnsupportedOrder):
 
270
                client.get_messages('baz', order='nonsense')