예제 #1
0
#!/usr/bin/env python

from cocaine.worker import Worker
from cocaine.decorators import wsgi

from app import app

print "A"
w = Worker()
print "B"
w.run({"http": wsgi(app)})
예제 #2
0
def test_worker_v1():
    socket_path = "tests/enp2"

    res = list()
    wsgi_res = {"body": list(),
                "status": None,
                "headers": None}
    err_res = list()

    http_res = {}

    def collector(func):
        def wrapper(environ, start_response):
            def g(func):
                def w(status, headers):
                    wsgi_res["status"] = status
                    wsgi_res["headers"] = headers
                    return func(status, headers)
                return w
            result = func(environ, g(start_response))
            wsgi_res["body"].extend(result)
            return res
        return wrapper

    ping_headers = {}

    def ping(request, response):
        ping_headers["Invoke_headers"] = request.headers
        res.append(1)
        inc = yield request.read()
        ping_headers["After_chunk_headers"] = request.headers
        res.append(2)
        res.append(inc)
        with response:
            response.write("A")
            res.append(3)

    def bad_ping(request, response):
        raise Exception("Exception")

    def notclosed(request, response):
        yield request.read()
        response.write("A")

    @collector
    def wsgi_app(environ, start_response):
        response_body = 'Method %s' % environ['REQUEST_METHOD']
        status = '200 OK'
        response_headers = [('Content-Type', 'text/plain'),
                            ('Content-Length', str(len(response_body)))]
        start_response(status, response_headers)
        return [response_body, "A"]

    @http
    def http_test(request, response):
        req = yield request.read()
        http_res["req"] = req
        response.write_head(200, {'Content-Type': 'text/plain'})
        response.write("OK")
        response.close()

    def error_handler(request, response):
        try:
            yield request.read()
        except Exception as err:
            err_res.append(err)

    kwargs = dict(app="testapp",
                  endpoint=socket_path,
                  uuid="randomuuid",
                  disown_timeout=1,
                  heartbeat_timeout=2)

    main_v1(socket_path, 10)
    w = WorkerV1(**kwargs)
    w.run({"ping": ping,
           "bad_ping": bad_ping,
           "notclosed": notclosed,
           "http_test": http_test,
           "err_res": error_handler,
           "http": wsgi(wsgi_app)})

    assert res[:4] == [1, 2, 'pong', 3], res[:4]
    print(ping_headers)
    assert ping_headers["Invoke_headers"][b'trace_id'] == b'\x00\x00\x00\x00\x00\x00\x00\x00'
    assert ping_headers["Invoke_headers"][b'PING'] == 'A'
    assert ping_headers["After_chunk_headers"][b'PING'] == 'B'
    assert wsgi_res["body"] == ["Method POST", "A"], wsgi_res
    assert wsgi_res["status"] == '200 OK', wsgi_res
    assert wsgi_res["headers"] == [('Content-Type', 'text/plain'), ('Content-Length', '11')], wsgi_res["headers"]

    req = http_res["req"]

    assert req.body == BODY, req.body
    assert req.headers == dict(HEADERS), req.headers
    assert req.meta["version"] == HTTP_VERSION, req.meta["version"]
    if sys.version_info[0] == 2:
        assert req.request == {'dsdsds': '', 'arg': '1'}, req.request
    else:
        assert req.request == {'dsdsds': b'', 'arg': '1'}, req.request
    assert req.files == {}, req.files

    assert len(err_res) == 1, err_res
    assert isinstance(err_res[0], RequestError)
    assert err_res[0].code == 100
    assert err_res[0].reason == "test_err"
def test_worker_v1():
    socket_path = "tests/enp2"

    res = list()
    wsgi_res = {"body": list(), "status": None, "headers": None}
    err_res = list()

    http_res = {}

    def collector(func):
        def wrapper(environ, start_response):
            def g(func):
                def w(status, headers):
                    wsgi_res["status"] = status
                    wsgi_res["headers"] = headers
                    return func(status, headers)

                return w

            result = func(environ, g(start_response))
            wsgi_res["body"].extend(result)
            return res

        return wrapper

    def ping(request, response):
        res.append(1)
        inc = yield request.read()
        res.append(2)
        res.append(inc)
        with response:
            response.write("A")
            res.append(3)

    def bad_ping(request, response):
        raise Exception("Exception")

    def notclosed(request, response):
        yield request.read()
        response.write("A")

    @collector
    def wsgi_app(environ, start_response):
        response_body = "Method %s" % environ["REQUEST_METHOD"]
        status = "200 OK"
        response_headers = [("Content-Type", "text/plain"), ("Content-Length", str(len(response_body)))]
        start_response(status, response_headers)
        return [response_body, "A"]

    @http
    def http_test(request, response):
        req = yield request.read()
        http_res["req"] = req
        response.write_head(200, {"Content-Type": "text/plain"})
        response.write("OK")
        response.close()

    def error_handler(request, response):
        try:
            yield request.read()
        except Exception as err:
            err_res.append(err)

    kwargs = dict(app="testapp", endpoint=socket_path, uuid="randomuuid", disown_timeout=1, heartbeat_timeout=2)

    main_v1(socket_path, 10)
    w = WorkerV1(**kwargs)
    w.run(
        {
            "ping": ping,
            "bad_ping": bad_ping,
            "notclosed": notclosed,
            "http_test": http_test,
            "err_res": error_handler,
            "http": wsgi(wsgi_app),
        }
    )

    assert res[:4] == [1, 2, "pong", 3], res[:4]
    assert wsgi_res["body"] == ["Method POST", "A"], wsgi_res
    assert wsgi_res["status"] == "200 OK", wsgi_res
    assert wsgi_res["headers"] == [("Content-Type", "text/plain"), ("Content-Length", "11")], wsgi_res["headers"]

    req = http_res["req"]

    assert req.body == BODY, req.body
    assert req.headers == dict(HEADERS), req.headers
    assert req.meta["version"] == HTTP_VERSION, req.meta["version"]
    if sys.version_info[0] == 2:
        assert req.request == {"dsdsds": "", "arg": "1"}, req.request
    else:
        assert req.request == {"dsdsds": b"", "arg": "1"}, req.request
    assert req.files == {}, req.files

    assert len(err_res) == 1, err_res
    assert isinstance(err_res[0], RequestError)
    assert err_res[0].code == 100
    assert err_res[0].reason == "test_err"
예제 #4
0
#!/usr/bin/env python
from __future__ import print_function, unicode_literals

from cocaine.worker import Worker
from cocaine.decorators import wsgi

from app import app

print("A")
w = Worker()
print("B")
w.run({"http": wsgi(app)})
예제 #5
0
def test_worker_v1():
    socket_path = "tests/enp2"

    res = list()
    wsgi_res = {"body": list(), "status": None, "headers": None}
    err_res = list()

    http_res = {}

    def collector(func):
        def wrapper(environ, start_response):
            def g(func):
                def w(status, headers):
                    wsgi_res["status"] = status
                    wsgi_res["headers"] = headers
                    return func(status, headers)

                return w

            result = func(environ, g(start_response))
            wsgi_res["body"].extend(result)
            return res

        return wrapper

    ping_headers = {}

    def ping(request, response):
        ping_headers["Invoke_headers"] = request.headers
        res.append(1)
        inc = yield request.read()
        ping_headers["After_chunk_headers"] = request.headers
        res.append(2)
        res.append(inc)
        with response:
            response.write("A")
            res.append(3)

    def bad_ping(request, response):
        raise Exception("Exception")

    def notclosed(request, response):
        yield request.read()
        response.write("A")

    @collector
    def wsgi_app(environ, start_response):
        response_body = 'Method %s' % environ[b'REQUEST_METHOD']
        status = '200 OK'
        response_headers = [('Content-Type', 'text/plain'),
                            ('Content-Length', str(len(response_body)))]
        start_response(status, response_headers)
        return [response_body, "A"]

    @http
    def http_test(request, response):
        req = yield request.read()
        http_res["req"] = req
        response.write_head(200, {'Content-Type': 'text/plain'})
        response.write("OK")
        response.close()

    def error_handler(request, response):
        try:
            yield request.read()
        except Exception as err:
            err_res.append(err)

    kwargs = dict(app="testapp",
                  endpoint=socket_path,
                  uuid="randomuuid",
                  disown_timeout=1,
                  heartbeat_timeout=2)

    main_v1(socket_path, 10)
    w = WorkerV1(**kwargs)
    w.run({
        "ping": ping,
        "bad_ping": bad_ping,
        "notclosed": notclosed,
        "http_test": http_test,
        "err_res": error_handler,
        "http": wsgi(wsgi_app)
    })

    assert res[:4] == [1, 2, b'pong', 3], res[:4]
    assert ping_headers["Invoke_headers"][
        b'trace_id'] == b'\x00\x00\x00\x00\x00\x00\x00\x00'
    assert ping_headers["Invoke_headers"][b'PING'] == b'A'
    assert ping_headers["After_chunk_headers"][b'PING'] == b'B'
    # assert wsgi_res["body"] == [b"Method POST", b"A"], wsgi_res
    # assert wsgi_res["status"] == b'200 OK', wsgi_res
    # assert wsgi_res["headers"] == [('Content-Type', 'text/plain'), ('Content-Length', '11')], wsgi_res["headers"]

    req = http_res["req"]

    assert req.body == BODY, req.body
    assert req.headers == dict(HEADERS), req.headers
    assert req.meta["version"] == HTTP_VERSION, req.meta["version"]
    if sys.version_info[0] == 2:
        assert req.request == {'dsdsds': '', 'arg': '1'}, req.request
    else:
        assert req.request == {'dsdsds': b'', 'arg': '1'}, req.request
    assert req.files == {}, req.files

    assert len(err_res) == 1, err_res
    assert isinstance(err_res[0], RequestError)
    assert err_res[0].code == 100
    assert err_res[0].reason == b"test_err"