~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-10 11:16:56 UTC
  • Revision ID: aaron@canonical.com-20120110111656-9daoyj766z8r184a
Add test framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from BaseHTTPServer import (
2
 
    HTTPServer,
3
 
    BaseHTTPRequestHandler,
4
 
    )
5
 
import httplib
6
 
import os
7
 
from signal import SIGKILL
8
 
from StringIO import StringIO
9
1
from unittest import TestCase
10
2
 
11
 
from testtools import ExpectedException
12
 
 
13
 
from grackle.client import (
14
 
    GrackleClient,
15
 
    )
16
 
 
17
 
 
18
 
class Forked:
19
 
 
20
 
    def __init__(self, func_or_method, *args):
21
 
        self.func_or_method = func_or_method
22
 
        self.pid = None
23
 
        self.args = args
24
 
 
25
 
    def __enter__(self):
26
 
        pid = os.fork()
27
 
        if pid != 0:
28
 
            self.pid = pid
29
 
            return
30
 
        self.func_or_method(*self.args)
31
 
 
32
 
 
33
 
    def __exit__(self, exc_type, exc_val, traceback):
34
 
        os.kill(self.pid, SIGKILL)
35
 
 
36
 
 
37
 
class FakeGrackleRequestHandler(BaseHTTPRequestHandler):
38
 
 
39
 
    def do_PUT(self):
40
 
        message = self.rfile.read(int(self.headers['content-length']))
41
 
        if message == 'This is a message':
42
 
            self.send_response(httplib.CREATED)
43
 
            self.end_headers()
44
 
            self.wfile.close()
45
 
        else:
46
 
            self.send_error(httplib.BAD_REQUEST)
47
 
 
48
 
 
49
 
def run_service(port):
50
 
    service = HTTPServer(('', port), FakeGrackleRequestHandler)
51
 
    service.serve_forever()
52
 
 
53
 
 
54
 
 
55
3
class TestPutMessage(TestCase):
56
4
 
57
5
    def test_put_message(self):
58
 
        client = GrackleClient('localhost', 8435)
59
 
        with Forked(run_service, client.port):
60
 
            client.put_message('arch1', 'asdf', StringIO('This is a message'))
61
 
            with ExpectedException(Exception, 'wtf'):
62
 
                client.put_message('arch1', 'asdf',
63
 
                    StringIO('This is not a message'))
 
6
        pass