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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from BaseHTTPServer import (
    HTTPServer,
    BaseHTTPRequestHandler,
    )
import httplib
import logging
import os
from signal import SIGKILL
import simplejson
from StringIO import StringIO
import sys
from unittest import TestCase
from urlparse import urlparse
from urlparse import parse_qs

from testtools import ExpectedException

from grackle.client import (
    GrackleClient,
    UnsupportedOrder,
    )


def threaded_messages(messages):
    threads = {}
    count = 0
    pending = []
    for message in messages:
        if message.get('in_reply_to') is None:
            threads[message['message_id']] = [message]
            count += 1
        else:
            pending.append(message)
    for message in pending:
        threads[message['in_reply_to']].append(message)
    return threads.values()


class GrackleStore:

    def __init__(self, messages):
        self.messages = messages

    def get_messages(self, archive_id, query_string):
        query = parse_qs(query_string)
        parameters = simplejson.loads(query['parameters'][0])
        order = parameters.get('order')
        messages = self.messages[archive_id]
        if order is not None :
            if order not in SUPPORTED_ORDERS:
                raise UnsupportedOrder
            elif order.startswith('thread_'):
                threaded = threaded_messages(messages)
                messages = []
                if order == 'thread_subject':
                    threaded.sort(key=lambda t: t[0]['subject'])
                if order == 'thread_oldest':
                    threaded.sort(key=lambda t: min(m['date'] for m in t))
                if order == 'thread_newest':
                    threaded.sort(key=lambda t: max(m['date'] for m in t))
                for thread in threaded:
                    messages.extend(thread)
            else:
                messages.sort(key=lambda m: m[order])
        new_messages = []
        for message in messages:
            if (
                not parameters['include_hidden']
                and message.get('hidden', False)):
                continue

            if ('message_ids' in parameters and
                message['message_id'] not in parameters['message_ids']):
                continue
            message = dict(message)
            if 'headers' in parameters:
                headers = dict(
                    (k, v) for k, v in message['headers'].iteritems()
                    if k in parameters['headers'])
                message['headers'] = headers
            max_body = parameters.get('max_body_length')
            if max_body is not None:
                message['body'] = message['body'][:max_body]
            new_messages.append(message)
        messages = new_messages
        limit = parameters.get('limit', 100)
        memo = parameters.get('memo')
        message_id_indices = dict(
            (m['message_id'], idx) for idx, m in enumerate(messages))
        if memo is None:
            start = 0
        else:
            start = message_id_indices[memo.encode('rot13')]
        if start > 0:
            previous_memo = messages[start - 1]['message_id'].encode('rot13')
        else:
            previous_memo = None
        end = min(start + limit, len(messages))
        if end < len(messages):
            next_memo = messages[end]['message_id'].encode('rot13')
        else:
            next_memo = None
        messages = messages[start:end]

        response = {
            'messages': messages,
            'next_memo': next_memo,
            'previous_memo': previous_memo
            }
        return response



class ForkedFake:

    def __init__(self, port, messages=None, write_logs=False):
        self.pid = None
        self.port = port
        if messages is None:
            self.messages = {}
        else:
            self.messages = messages
        self.read_end, self.write_end = os.pipe()
        self.write_logs = write_logs

    @staticmethod
    def from_client(client, messages=None):
        return ForkedFake(client.port, messages)

    def is_ready(self):
        os.write(self.write_end, 'asdf')

    def __enter__(self):
        pid = os.fork()
        if pid == 0:
            self.start_server()
        self.pid = pid
        os.read(self.read_end, 1)
        return

    def start_server(self):
        service = HTTPServer(('', self.port), FakeGrackleRequestHandler)
        service.store = GrackleStore(self.messages)
        for archive_id, messages in service.store.messages.iteritems():
            for message in messages:
                message.setdefault('headers', {})
        self.is_ready()
        if self.write_logs:
            logging.basicConfig(
                stream=sys.stderr, level=logging.INFO)
        service.serve_forever()

    def __exit__(self, exc_type, exc_val, traceback):
        os.kill(self.pid, SIGKILL)


SUPPORTED_ORDERS = set(
    ['date', 'author', 'subject', 'thread_newest', 'thread_oldest',
     'thread_subject'])


class FakeGrackleRequestHandler(BaseHTTPRequestHandler):

    def __init__(self, *args, **kwargs):
        self.logger = logging.getLogger('http')
        BaseHTTPRequestHandler.__init__(self, *args, **kwargs)

    def do_POST(self):
        message = self.rfile.read(int(self.headers['content-length']))
        if message == 'This is a message':
            self.send_response(httplib.CREATED)
            self.end_headers()
            self.wfile.close()
        else:
            self.send_error(httplib.BAD_REQUEST)

    def do_GET(self):
        scheme, netloc, path, params, query_string, fragments = (
            urlparse(self.path))
        parts = path.split('/')
        if parts[1] == 'archive':
            try:
                response = self.server.store.get_messages(
                    parts[2], query_string)
                self.send_response(httplib.OK)
                self.end_headers()
                self.wfile.write(simplejson.dumps(response))
            except UnsupportedOrder:
                self.send_response(httplib.BAD_REQUEST)
                self.wfile.write('Unsupported order')
                return

    def log_message(self, format, *args):
        message = "%s - - [%s] %s\n" % (
            self.address_string(), self.log_date_time_string(), format%args)
        self.logger.info(message)


class TestPutMessage(TestCase):

    def test_put_message(self):
        client = GrackleClient('localhost', 8436)
        with ForkedFake.from_client(client):
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
            with ExpectedException(Exception, 'wtf'):
                client.put_message('arch1', 'asdf',
                    StringIO('This is not a message'))


class TestGetMessages(TestCase):

    def assertIDOrder(self, ids, messages):
        self.assertEqual(ids, [m['message_id'] for m in messages])

    def assertMessageIDs(self, ids, messages):
        self.assertIDOrder(
            sorted(ids), sorted(messages, key=lambda m:m['message_id']))

    def test_get_messages(self):
        client = GrackleClient('localhost', 8435)
        with ForkedFake.from_client(client,
            {'baz':
            [{'message_id': 'foo'},
             {'message_id': 'bar'}]}):
            response = client.get_messages('baz')
        self.assertEqual(['bar', 'foo'], sorted(m['message_id'] for m in
            response['messages']))
        self.assertIs(None, response['next_memo'])
        self.assertIs(None, response['previous_memo'])

    def test_get_messages_by_id(self):
        client = GrackleClient('localhost', 8437)
        with ForkedFake.from_client(client,
            {'baz':
            [{'message_id': 'foo'},
             {'message_id': 'bar'}]}):
            response = client.get_messages('baz', message_ids=['foo'])
        message, = response['messages']
        self.assertEqual('foo', message['message_id'])

    def test_get_messages_batching(self):
        client = GrackleClient('localhost', 8438)
        with ForkedFake.from_client(client,
            {'baz':
            [{'message_id': 'foo'},
             {'message_id': 'bar'}]}):
            response = client.get_messages('baz', limit=1)
            self.assertEqual(1, len(response['messages']))
            messages = response['messages']
            response = client.get_messages(
                'baz', limit=1, memo=response['next_memo'])
            self.assertEqual(1, len(response['messages']))
            messages.extend(response['messages'])
            self.assertMessageIDs(['foo', 'bar'], messages)

    def get_messages_member_order_test(self, key):
        client = GrackleClient('localhost', 8439)
        with ForkedFake.from_client(client,
                {'baz': [{'message_id': 'foo', key: '2011-03-25'},
                 {'message_id': 'bar', key: '2011-03-24'}]}):
            response = client.get_messages('baz')
            self.assertIDOrder(['foo', 'bar'], response['messages'])
            response = client.get_messages('baz', order=key)
            self.assertIDOrder(['bar', 'foo'], response['messages'])

    def test_get_messages_date_order(self):
        self.get_messages_member_order_test('date')

    def test_get_messages_author_order(self):
        self.get_messages_member_order_test('author')

    def test_get_messages_subject_order(self):
        self.get_messages_member_order_test('subject')

    def test_get_messages_thread_subject_order(self):
        client = GrackleClient('localhost', 8439)
        with ForkedFake.from_client(client, {'baz': [
            {'message_id': 'bar', 'subject': 'y'},
            {'message_id': 'qux', 'subject': 'z'},
            {'message_id': 'foo', 'subject': 'x', 'in_reply_to': 'qux'},
            ]}):
            response = client.get_messages('baz')
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
            response = client.get_messages('baz', order='subject')
            self.assertIDOrder(['foo', 'bar', 'qux'], response['messages'])
            response = client.get_messages('baz', order='thread_subject')
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])

    def test_get_messages_thread_oldest_order(self):
        client = GrackleClient('localhost', 8439)
        with ForkedFake.from_client(client, {'baz': [
            {'message_id': 'bar', 'date': 'x'},
            {'message_id': 'qux', 'date': 'z'},
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'qux'},
            ]}):
            response = client.get_messages('baz')
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])
            response = client.get_messages('baz', order='date')
            self.assertIDOrder(['bar', 'foo', 'qux'], response['messages'])
            response = client.get_messages('baz', order='thread_oldest')
            self.assertIDOrder(['bar', 'qux', 'foo'], response['messages'])

    def test_get_messages_thread_newest_order(self):
        client = GrackleClient('localhost', 8439)
        with ForkedFake.from_client(client, {'baz': [
            {'message_id': 'bar', 'date': 'x'},
            {'message_id': 'qux', 'date': 'w'},
            {'message_id': 'foo', 'date': 'y', 'in_reply_to': 'bar'},
            {'message_id': 'baz', 'date': 'z', 'in_reply_to': 'qux'},
            ]}):
            response = client.get_messages('baz', order='date')
            self.assertIDOrder(
                ['qux', 'bar', 'foo', 'baz'], response['messages'])
            response = client.get_messages('baz', order='thread_newest')
            self.assertIDOrder(
                ['bar', 'foo', 'qux', 'baz'], response['messages'])

    def test_get_messages_unsupported_order(self):
        client = GrackleClient('localhost', 8439)
        with ForkedFake.from_client(client,
                {'baz': [{'message_id': 'foo', 'date': '2011-03-25'},
                 {'message_id': 'bar', 'date': '2011-03-24'}]}):
            with ExpectedException(UnsupportedOrder):
                client.get_messages('baz', order='nonsense')

    def test_get_messages_headers_no_headers(self):
        client = GrackleClient('localhost', 8440)
        with ForkedFake.from_client(client,
            {'baz': [
                {'message_id': 'foo'}
            ]}):
            response = client.get_messages('baz', headers=[
                'Subject', 'Date', 'X-Launchpad-Message-Rationale'])
        first_message = response['messages'][0]
        self.assertEqual('foo', first_message['message_id'])
        self.assertEqual({}, first_message['headers'])

    def test_get_messages_headers_exclude_headers(self):
        client = GrackleClient('localhost', 8441)
        with ForkedFake.from_client(client,
            {'baz': [
                {'message_id': 'foo', 'headers': {'From': 'me'}}
            ]}):
            response = client.get_messages('baz', headers=[
                'Subject', 'Date', 'X-Launchpad-Message-Rationale'])
        first_message = response['messages'][0]
        self.assertEqual('foo', first_message['message_id'])
        self.assertEqual({}, first_message['headers'])

    def test_get_messages_headers_include_headers(self):
        client = GrackleClient('localhost', 8442)
        with ForkedFake.from_client(client,
            {'baz': [
                {'message_id': 'foo', 'headers': {'From': 'me', 'To': 'you'}}
            ]}):
            response = client.get_messages('baz', headers=[
                'From', 'To'])
        first_message = response['messages'][0]
        self.assertEqual('foo', first_message['message_id'])
        self.assertEqual({'From': 'me', 'To': 'you'}, first_message['headers'])

    def test_get_messages_max_body_length(self):
        client = GrackleClient('localhost', 8443)
        with ForkedFake.from_client(client,
            {'baz': [
                {'message_id': 'foo', 'body': u'abcdefghi'}
            ]}):
            response = client.get_messages('baz', max_body_length=3)
        first_message = response['messages'][0]
        self.assertEqual('abc', first_message['body'])

    def test_include_hidden(self):
        client = GrackleClient('localhost', 8444)
        with ForkedFake.from_client(client,
            {'baz': [
                {'message_id': 'foo', 'hidden': True},
                {'message_id': 'bar', 'hidden': False}
            ]}):
            response = client.get_messages('baz', include_hidden=True)
            self.assertMessageIDs(['bar', 'foo'], response['messages'])
            response = client.get_messages('baz', include_hidden=False)
            self.assertMessageIDs(['bar'], response['messages'])