Here are the examples of the python api pytest.mark.gen_test taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
176 Examples
3
Example 1
Project: tchannel-python Source File: test_forwarding.py
@pytest.mark.gen_test
def test_forwarding_json(client):
json_response = yield client.json('keyvalue', 'putItem', {
'key': 'hello',
'value': 'world',
}, timeout=1.0)
assert json_response.body == {'success': True}
3
Example 2
Project: tchannel-python Source File: test_peer_strategy.py
@pytest.mark.gen_test
def test_get_rank_with_imcoming():
server = TChannel('server')
server.listen()
connection = yield TornadoConnection.outgoing(server.hostport)
connection.direction = INCOMING
peer = Peer(TChannel('test'), '10.10.101.21:230')
calculator = PreferIncomingCalculator()
peer.register_incoming_conn(connection)
assert sys.maxint != calculator.get_rank(peer)
3
Example 3
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_record_success_with_ttl_timeout(tmpdir, mock_server, call, get_body):
"""Make sure legitimate request timeouts propagate during recording."""
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_write('world', delay=0.1).once()
with pytest.raises(TimeoutError):
with vcr.use_cassette(str(path)) as cass:
response = yield call('hello', 'world', service='hello_service',
ttl=0.05)
assert 'world' == (yield get_body(response))
assert cass.play_count == 0
3
Example 4
Project: tchannel-python Source File: test_dispatch.py
@pytest.mark.gen_test
def test_routing_delegate_is_propagated_thrift(tmpdir):
tmpdir.join('service.thrift').write('service Service { bool healthy() }')
thrift_module = thrift.load(str(tmpdir.join('service.thrift')),
service='service')
server = TChannel('server')
server.listen()
@server.thrift.register(thrift_module.Service)
def healthy(request):
assert request.transport.routing_delegate == 'delegate'
return True
client = TChannel('client', known_peers=[server.hostport])
res = yield client.thrift(
thrift_module.Service.healthy(), routing_delegate='delegate'
)
assert res.body is True
3
Example 5
Project: tchannel-python Source File: test_server.py
@pytest.mark.gen_test
def test_write_protected(vcr_service, cassette, call):
cassette.record_mode = 'none'
cassette.write_protected = True
allow(cassette).can_replay.and_return(False)
with pytest.raises(proxy.CannotRecordInteractionsError):
yield call('endpoint', 'request body')
3
Example 6
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_use_cassette_as_decorator(tmpdir, mock_server, call, get_body):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_write('world').once()
@gen.coroutine
@vcr.use_cassette(str(path))
def f():
response = yield call('hello', 'world', service='hello_service')
body = yield get_body(response)
raise gen.Return(body)
body = yield f()
assert body == 'world'
body = yield f()
assert body == 'world'
3
Example 7
Project: tchannel-python Source File: test_hyperbahn_blackhole.py
@pytest.mark.gen_test
def test_ad_blackhole(blackhole, monkeypatch):
# start new ad requests more frequently
monkeypatch.setattr(hyperbahn, 'DELAY', 100) # milliseconds
monkeypatch.setattr(hyperbahn, 'PER_ATTEMPT_TIMEOUT', 5) # seconds
# No jitter
monkeypatch.setattr(hyperbahn, 'DEFAULT_INTERVAL_MAX_JITTER_SECS', 0.0)
# verify that we don't go crazy if Hyperbahn starts blackholing requests.
client = TChannel('client')
client.advertise(routers=blackhole.peers)
yield gen.sleep(0.5)
# The second ad request is still ongoing so no other ads should have been
# made.
assert 2 == blackhole.ad_count
3
Example 8
@pytest.mark.gen_test
@pytest.mark.call
def test_void(server, service, ThriftTest):
# Given this test server:
@server.thrift.register(ThriftTest)
def testVoid(request):
pass
# Make a call:
tchannel = TChannel(name='client')
resp = yield tchannel.thrift(service.testVoid())
assert resp.headers == {}
assert resp.body is None
3
Example 9
Project: tchannel-python Source File: test_queue.py
@pytest.mark.gen_test
def test_get_then_put(items):
queue = Queue()
got_futures = []
for i in range(len(items)):
got_futures.append(queue.get())
for item in items:
yield queue.put(item)
got = yield got_futures
assert got == items
3
Example 10
Project: tchannel-python Source File: test_thrift.py
@pytest.mark.gen_test
@pytest.mark.call
@pytest.mark.parametrize('ClientTChannel', [TChannel, DeprecatedTChannel])
def test_client_for(ClientTChannel, server, ThriftTest):
@server.thrift.register(ThriftTest)
def testString(request):
return request.body.thing.encode('rot13')
tchannel = ClientTChannel(name='client')
client = client_for('server', _ThriftTest)(
tchannel=tchannel,
hostport=server.hostport,
)
resp = yield client.testString(thing='foo')
assert resp == 'sbb'
3
Example 11
Project: tchannel-python Source File: test_dispatch.py
@pytest.mark.gen_test
def test_routing_delegate_is_propagated_raw():
server = TChannel('server')
server.listen()
@server.raw.register('foo')
def handler(request):
assert request.transport.routing_delegate == 'delegate'
return b'success'
client = TChannel('client', known_peers=[server.hostport])
res = yield client.raw('service', 'foo', b'', routing_delegate='delegate')
assert res.body == b'success'
3
Example 12
Project: tchannel-python Source File: test_server.py
@pytest.mark.gen_test
def test_unexpected_error(vcr_service, cassette, call):
allow(cassette).can_replay.and_raise(SomeException("great sadness"))
with pytest.raises(proxy.VCRServiceError) as exc_info:
yield call('endpoint', 'body')
assert 'great sadness' in str(exc_info)
3
Example 13
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_record_success(tmpdir, mock_server, call, get_body):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_write('world').once()
with vcr.use_cassette(str(path)) as cass:
response = yield call('hello', 'world', service='hello_service')
assert 'world' == (yield get_body(response))
assert cass.play_count == 0
assert path.check(file=True)
with vcr.use_cassette(str(path)) as cass:
response = yield call('hello', 'world', service='hello_service')
assert 'world' == (yield get_body(response))
assert cass.play_count == 1
3
Example 14
Project: tchannel-python Source File: test_forwarding.py
@pytest.mark.gen_test
def test_forwarding_thrift_exception(keyvalue, client):
with pytest.raises(keyvalue.ItemDoesNotExist):
yield client.thrift(
keyvalue.KeyValue.getItem('foo'),
headers={'expect': 'failure'},
)
3
Example 15
Project: tchannel-python Source File: test_tornado_client.py
@pytest.mark.gen_test
def test_false_result(thrift_service):
# Verify that we aren't treating False as None.
app = TChannel(name='app')
@app.register(thrift_service)
def healthy(request, response):
return False
app.listen()
client = TChannel(name='client')
response = yield client.request(
hostport=app.hostport, arg_scheme='thrift'
).send('Service::healthy', '\x00\x00', '\x00')
body = yield response.get_body()
assert body == '\x02\x00\x00\x00\x00'
3
Example 16
Project: tchannel-python Source File: test_future.py
@pytest.mark.gen_test
@pytest.mark.gen_test
def test_fail_to_failure_in_coroutine():
answer = gen.Future()
@fail_to(answer)
@gen.coroutine
def f():
raise GreatSadness
with pytest.raises(GreatSadness):
yield f()
assert answer.running()
3
Example 17
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_use_cassette_with_matchers(tmpdir, mock_server, call, get_body):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_write('world').once()
with vcr.use_cassette(str(path), matchers=['body']) as cass:
response = yield call('hello', 'world', service='hello_service')
assert 'world' == (yield get_body(response))
assert cass.play_count == 0
assert path.check(file=True)
with vcr.use_cassette(str(path), matchers=['body']) as cass:
response = yield call(
'not-hello', 'world', service='not_hello_service'
)
assert 'world' == (yield get_body(response))
assert cass.play_count == 1
3
Example 18
Project: tchannel-python Source File: test_peer_strategy.py
@pytest.mark.gen_test
def test_get_rank_no_connection():
server = TChannel('server')
server.listen()
peer = Peer(TChannel('test'), '10.10.101.21:230')
calculator = PreferIncomingCalculator()
assert sys.maxint == calculator.get_rank(peer)
3
Example 19
Project: tchannel-python Source File: test_thriftrw.py
@pytest.mark.gen_test
def test_call_thrift_exception(keyvalue, call):
with pytest.raises(keyvalue.ItemAlreadyExists) as exc_info:
yield call(
keyvalue.Service.putItem(
keyvalue.Item('hello', keyvalue.Value(stringValue='foo')),
True,
)
)
assert 'this item already exists' in str(exc_info)
3
Example 20
Project: tchannel-python Source File: test_queue.py
@pytest.mark.gen_test
def test_put_then_get_nowait(items):
queue = Queue()
for item in items:
yield queue.put(item)
got = []
for i in range(len(items)):
got.append(queue.get_nowait())
assert got == items
with pytest.raises(QueueEmpty):
queue.get_nowait()
future = queue.get()
yield queue.put(42)
assert 42 == (yield future)
3
Example 21
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_old_recording_without_tracing(mock_server, tracer):
from tchannel import TChannel
# an existing recording that does not contain tracing information
path = os.path.join(
os.path.dirname(__file__), 'data', 'old_without_tracing.yaml'
)
ch = TChannel('client', trace=True, tracer=tracer)
mock_server.expect_call('hello', 'json').and_write('world').once()
with vcr.use_cassette(path, record_mode=vcr.RecordMode.NONE):
response = yield ch.json(
hostport=mock_server.hostport,
service='hello_service',
endpoint='hello',
body='world',
)
assert 'world' == response.body
3
Example 22
Project: tchannel-python Source File: test_server.py
@pytest.mark.gen_test
def test_replay(cassette, call):
allow(cassette).can_replay.and_return(True)
expect(cassette).replay.and_return(
proxy.Response(
code=0, headers='{key: value}', body='response body'
)
)
response = yield call('endpoint', 'request body')
assert response.body.code == 0
assert response.body.body == 'response body'
assert response.body.headers == '{key: value}'
3
Example 23
@pytest.mark.gen_test
def test_call(mock_server, thrift_service):
mock_server.expect_call(
thrift_service,
'thrift',
method='putItem',
).and_result(None)
client = mk_client(thrift_service, mock_server.port)
yield client.putItem(
thrift_service.Item(
key="foo",
value=thrift_service.Value(stringValue='bar')
),
True
)
3
Example 24
Project: tchannel-python Source File: test_server.py
@pytest.mark.gen_test
def test_no_peers(vcr_service, cassette, vcr_hostport):
allow(cassette).can_replay.and_return(False)
vcr_request = proxy.Request(
serviceName='hello_service',
endpoint='hello',
headers='',
body='body',
)
with pytest.raises(proxy.NoPeersAvailableError):
yield TChannel('foo').thrift(
proxy.VCRProxy.send(vcr_request),
hostport=vcr_hostport,
)
3
Example 25
Project: tchannel-python Source File: test_thrift.py
@pytest.mark.gen_test
@pytest.mark.call
def test_client_for_with_sync_tchannel_and_injected_thread_loop(
server,
ThriftTest,
loop,
):
tchannel = SyncTChannel(name='client', threadloop=loop)
client = sync_client_for('server', _ThriftTest)(
tchannel=tchannel,
hostport=server.hostport,
)
assert client.testString(thing='foo')
3
Example 26
Project: tchannel-python Source File: test_context.py
@pytest.mark.gen_test
def test_deprecated_context_provider():
"""
Test that the deprecated RequestContextProvider() can still propagate
the request context across coroutines.
"""
provider = context.RequestContextProvider()
@gen.coroutine
def _get_context():
res = provider.get_current_context().parent_tracing
raise gen.Return(res)
with provider.request_context(parent_tracing='Farnsworth'):
res = _get_context()
res = yield res
assert res == 'Farnsworth'
3
Example 27
Project: tchannel-python Source File: test_server.py
@pytest.mark.gen_test
def test_protocol_error(vcr_service, cassette, call, mock_server):
allow(cassette).can_replay.and_return(False)
expect(cassette).record.never()
mock_server.expect_call('endpoint').and_raise(
TChannelError.from_code(1, description='great sadness')
)
with pytest.raises(proxy.RemoteServiceError) as exc_info:
yield call('endpoint', 'body')
assert 'great sadness' in str(exc_info)
assert exc_info.value.code == 1
3
Example 28
Project: tchannel-python Source File: test_sync_client.py
@pytest.mark.gen_test
def test_protocol_exception(tmpdir, mock_server):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_raise(
Exception('great sadness')
).once()
with pytest.raises(UnexpectedError):
with vcr.use_cassette(str(path)):
client = TChannel('test')
client.raw(
'hello_service',
'hello',
'world',
hostport=mock_server.hostport,
).result(1)
assert not path.check() # nothing should've been recorded
3
Example 29
Project: tchannel-python Source File: test_dispatch.py
@pytest.mark.gen_test
def test_routing_delegate_is_propagated_json():
server = TChannel('server')
server.listen()
@server.json.register('foo')
def handler(request):
assert request.transport.routing_delegate == 'delegate'
return {'success': True}
client = TChannel('client', known_peers=[server.hostport])
res = yield client.json('service', 'foo', {}, routing_delegate='delegate')
assert res.body == {'success': True}
3
Example 30
Project: tchannel-python Source File: test_tornado_client.py
@pytest.mark.gen_test
def test_thrift_exception(mock_server, thrift_service):
mock_server.expect_call(
thrift_service,
'thrift',
method='getItem',
).and_raise(thrift_service.ItemDoesNotExist("stahp"))
client = mk_client(thrift_service, mock_server.port, trace=False)
with (
pytest.raises(thrift_service.ItemDoesNotExist)
) as excinfo:
yield client.getItem("foo")
assert 'stahp' in str(excinfo.value)
3
Example 31
Project: tchannel-python Source File: test_event.py
@pytest.mark.gen_test
def test_after_send_error_event_called():
tchannel = TChannel('test')
tchannel.listen()
with mock.patch(
'tchannel.event.EventEmitter.fire', autospec=True,
) as mock_fire:
mock_fire.return_value = None
with pytest.raises(BadRequestError):
yield tchannel.call(
scheme=schemes.RAW,
service='test',
arg1='endpoint',
hostport=tchannel.hostport,
timeout=0.3,
)
mock_fire.assert_any_call(
mock.ANY, EventType.after_send_error, mock.ANY,
)
3
Example 32
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_protocol_exception(tmpdir, mock_server, call):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_raise(
Exception('great sadness')
).once()
with pytest.raises(UnexpectedError):
with vcr.use_cassette(str(path)):
yield call('hello', 'world', service='hello_service')
assert not path.check() # nothing should've been recorded
3
Example 33
Project: tchannel-python Source File: test_forwarding.py
@pytest.mark.gen_test
def test_forwarding_thrift_success(keyvalue, client, keyvalue_data):
keyvalue_data['hello'] = 'world'
response = yield client.thrift(
keyvalue.KeyValue.getItem('hello'),
headers={'expect': 'success'},
)
assert response.body == 'world'
3
Example 34
Project: tchannel-python Source File: test_thriftrw.py
@pytest.mark.gen_test
def test_call_success(keyvalue, call):
response = yield call(
keyvalue.Service.getItem('foo')
)
assert response.body == keyvalue.Item(
'hello', keyvalue.Value(stringValue='foo')
)
3
Example 35
Project: tchannel-python Source File: test_future.py
@pytest.mark.gen_test
def test_fail_to_failure():
answer = gen.Future()
@fail_to(answer)
def f():
raise GreatSadness
assert f() is None
with pytest.raises(GreatSadness):
yield answer
3
Example 36
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_use_cassette_as_decorator_with_inject(tmpdir, mock_server, call):
path = tmpdir.join('data.yaml')
mock_server.expect_call('hello').and_raise(Exception('great sadness'))
@gen.coroutine
@vcr.use_cassette(str(path), inject=True)
def f(cassette):
with pytest.raises(UnexpectedError):
yield call('hello', 'world', service='hello_service')
assert len(cassette.data) == 0
assert cassette.play_count == 0
yield f()
yield f()
3
Example 37
Project: tchannel-python Source File: test_health.py
@pytest.mark.gen_test
def test_default_health():
server = TChannel("health_test_server")
server.listen()
client = TChannel("health_test_client")
service = thrift.load(
path='tchannel/health/meta.thrift',
service='health_test_server',
hostport=server.hostport,
)
resp = yield client.thrift(service.Meta.health())
assert resp.body.ok is True
assert resp.body.message is None
3
Example 38
@pytest.mark.gen_test
def test_invalid_headers():
server = TChannel('server')
server.listen()
client = TChannel('client')
with pytest.raises(ValueError) as exc_info:
yield client.json(
service='foo',
endpoint='bar',
hostport=server.hostport,
headers={'foo': ['bar']},
)
assert 'headers must be a map[string]string' in str(exc_info)
3
Example 39
Project: tchannel-python Source File: test_messages.py
@pytest.mark.gen_test
def test_init_req_header():
with mock.patch.object(
TornadoConnection,
'_extract_handshake_headers',
side_effect=verify_init_header,
) as mock_extract:
mock_extract.return_value = None
server = TChannel('test_server')
server.listen()
yield TornadoConnection.outgoing(server.hostport)
3
Example 40
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_record_into_nonexistent_directory(tmpdir, mock_server, call,
get_body):
path = tmpdir.join('somedir/data.yaml')
mock_server.expect_call('hello').and_write('world').once()
with vcr.use_cassette(str(path)) as cass:
response = yield call('hello', 'world', service='hello_service')
assert 'world' == (yield get_body(response))
assert cass.play_count == 0
assert path.check(file=True)
with vcr.use_cassette(str(path)) as cass:
response = yield call('hello', 'world', service='hello_service')
assert 'world' == (yield get_body(response))
assert cass.play_count == 1
3
Example 41
Project: tchannel-python Source File: test_peer_strategy.py
@pytest.mark.gen_test
def test_get_rank_with_outgoing():
server = TChannel('server')
server.listen()
connection = yield TornadoConnection.outgoing(server.hostport)
peer = Peer(TChannel('test'), '10.10.101.21:230')
calculator = PreferIncomingCalculator()
peer.register_outgoing_conn(connection)
assert PreferIncomingCalculator.TIERS[1] == calculator.get_rank(peer)
3
Example 42
Project: tchannel-python Source File: test_client_server.py
@pytest.mark.gen_test
def test_tcurl(mock_server):
endpoint = b'tcurltest'
mock_server.expect_call(endpoint).and_write(
headers=endpoint,
body="hello"
)
hostport = 'localhost:%d' % mock_server.port
response = yield tcurl.main([
'--host', hostport,
'--endpoint', endpoint,
'--service', 'mock-server',
'--raw',
])
assert response.headers == endpoint
assert response.body == "hello"
3
Example 43
Project: tchannel-python Source File: test_queue.py
@pytest.mark.gen_test
def test_put_then_get(items):
queue = Queue()
for item in items:
yield queue.put(item)
got_futures = []
for i in range(len(items)):
got_futures.append(queue.get())
got = yield got_futures
assert got == items
3
Example 44
Project: tchannel-python Source File: test_vcr.py
@pytest.mark.gen_test
def test_old_recording_with_tracing(mock_server, tracer):
from tchannel import TChannel
# an existing recording that contains tracing information
path = os.path.join(
os.path.dirname(__file__), 'data', 'old_with_tracing.yaml'
)
ch = TChannel('client', trace=True, tracer=tracer)
mock_server.expect_call('hello', 'json').and_write('world').once()
with vcr.use_cassette(path, record_mode=vcr.RecordMode.NONE):
response = yield ch.json(
hostport=mock_server.hostport,
service='hello_service',
endpoint='hello',
body='world',
)
assert 'world' == response.body
3
Example 45
Project: tchannel-python Source File: test_thrift.py
@pytest.mark.gen_test
@pytest.mark.call
def test_oneway(server, service, ThriftTest):
# Given this test server:
# TODO - server should raise same exception as client
with pytest.raises(AssertionError):
@server.thrift.register(ThriftTest)
def testOneway(request):
pass
# Make a call:
tchannel = TChannel(name='client')
with pytest.raises(OneWayNotSupportedError):
yield tchannel.thrift(service.testOneway(1))
3
Example 46
Project: tchannel-python Source File: test_client_server.py
@pytest.mark.gen_test
def test_tornado_client_with_server_not_there():
with pytest.raises(NetworkError):
yield StreamConnection.outgoing(
# Try a random port that we're not listening on.
# This should fail.
'localhost:41942'
)
3
Example 47
@pytest.mark.gen_test
def test_endpoint_not_found(mock_server):
tchannel = TChannel(name='test')
with pytest.raises(BadRequestError):
yield tchannel.raw(
service='test-server',
endpoint='fooo',
hostport=mock_server.hostport,
)
3
Example 48
@pytest.mark.gen_test
def test_unexpected_error(mock_server, thrift_service):
mock_server.expect_call(
thrift_service,
'thrift',
method='getItem',
).and_raise(ValueError("I was not defined in the IDL"))
client = mk_client(thrift_service, mock_server.port, trace=False)
with pytest.raises(errors.UnexpectedError):
yield client.getItem("foo")
3
Example 49
Project: tchannel-python Source File: test_client.py
@pytest.mark.gen_test
@pytest.mark.parametrize('register_endpoint, make_request', [
(register_json, request_json),
(register_raw, request_raw),
(register_thrift, request_thrift),
(register_from_top, request_raw),
])
def test_sync_register(register_endpoint, make_request):
sync_client = TChannel('test-client')
register_endpoint(sync_client)
sync_client.listen()
async_client = AsyncTchannel('async')
with pytest.raises(BadRequestError):
yield make_request(async_client, sync_client.hostport)
with pytest.raises(BadRequestError):
yield make_request(sync_client, sync_client.hostport)
3
Example 50
Project: tchannel-python Source File: test_checksum.py
@pytest.mark.gen_test
def test_default_checksum_type():
server = TChannel("server")
server.listen()
with mock.patch(
'tchannel.messages.common.compute_checksum', autospec=True,
) as mock_compute_checksum:
client = TChannel("client")
service = thrift.load(
path='tchannel/health/meta.thrift',
service='health_test_server',
hostport=server.hostport,
)
with pytest.raises(FatalProtocolError):
yield client.thrift(service.Meta.health())
mock_compute_checksum.assert_called_with(
ChecksumType.crc32c, mock.ANY, mock.ANY,
)