示例#1
0
def test_failing_advertise_should_raise(mock_server):

    mock_server.expect_call('ad', 'json').and_raise(
        Exception('great sadness')
    )

    routers = [mock_server.tchannel.hostport]
    client = TChannelSyncClient('test-client')

    with pytest.raises(AdvertiseError):
        client.advertise(routers, timeout=0.1)
    def __init__(
        self,
        tchannel,
        logger,
        client_name=None,
        headers={},
        timeout_seconds=30,
        reconfigure_interval_seconds=10,
        deployment_str='prod',
        hyperbahn_host='',
    ):
        self.logger = logger
        self.headers = headers
        self.deployment_str = deployment_str
        self.headers['user-name'] = util.get_username()
        self.headers['host-name'] = socket.gethostname()
        self.timeout_seconds = timeout_seconds
        self.reconfigure_interval_seconds = reconfigure_interval_seconds

        if not tchannel:
            if not client_name:
                raise Exception(
                    "Client name is needed when tchannel not provided")
            elif not hyperbahn_host:
                raise Exception(
                    "Hyperbahn host is needed when tchannel not provided")
            else:
                self.tchannel = TChannelSyncClient(name=client_name)
                self.tchannel.advertise(router_file=hyperbahn_host)
        else:
            self.tchannel = tchannel
示例#3
0
def test_sync_client_should_get_raw_response(mock_server):

    endpoint = 'health'
    mock_server.expect_call(endpoint).and_write(
        headers="",
        body="OK"
    )
    hostport = mock_server.tchannel.hostport

    client = TChannelSyncClient('test-client')
    request = client.request(hostport)

    future = request.send(endpoint, None, "")
    response = future.result()

    assert response.header == ""
    assert response.body == "OK"
示例#4
0
def test_advertise_should_result_in_peer_connections(mock_server):

    body = {"hello": "world"}

    mock_server.expect_call('ad', 'json').and_write(
        headers="",
        body=body,
    )

    routers = [
        mock_server.tchannel.hostport
    ]

    client = TChannelSyncClient('test-client')
    result = client.advertise(routers)

    assert result.header == ""
    # @todo https://github.com/uber/tchannel-python/issues/34
    assert result.body == json.dumps(body)
    assert client._async_client.peers.hosts == routers
示例#5
0
def test_should_discover_ip():

    client = TChannelSyncClient('test-client')
    hostport = client._async_client.hostport

    assert '0.0.0.0:0' != hostport
示例#6
0
def thrift_sync_client(mock_server, thrift_service):
    ServiceClient = client_for("service", thrift_service)
    tchannel_sync = TChannelSyncClient('test-client')
    thrift_sync_client = ServiceClient(tchannel_sync,
                                       hostport=mock_server.hostport)
    return thrift_sync_client
示例#7
0
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""
A client instance that can be used to create publisher or consumer instances.
"""

from __future__ import absolute_import, print_function

import time
import logging

from tchannel.sync import TChannel as TChannelSyncClient
from cherami_client.client import Client


tchannel = TChannelSyncClient(name='example_service', known_peers=['127.0.0.1:4922'])
logger = logging.getLogger('example_client')

while True:
    try:
        client = Client(tchannel, logger)
        print('Success: Created cherami client.')

        break
    except Exception as e:
        logger.exception('Failed to create to cherami: %s', e)
        time.sleep(2)