Esempio n. 1
0
def test_second_service_blah_blah(
    server, service, second_service, ThriftTest, SecondService
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    @server.thrift.register(SecondService)
    def blahBlah(request):
        pass

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(service.testString('thing'))

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'thing'

    resp = yield tchannel.thrift(second_service.blahBlah())

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body is None
Esempio n. 2
0
def test_service_inheritance_with_import(tmpdir):
    tmpdir.join('shared/base.thrift').write(
        'service Base { bool healthy() }', ensure=True
    )

    inherited = tmpdir.join('myservice.thrift')
    inherited.write('''
        include "./shared/base.thrift"

        service MyService extends base.Base {
            i32 getValue()
        }
    ''')

    service = thrift.load(str(inherited), service='myservice')

    server = TChannel('server')

    @server.thrift.register(service.MyService)
    def getValue(request):
        return 42

    @server.thrift.register(service.MyService)
    def healthy(request):
        return True

    server.listen()

    client = TChannel('client', known_peers=[server.hostport])

    response = yield client.thrift(service.MyService.getValue())
    assert response.body == 42

    response = yield client.thrift(service.MyService.healthy())
    assert response.body is True
Esempio n. 3
0
def test_service_inheritance_with_import(tmpdir):
    tmpdir.join('shared/base.thrift').write('service Base { bool healthy() }',
                                            ensure=True)

    inherited = tmpdir.join('myservice.thrift')
    inherited.write('''
        include "./shared/base.thrift"

        service MyService extends base.Base {
            i32 getValue()
        }
    ''')

    service = thrift.load(str(inherited), service='myservice')

    server = TChannel('server')

    @server.thrift.register(service.MyService)
    def getValue(request):
        return 42

    @server.thrift.register(service.MyService)
    def healthy(request):
        return True

    server.listen()

    client = TChannel('client', known_peers=[server.hostport])

    response = yield client.thrift(service.MyService.getValue())
    assert response.body == 42

    response = yield client.thrift(service.MyService.healthy())
    assert response.body is True
Esempio n. 4
0
def test_service_inheritance(tmpdir):
    path = tmpdir.join('myservice.thrift')
    path.write('''
        service Base { bool healthy() }

        service MyService extends Base {
            i32 getValue()
        }
    ''')
    service = thrift.load(str(path), service='myservice')

    server = TChannel('server')
    server.listen()

    @server.thrift.register(service.MyService)
    def getValue(request):
        return 42

    @server.thrift.register(service.MyService)
    def healthy(request):
        return True

    client = TChannel('client', known_peers=[server.hostport])

    response = yield client.thrift(service.MyService.getValue())
    assert response.body == 42

    response = yield client.thrift(service.MyService.healthy())
    assert response.body is True
Esempio n. 5
0
def test_service_inheritance(tmpdir):
    path = tmpdir.join('myservice.thrift')
    path.write('''
        service Base { bool healthy() }

        service MyService extends Base {
            i32 getValue()
        }
    ''')
    service = thrift.load(str(path), service='myservice')

    server = TChannel('server')
    server.listen()

    @server.thrift.register(service.MyService)
    def getValue(request):
        return 42

    @server.thrift.register(service.MyService)
    def healthy(request):
        return True

    client = TChannel('client', known_peers=[server.hostport])

    response = yield client.thrift(service.MyService.getValue())
    assert response.body == 42

    response = yield client.thrift(service.MyService.healthy())
    assert response.body is True
Esempio n. 6
0
def test_exception(server, service, ThriftTest, server_ttypes, client_ttypes):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testException(request):

        if request.body.arg == 'Xception':
            raise server_ttypes.Xception(errorCode=1001,
                                         message=request.body.arg)
        elif request.body.arg == 'TException':
            # TODO - what to raise here? We dont want dep on Thrift
            # so we don't have thrift.TException available to us...
            raise Exception()

    # Make a call:

    tchannel = TChannel(name='client')

    # case #1
    with pytest.raises(client_ttypes.Xception) as e:
        yield tchannel.thrift(service.testException(arg='Xception'))
        assert e.value.errorCode == 1001
        assert e.value.message == 'Xception'

    # case #2
    with pytest.raises(UnexpectedError):
        yield tchannel.thrift(service.testException(arg='TException'))

    # case #3
    resp = yield tchannel.thrift(service.testException(arg='something else'))
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body is None
Esempio n. 7
0
def test_i32(server, service, ThriftTest):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testI32(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    # case #1
    resp = yield tchannel.thrift(
        service.testI32(-1)
    )
    assert resp.headers == {}
    assert resp.body == -1

    # case #2
    resp = yield tchannel.thrift(
        service.testI32(1)
    )
    assert resp.headers == {}
    assert resp.body == 1
Esempio n. 8
0
def test_inherited_method_names(tmpdir):
    thrift_file = tmpdir.join('service.thrift')
    thrift_file.write('''
        service Base { string hello() }
        service Foo extends Base {}
        service Bar extends Base {}
    ''')

    service = thrift.load(str(thrift_file), 'myservice')

    server = TChannel('server')

    @server.thrift.register(service.Foo, method='hello')
    def foo_hello(request):
        return 'foo'

    @server.thrift.register(service.Bar, method='hello')
    def bar_hello(request):
        return 'bar'

    server.listen()

    client = TChannel('client')

    res = yield client.thrift(service.Foo.hello(), hostport=server.hostport)
    assert res.body == 'foo'

    res = yield client.thrift(service.Bar.hello(), hostport=server.hostport)
    assert res.body == 'bar'
def test_i32():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testI32(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    # case #1
    resp = yield tchannel.thrift(
        service.testI32(-1)
    )
    assert resp.headers == {}
    assert resp.body == -1

    # case #2
    resp = yield tchannel.thrift(
        service.testI32(1)
    )
    assert resp.headers == {}
    assert resp.body == 1
Esempio n. 10
0
def test_i32(server, service, ThriftTest):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testI32(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    # case #1
    resp = yield tchannel.thrift(
        service.testI32(-1)
    )
    assert resp.headers == {}
    assert resp.body == -1

    # case #2
    resp = yield tchannel.thrift(
        service.testI32(1)
    )
    assert resp.headers == {}
    assert resp.body == 1
Esempio n. 11
0
def test_second_service_blah_blah(
    server, service, second_service, ThriftTest, SecondService
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    @server.thrift.register(SecondService)
    def blahBlah(request):
        pass

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(service.testString('thing'))

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'thing'

    resp = yield tchannel.thrift(second_service.blahBlah())

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body is None
Esempio n. 12
0
def test_i32():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testI32(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    # case #1
    resp = yield tchannel.thrift(service.testI32(-1))
    assert resp.headers == {}
    assert resp.body == -1

    # case #2
    resp = yield tchannel.thrift(service.testI32(1))
    assert resp.headers == {}
    assert resp.body == 1
def test_second_service_second_test_string():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    @server.thrift.register(SecondService)
    @gen.coroutine
    def secondtestString(request):

        service = thrift_request_builder(
            service='server',
            thrift_module=ThriftTest,
            hostport=server.hostport,
        )
        resp = yield tchannel.thrift(
            service.testString(request.body.thing),
        )

        raise gen.Return(resp)

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport
    )

    second_service = thrift_request_builder(
        service='server',
        thrift_module=SecondService,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testString('thing'))

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'thing'

    resp = yield tchannel.thrift(
        second_service.secondtestString('second_string')
    )

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'second_string'
def test_multi_exception():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMultiException(request):

        if request.body.arg0 == 'Xception':
            raise ThriftTest.Xception(
                errorCode=1001,
                message='This is an Xception',
            )
        elif request.body.arg0 == 'Xception2':
            raise ThriftTest.Xception2(
                errorCode=2002
            )

        return ThriftTest.Xtruct(string_thing=request.body.arg1)

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='service',
        thrift_module=ThriftTest,
        hostport=server.hostport
    )

    # case #1
    with pytest.raises(ThriftTest.Xception) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception', arg1='thingy')
        )
        assert e.value.errorCode == 1001
        assert e.value.message == 'This is an Xception'

    # case #2
    with pytest.raises(ThriftTest.Xception2) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception2', arg1='thingy')
        )
        assert e.value.errorCode == 2002

    # case #3
    resp = yield tchannel.thrift(
        service.testMultiException(arg0='something else', arg1='thingy')
    )
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == ThriftTest.Xtruct('thingy')
Esempio n. 15
0
def run():
    app_name = 'keyvalue-client'

    tchannel = TChannel(app_name)

    KeyValueClient.setValue("foo", "Hello, world!"),

    yield tchannel.thrift(KeyValueClient.setValue("foo", "Hello, world!"), )

    response = yield tchannel.thrift(KeyValueClient.getValue("foo"), )

    print response.body
Esempio n. 16
0
def run():
    app_name = "keyvalue-client"

    tchannel = TChannel(app_name)

    KeyValueClient.setValue("foo", "Hello, world!"),

    yield tchannel.thrift(KeyValueClient.setValue("foo", "Hello, world!"))

    response = yield tchannel.thrift(KeyValueClient.getValue("foo"))

    print response.body
def test_exception():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testException(request):

        if request.body.arg == 'Xception':
            raise ThriftTest.Xception(
                errorCode=1001,
                message=request.body.arg
            )
        elif request.body.arg == 'TException':
            # TODO - what to raise here? We dont want dep on Thrift
            # so we don't have thrift.TException available to us...
            raise Exception()

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='service',
        thrift_module=ThriftTest,
        hostport=server.hostport
    )

    # case #1
    with pytest.raises(ThriftTest.Xception) as e:
        yield tchannel.thrift(
            service.testException(arg='Xception')
        )
        assert e.value.errorCode == 1001
        assert e.value.message == 'Xception'

    # case #2
    with pytest.raises(UnexpectedError):
        yield tchannel.thrift(
            service.testException(arg='TException')
        )

    # case #3
    resp = yield tchannel.thrift(
        service.testException(arg='something else')
    )
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body is None
Esempio n. 18
0
def test_second_service_second_test_string():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    @server.thrift.register(SecondService)
    @gen.coroutine
    def secondtestString(request):

        service = thrift_request_builder(
            service='server',
            thrift_module=ThriftTest,
            hostport=server.hostport,
        )
        resp = yield tchannel.thrift(service.testString(request.body.thing), )

        raise gen.Return(resp)

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(service='server',
                                     thrift_module=ThriftTest,
                                     hostport=server.hostport)

    second_service = thrift_request_builder(
        service='server',
        thrift_module=SecondService,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testString('thing'))

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'thing'

    resp = yield tchannel.thrift(
        second_service.secondtestString('second_string'))

    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'second_string'
Esempio n. 19
0
def test_multi_exception(
    server, service, ThriftTest, server_ttypes, client_ttypes
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testMultiException(request):

        if request.body.arg0 == 'Xception':
            raise server_ttypes.Xception(
                errorCode=1001,
                message='This is an Xception',
            )
        elif request.body.arg0 == 'Xception2':
            raise server_ttypes.Xception2(
                errorCode=2002
            )

        return server_ttypes.Xtruct(string_thing=request.body.arg1)

    # Make a call:

    tchannel = TChannel(name='client')

    # case #1
    with pytest.raises(client_ttypes.Xception) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception', arg1='thingy')
        )
        assert e.value.errorCode == 1001
        if six.PY2:
            assert e.value.message == 'This is an Xception'
        if six.PY3:
            assert e.value.args[0] == 'This is an Xception'
    # case #2
    with pytest.raises(client_ttypes.Xception2) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception2', arg1='thingy')
        )
        assert e.value.errorCode == 2002

    # case #3
    resp = yield tchannel.thrift(
        service.testMultiException(arg0='something else', arg1='thingy')
    )
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == client_ttypes.Xtruct('thingy')
Esempio n. 20
0
def test_multi_exception():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMultiException(request):

        if request.body.arg0 == 'Xception':
            raise ThriftTest.Xception(
                errorCode=1001,
                message='This is an Xception',
            )
        elif request.body.arg0 == 'Xception2':
            raise ThriftTest.Xception2(errorCode=2002)

        return ThriftTest.Xtruct(string_thing=request.body.arg1)

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(service='service',
                                     thrift_module=ThriftTest,
                                     hostport=server.hostport)

    # case #1
    with pytest.raises(ThriftTest.Xception) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception', arg1='thingy'))
        assert e.value.errorCode == 1001
        assert e.value.message == 'This is an Xception'

    # case #2
    with pytest.raises(ThriftTest.Xception2) as e:
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception2', arg1='thingy'))
        assert e.value.errorCode == 2002

    # case #3
    resp = yield tchannel.thrift(
        service.testMultiException(arg0='something else', arg1='thingy'))
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == ThriftTest.Xtruct('thingy')
Esempio n. 21
0
def test_exception(server, service, ThriftTest, server_ttypes, client_ttypes):

    # Given this test server:

    class MyCoolError(Exception):
        pass

    @server.thrift.register(ThriftTest)
    def testException(request):

        if request.body.arg == 'Xception':
            raise server_ttypes.Xception(
                errorCode=1001,
                message=request.body.arg
            )
        elif request.body.arg == 'TException':
            # TODO - what to raise here? We dont want dep on Thrift
            # so we don't have thrift.TException available to us...
            raise MyCoolError("hi")

    # Make a call:

    tchannel = TChannel(name='client')

    # case #1
    with pytest.raises(client_ttypes.Xception) as e:
        yield tchannel.thrift(
            service.testException(arg='Xception')
        )
    assert e.value.errorCode == 1001
    assert e.value.message == 'Xception'

    # case #2
    with pytest.raises(UnexpectedError) as e:
        yield tchannel.thrift(
            service.testException(arg='TException')
        )
    assert 'MyCoolError' in str(e)
    assert 'ThriftTest::testException' in str(e)
    assert 'test_thrift.py' in str(e)

    # case #3
    resp = yield tchannel.thrift(
        service.testException(arg='something else')
    )
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body is None
Esempio n. 22
0
def test_void():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testVoid(request):
        pass

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testVoid())

    assert resp.headers == {}
    assert resp.body is None
Esempio n. 23
0
def test_map():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMap(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        -1: -2,
    }

    resp = yield tchannel.thrift(service.testMap(thing=x))

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 24
0
def test_binary():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testBinary(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(
        service.testBinary(
            # this is ThriftTest.Xtruct(string_thing='hi')
            '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00'))

    assert resp.headers == {}
    assert (resp.body == '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00')
def test_enum():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testEnum(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = ThriftTest.Numberz.FIVE

    resp = yield tchannel.thrift(
        service.testEnum(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 26
0
def test_call_response_should_contain_transport_headers(
    server, service, ThriftTest
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(service.testString('hi'))

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'hi'

    # verify response transport headers
    assert isinstance(resp.transport, TransportHeaders)
    assert resp.transport.scheme == schemes.THRIFT
    assert resp.transport.failure_domain is None
Esempio n. 27
0
def test_map_map(server, service, ThriftTest):

    # Given this test server:

    map_map = {
        -4: {
            -4: -4,
            -3: -3,
            -2: -2,
            -1: -1,
        },
        4: {
            1: 1,
            2: 2,
            3: 3,
            4: 4,
        },
    }

    @server.thrift.register(ThriftTest)
    def testMapMap(request):
        return map_map

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(
        service.testMapMap(1)
    )

    assert resp.headers == {}
    assert resp.body == map_map
Esempio n. 28
0
def test_nest(server, service, ThriftTest, client_ttypes):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testNest(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    xstruct = client_ttypes.Xtruct(
        string_thing='hi',
        byte_thing=1,
        i32_thing=-1,
        i64_thing=-34359738368,
    )
    xstruct2 = client_ttypes.Xtruct2(
        byte_thing=1,
        struct_thing=xstruct,
        i32_thing=1,
    )

    resp = yield tchannel.thrift(
        service.testNest(thing=xstruct2)
    )

    assert resp.headers == {}
    assert resp.body == xstruct2
Esempio n. 29
0
def test_nest(server, service, ThriftTest, client_ttypes):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testNest(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    xstruct = client_ttypes.Xtruct(
        string_thing='hi',
        byte_thing=1,
        i32_thing=-1,
        i64_thing=-34359738368,
    )
    xstruct2 = client_ttypes.Xtruct2(
        byte_thing=1,
        struct_thing=xstruct,
        i32_thing=1,
    )

    resp = yield tchannel.thrift(
        service.testNest(thing=xstruct2)
    )

    assert resp.headers == {}
    assert resp.body == xstruct2
Esempio n. 30
0
def test_struct_with_headers(
    server, service, ThriftTest, server_ttypes, client_ttypes
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testStruct(request):

        assert isinstance(request, Request)
        assert request.headers == {'req': 'header'}
        assert request.body.thing.string_thing == 'req string'

        return Response(
            server_ttypes.Xtruct(
                string_thing="resp string"
            ),
            headers={'resp': 'header'},
        )

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(
        service.testStruct(client_ttypes.Xtruct("req string")),
        headers={'req': 'header'},
    )

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {'resp': 'header'}
    assert resp.body == client_ttypes.Xtruct("resp string")
def test_map():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMap(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        -1: -2,
    }

    resp = yield tchannel.thrift(
        service.testMap(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
def test_string_map():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testStringMap(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = {
        'hello': 'there',
        'my': 'name',
        'is': 'shirly',
    }

    resp = yield tchannel.thrift(
        service.testStringMap(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
def test_void():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testVoid(request):
        pass

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testVoid())

    assert resp.headers == {}
    assert resp.body is None
Esempio n. 34
0
def test_type_def():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testTypedef(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = 0xffffffffffffff  # 7 bytes of 0xff

    resp = yield tchannel.thrift(service.testTypedef(thing=x))

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 35
0
def test_map(server, service, ThriftTest):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testMap(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    x = {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        -1: -2,
    }

    resp = yield tchannel.thrift(
        service.testMap(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 36
0
def test_struct_with_headers(
    server, service, ThriftTest, server_ttypes, client_ttypes
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testStruct(request):

        assert isinstance(request, Request)
        assert request.headers == {'req': 'header'}
        assert request.body.thing.string_thing == 'req string'

        return Response(
            server_ttypes.Xtruct(
                string_thing="resp string"
            ),
            headers={'resp': 'header'},
        )

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(
        service.testStruct(client_ttypes.Xtruct("req string")),
        headers={'req': 'header'},
    )

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {'resp': 'header'}
    assert resp.body == client_ttypes.Xtruct("resp string")
def test_struct():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testStruct(request):

        assert request.body.thing.string_thing == 'req string'

        return ThriftTest.Xtruct(
            string_thing="resp string"
        )

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='service',
        thrift_module=ThriftTest,
        hostport=server.hostport
    )

    resp = yield tchannel.thrift(
        service.testStruct(ThriftTest.Xtruct("req string"))
    )

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == ThriftTest.Xtruct("resp string")
def test_void_with_headers():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testVoid(request):
        assert request.headers == {'req': 'header'}
        return Response(headers={'resp': 'header'})

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(
        service.testVoid(),
        headers={'req': 'header'},
    )

    assert resp.headers == {
        'resp': 'header'
    }
    assert resp.body is None
Esempio n. 39
0
def test_call_unexpected_error_should_result_in_protocol_error():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMultiException(request):
        raise Exception('well, this is unfortunate')

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    with pytest.raises(ProtocolError):
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception', arg1='thingy'))
def test_call_unexpected_error_should_result_in_unexpected_error():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testMultiException(request):
        raise Exception('well, this is unfortunate')

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    with pytest.raises(UnexpectedError):
        yield tchannel.thrift(
            service.testMultiException(arg0='Xception', arg1='thingy')
        )
def test_call_response_should_contain_transport_headers():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testString('hi'))

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'hi'

    # verify response transport headers
    assert isinstance(resp.transport, TransportHeaders)
    assert resp.transport.scheme == schemes.THRIFT
    assert resp.transport.failure_domain is None
Esempio n. 42
0
def test_call_response_should_contain_transport_headers():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testString('hi'))

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'hi'

    # verify response transport headers
    assert isinstance(resp.transport, TransportHeaders)
    assert resp.transport.scheme == schemes.THRIFT
    assert resp.transport.failure_domain is None
def test_type_def():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testTypedef(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = 0xffffffffffffff  # 7 bytes of 0xff

    resp = yield tchannel.thrift(
        service.testTypedef(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
def test_binary():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testBinary(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(
        service.testBinary(
            # this is ThriftTest.Xtruct(string_thing='hi')
            '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00'
        )
    )

    assert resp.headers == {}
    assert (
        resp.body ==
        '\x0c\x00\x00\x0b\x00\x01\x00\x00\x00\x0bhi\x00\x00'
    )
Esempio n. 45
0
def test_call_response_should_contain_transport_headers(
    server, service, ThriftTest
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testString(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(service.testString('hi'))

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == 'hi'

    # verify response transport headers
    assert isinstance(resp.transport, TransportHeaders)
    assert resp.transport.scheme == schemes.THRIFT
    assert resp.transport.failure_domain is None
Esempio n. 46
0
def test_double():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testDouble(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(service.testDouble(-5.235098235))

    assert resp.headers == {}
    assert resp.body == -5.235098235
Esempio n. 47
0
def test_value_expected_but_none_returned_should_error(
    server, service, ThriftTest, use_thriftrw_server
):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testString(request):
        pass

    # Make a call:

    tchannel = TChannel(name='client')

    if use_thriftrw_server:
        # With thirftrw the client only sees an unexpected error because
        # thriftrw always disallows None results for functions that return
        # values.
        exc = UnexpectedError
    else:
        exc = ValueExpectedError

    with pytest.raises(exc) as exc_info:
        yield tchannel.thrift(
            service.testString('no return!?')
        )

    if not use_thriftrw_server:
        assert 'Expected a value to be returned' in str(exc_info)
        assert 'ThriftTest::testString' in str(exc_info)
Esempio n. 48
0
def test_struct():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testStruct(request):

        assert request.body.thing.string_thing == 'req string'

        return ThriftTest.Xtruct(string_thing="resp string")

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(service='service',
                                     thrift_module=ThriftTest,
                                     hostport=server.hostport)

    resp = yield tchannel.thrift(
        service.testStruct(ThriftTest.Xtruct("req string")))

    # verify response
    assert isinstance(resp, Response)
    assert resp.headers == {}
    assert resp.body == ThriftTest.Xtruct("resp string")
def test_double():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testDouble(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(
        service.testDouble(-5.235098235)
    )

    assert resp.headers == {}
    assert resp.body == -5.235098235
Esempio n. 50
0
def test_string_map():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testStringMap(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = {
        'hello': 'there',
        'my': 'name',
        'is': 'shirly',
    }

    resp = yield tchannel.thrift(service.testStringMap(thing=x))

    assert resp.headers == {}
    assert resp.body == x
def test_value_expected_but_none_returned_should_error():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testString(request):
        pass

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    with pytest.raises(ValueExpectedError):
        yield tchannel.thrift(
            service.testString('no return!?')
        )
Esempio n. 52
0
def test_enum():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testEnum(request):
        return request.body.thing

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )
    x = ThriftTest.Numberz.FIVE

    resp = yield tchannel.thrift(service.testEnum(thing=x))

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 53
0
def test_map(server, service, ThriftTest):

    # Given this test server:

    @server.thrift.register(ThriftTest)
    def testMap(request):
        return request.body.thing

    # Make a call:

    tchannel = TChannel(name='client')

    x = {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        -1: -2,
    }

    resp = yield tchannel.thrift(
        service.testMap(thing=x)
    )

    assert resp.headers == {}
    assert resp.body == x
Esempio n. 54
0
def test_map_map(server, service, ThriftTest):

    # Given this test server:

    map_map = {
        -4: {
            -4: -4,
            -3: -3,
            -2: -2,
            -1: -1,
        },
        4: {
            1: 1,
            2: 2,
            3: 3,
            4: 4,
        },
    }

    @server.thrift.register(ThriftTest)
    def testMapMap(request):
        return map_map

    # Make a call:

    tchannel = TChannel(name='client')

    resp = yield tchannel.thrift(
        service.testMapMap(1)
    )

    assert resp.headers == {}
    assert resp.body == map_map
def test_oneway():

    # Given this test server:

    server = TChannel(name='server')

    # TODO - server should raise same exception as client
    with pytest.raises(AssertionError):
        @server.thrift.register(ThriftTest)
        def testOneway(request):
            pass

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    with pytest.raises(OneWayNotSupportedError):
        yield tchannel.thrift(service.testOneway(1))
Esempio n. 56
0
def test_oneway():

    # Given this test server:

    server = TChannel(name='server')

    # TODO - server should raise same exception as client
    with pytest.raises(AssertionError):

        @server.thrift.register(ThriftTest)
        def testOneway(request):
            pass

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    with pytest.raises(OneWayNotSupportedError):
        yield tchannel.thrift(service.testOneway(1))
Esempio n. 57
0
def test_void_with_headers():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testVoid(request):
        assert request.headers == {'req': 'header'}
        return Response(headers={'resp': 'header'})

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    resp = yield tchannel.thrift(
        service.testVoid(),
        headers={'req': 'header'},
    )

    assert resp.headers == {'resp': 'header'}
    assert resp.body is None
Esempio n. 58
0
def test_headers_should_be_a_map_of_strings(headers):

    tchannel = TChannel('client')

    with pytest.raises(ValueError):
        yield tchannel.thrift(
            request=mock.MagicMock(),
            headers=headers,
        )
Esempio n. 59
0
def test_insanity():

    # Given this test server:

    server = TChannel(name='server')

    @server.thrift.register(ThriftTest)
    def testInsanity(request):
        result = {
            1: {
                2: request.body.argument,
                3: request.body.argument,
            },
            2: {
                6: ThriftTest.Insanity(),
            },
        }
        return result

    server.listen()

    # Make a call:

    tchannel = TChannel(name='client')

    service = thrift_request_builder(
        service='server',
        thrift_module=ThriftTest,
        hostport=server.hostport,
    )

    x = ThriftTest.Insanity(
        userMap={
            ThriftTest.Numberz.EIGHT: 0xffffffffffffff,
        },
        xtructs=[
            ThriftTest.Xtruct(
                string_thing='Hello2',
                byte_thing=74,
                i32_thing=0xff00ff,
                i64_thing=-34359738368,
            ),
        ],
    )

    resp = yield tchannel.thrift(service.testInsanity(x))

    assert resp.headers == {}
    assert resp.body == {
        1: {
            2: x,
            3: x,
        },
        2: {
            6: ThriftTest.Insanity(),
        },
    }