def test_record_success_thrift(tmpdir, thrift_service, mock_server): myservice = thrift_request_builder( 'myservice', thrift_service, hostport=mock_server.hostport ) path = tmpdir.join('data.yaml') expected_item = thrift_service.Item( 'foo', thrift_service.Value(stringValue='bar') ) mock_server.expect_call(thrift_service, method='getItem').and_result( expected_item ).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 1
def test_record_success_no_hostport_new_channels(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_write('world').once() with vcr.use_cassette(str(path)) as cass: client = TChannel('test', known_peers=[mock_server.hostport]) response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: client = TChannel('test', known_peers=[mock_server.hostport]) response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 1
def test_record_success_new_channels(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call( 'hello', scheme='json', ).and_write('world').once() with vcr.use_cassette(str(path)) as cass: client = TChannel('test') response = client.json( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: client = TChannel('test') response = client.json( 'hello_service', 'hello', 'world', hostport=mock_server.hostport, ).result(timeout=1) assert 'world' == response.body assert cass.play_count == 1
def test_record_success_no_hostport(tmpdir, mock_server): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_write('world').once() client = TChannel('test', known_peers=[mock_server.hostport]) with vcr.use_cassette(str(path)) as cass: response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert b'world' == response.body assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.raw( 'hello_service', 'hello', 'world', ).result(timeout=1) assert b'world' == response.body assert cass.play_count == 1
def test_record_success_thrift(tmpdir, thrift_service, mock_server): myservice = thrift_request_builder('myservice', thrift_service, hostport=mock_server.hostport) path = tmpdir.join('data.yaml') expected_item = thrift_service.Item( 'foo', thrift_service.Value(stringValue='bar')) mock_server.expect_call(thrift_service, method='getItem').and_result(expected_item).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: response = client.thrift(myservice.getItem('foo')).result(1) item = response.body assert item == expected_item assert cass.play_count == 1
def test_record_thrift_exception(tmpdir, mock_server, thrift_service): path = tmpdir.join('data.yaml') myservice = thrift_request_builder( 'myservice', thrift_service, hostport=mock_server.hostport ) mock_server.expect_call(thrift_service, method='getItem').and_raise( thrift_service.ItemDoesNotExist('foo') ).once() client = TChannel('test') with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): client.thrift(myservice.getItem('foo')).result(1) assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): client.thrift(myservice.getItem('foo')).result(1) assert cass.play_count == 1
def test_record_success_thrift(tmpdir, mock_server, thrift_service, thrift_client): path = tmpdir.join("data.yaml") expected_item = thrift_service.Item("foo", thrift_service.Value(stringValue="bar")) mock_server.expect_call(thrift_service, method="getItem").and_result(expected_item).once() with vcr.use_cassette(str(path)) as cass: item = yield thrift_client.getItem("foo") assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: item = yield thrift_client.getItem("foo") assert item == expected_item assert cass.play_count == 1
def test_record_success(tmpdir, mock_server, call): 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 response.get_body()) 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 response.get_body()) assert cass.play_count == 1
def test_record_thrift_exception(tmpdir, mock_server, thrift_service, thrift_client): path = tmpdir.join("data.yaml") mock_server.expect_call(thrift_service, method="getItem").and_raise(thrift_service.ItemDoesNotExist("foo")).once() with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): yield thrift_client.getItem("foo") assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): yield thrift_client.getItem("foo") assert cass.play_count == 1
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
def test_record_success(tmpdir, mock_server, call): 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 response.get_body()) 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 response.get_body()) assert cass.play_count == 1
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
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 b'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 b'world' == (yield get_body(response)) assert cass.play_count == 1
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
def test_record_thrift_exception(tmpdir, mock_server, thrift_service, thrift_client): path = tmpdir.join('data.yaml') mock_server.expect_call(thrift_service, method='getItem').and_raise( thrift_service.ItemDoesNotExist('foo')).once() with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): yield thrift_client.getItem('foo') assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: with pytest.raises(thrift_service.ItemDoesNotExist): yield thrift_client.getItem('foo') assert cass.play_count == 1
def test_record_success_with_ttl(tmpdir, mock_server, call, get_body): path = tmpdir.join('data.yaml') mock_server.expect_call('hello').and_write('world', delay=0.1).once() with vcr.use_cassette(str(path)) as cass: response = yield call('hello', 'world', service='hello_service', ttl=0.2) 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', ttl=0.05) # shouldn't time out assert 'world' == (yield get_body(response)) assert cass.play_count == 1
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(ProtocolError): with vcr.use_cassette(str(path)): yield call("hello", "world") assert not path.check() # nothing should've been recorded
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
def test_record_success_thrift(tmpdir, mock_server, thrift_service, thrift_client): path = tmpdir.join('data.yaml') expected_item = thrift_service.Item( 'foo', thrift_service.Value(stringValue='bar')) mock_server.expect_call(thrift_service, method='getItem').and_result(expected_item).once() with vcr.use_cassette(str(path)) as cass: item = yield thrift_client.getItem('foo') assert item == expected_item assert cass.play_count == 0 assert path.check(file=True) with vcr.use_cassette(str(path)) as cass: item = yield thrift_client.getItem('foo') assert item == expected_item assert cass.play_count == 1
def test_vcr_with_tracing( tmpdir, mock_server, tracer, tracing_before, tracing_after ): from tchannel import TChannel mock_server.expect_call('hello', 'json').and_write('world').once() path = tmpdir.join('data.yaml') if tracing_before: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path)) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) if tracing_after: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path), record_mode=vcr.RecordMode.NONE) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 1
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') assert not path.check() # nothing should've been recorded
def test_vcr_with_tracing(tmpdir, mock_server, tracer, tracing_before, tracing_after): from tchannel import TChannel mock_server.expect_call('hello', 'json').and_write('world').once() path = tmpdir.join('data.yaml') if tracing_before: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path)) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 0 assert path.check(file=True) if tracing_after: ch = TChannel('client', trace=True, tracer=tracer) else: ch = TChannel('client') with vcr.use_cassette(str(path), record_mode=vcr.RecordMode.NONE) as cass: response = yield ch.json( hostport=mock_server.hostport, service='hello_service', endpoint='hello', body='world', ) assert 'world' == response.body assert cass.play_count == 1
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
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
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
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
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