def test_advertise_is_retryable(router_file): from tchannel.tornado.response import Response as TornadoResponse def new_advertise(*args, **kwargs): f = gen.Future() f.set_result( TornadoResponse( argstreams=[closed_stream(b'{}') for i in range(3)], )) return f tchannel = TChannel(name='client') with patch('tchannel.tornado.TChannel.advertise', autospec=True) as mock_advertise: f = gen.Future() f.set_exception(Exception('great sadness')) mock_advertise.return_value = f with pytest.raises(Exception) as e: yield tchannel.advertise(router_file=router_file) assert 'great sadness' in str(e) assert mock_advertise.call_count == 1 mock_advertise.side_effect = new_advertise yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) assert mock_advertise.call_count == 2
def test_advertise_is_retryable(router_file): from tchannel.tornado.response import Response as TornadoResponse def new_advertise(*args, **kwargs): f = gen.Future() f.set_result(TornadoResponse( argstreams=[closed_stream(b'{}') for i in range(3)], )) return f tchannel = TChannel(name='client') with patch( 'tchannel.tornado.TChannel.advertise', autospec=True ) as mock_advertise: f = gen.Future() f.set_exception(Exception('great sadness')) mock_advertise.return_value = f with pytest.raises(Exception) as e: yield tchannel.advertise(router_file=router_file) assert 'great sadness' in str(e) assert mock_advertise.call_count == 1 mock_advertise.side_effect = new_advertise yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) assert mock_advertise.call_count == 2
def test_advertise_should_raise_on_invalid_router_file(): tchannel = TChannel(name='client') with pytest.raises(IOError): yield tchannel.advertise(router_file='?~~lala') with pytest.raises(ValueError): yield tchannel.advertise(routers='lala', router_file='?~~lala')
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
def test_advertise_is_idempotent(router_file): from tchannel.tornado.response import Response as TornadoResponse def new_advertise(*args, **kwargs): f = gen.Future() f.set_result( TornadoResponse( argstreams=[closed_stream(b'{}') for i in range(3)], )) return f tchannel = TChannel(name='client') with patch('tchannel.tornado.TChannel.advertise', autospec=True) as mock_advertise: mock_advertise.side_effect = new_advertise yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) assert mock_advertise.call_count == 1
def test_advertise_should_take_a_router_file(router_file): from tchannel.tornado.response import Response as TornadoResponse tchannel = TChannel(name='client') with open(router_file, 'r') as json_data: routers = json.load(json_data) with (patch( 'tchannel.tornado.TChannel.advertise', autospec=True, )) as mock_advertise: f = gen.Future() mock_advertise.return_value = f f.set_result(TornadoResponse()) tchannel.advertise(router_file=router_file) mock_advertise.assert_called_once_with(ANY, routers=routers, name=ANY, timeout=ANY)
def test_advertise_should_take_a_router_file(router_file): from tchannel.tornado.response import Response as TornadoResponse tchannel = TChannel(name='client') with open(router_file, 'r') as json_data: routers = json.load(json_data) with ( patch( 'tchannel.tornado.TChannel.advertise', autospec=True, ) ) as mock_advertise: f = gen.Future() mock_advertise.return_value = f f.set_result(TornadoResponse()) tchannel.advertise(router_file=router_file) mock_advertise.assert_called_once_with(ANY, routers=routers, name=ANY, timeout=ANY)
def test_advertise_is_idempotent(router_file): from tchannel.tornado.response import Response as TornadoResponse def new_advertise(*args, **kwargs): f = gen.Future() f.set_result(TornadoResponse( argstreams=[closed_stream(b'{}') for i in range(3)], )) return f tchannel = TChannel(name='client') with patch( 'tchannel.tornado.TChannel.advertise', autospec=True ) as mock_advertise: mock_advertise.side_effect = new_advertise yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) yield tchannel.advertise(router_file=router_file) assert mock_advertise.call_count == 1
def test_advertise_should_take_a_router_file(): host_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data/hosts.json', ) tchannel = TChannel(name='client') with open(host_path, 'r') as json_data: routers = json.load(json_data) with ( patch( 'tchannel.tornado.TChannel.advertise', autospec=True, ) ) as mock_advertise: f = gen.Future() mock_advertise.return_value = f f.set_result(Response()) tchannel.advertise(router_file=host_path) mock_advertise.assert_called_once_with(ANY, routers=routers, name=ANY, timeout=ANY)