Exemplo n.º 1
0
def empty_client(request, requests_mock, cache_dir, clear_ioloop_instance):
    client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    client.install()

    request.addfinalizer(lambda: client.stop(3))
    request.addfinalizer(Component.value_processors.clear)
    return client
Exemplo n.º 2
0
def test_start_long_poll_exec_once():
    def poll(*args, **kwds):
        pass

    client = HuskarApiIOLoop('url', 'token')
    client.start_long_poll = mock.Mock(spec=poll)
    assert client.greenlet is None
    client.run()
    client.run()
    time.sleep(1)
    client.start_long_poll.assert_called_once()
Exemplo n.º 3
0
def test_delete_config_value_if_cache_is_not_consistent_with_server(
        requests_mock, client, clear_ioloop_instance, cache_dir):
    client.run()
    assert client.connected.wait(1)
    requests_mock.add_response(
        '{"body": {"config": {"arch.test": {"overall": {"new_config":'
        ' {"value": "new_value_2"}}}}}, "message": "update"}')
    assert requests_mock.wait_processed()
    assert client.watched_configs.get('arch.test', 'overall',
                                      'new_config') == {
                                          'value': 'new_value_2'
                                      }
    assert client.stop(3)

    client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    client.install()
    client.watched_configs.add_watch("arch.test", 'overall')
    client.run()
    assert client.connected.wait(1)
    with pytest.raises(RuntimeError):
        assert client.watched_configs.get('arch.test',
                                          'overall',
                                          'new_config',
                                          raises=True) == {
                                              'value': 'new_value_2'
                                          }
    assert client.stop(3)
def test_should_get_correct_config_from_cache(requests_mock, started_client,
                                              config_component, cache_dir):
    import requests
    from huskar_sdk_v2.http.ioloops.http import HuskarApiIOLoop
    assert started_client.connected.wait(1)
    requests_mock.add_response(
        r'{"body": {"config": {"arch.test": {"overall": {"test_json":'
        r' {"value": "\"abcdefg\""}}}}}, "message": "update"}')
    assert requests_mock.wait_processed()
    assert started_client.watched_configs.get('arch.test',
                                              'overall',
                                              'test_json',
                                              raises=True) == {
                                                  'value': 'abcdefg'
                                              }

    assert started_client.stop(3)

    requests_mock.stop_exception = requests.Timeout
    client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    client.install()
    client.run()

    assert client.watched_configs.get('arch.test',
                                      'overall',
                                      'test_json',
                                      raises=True) == {
                                          'value': 'abcdefg'
                                      }
Exemplo n.º 5
0
def test_should_work_with_illegal_cache_dir(requests_mock):
    client = HuskarApiIOLoop('test_url',
                             'test_token',
                             cache_dir='/that/ileegal')
    client.watched_configs.add_watch("arch.test", 'overall')
    client.run()
    assert_config_value_correct(client)
    requests_mock.set_result_file('test_data_changed.txt')
    assert requests_mock.wait_processed()
    assert_config_new_value_correct(client)
    client.stop()
def test_IOLoop_replacement(cache_dir):
    def func():
        pass

    ioloop = FileCacheIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    ioloop.install()

    ioloop.watched_configs.set_default_fail_strategy_to_raise()

    ioloop.watched_configs.add_watch('foo', 'bar')
    ioloop.watched_configs.add_listener_for_app_id_at_cluster(
        'app_foo', 'cluster_foo', func)
    ioloop.watched_services.add_watch('bar', 'baz')
    ioloop.watched_services.add_listener_for_app_id_at_cluster(
        'app_bar', 'cluster_bar', func)
    ioloop.watched_switches.add_watch('baz', 'foo')
    ioloop.watched_switches.add_listener_for_app_id_at_cluster(
        'app_baz', 'cluster_baz', func)

    assert isinstance(IOLoop.current(), FileCacheIOLoop)

    HuskarApiIOLoop(ioloop.url, ioloop.token, ioloop.cache_dir).install()
    ioloop = IOLoop.current()

    assert isinstance(ioloop, HuskarApiIOLoop)
    configs = ioloop.watched_configs
    services = ioloop.watched_services
    switches = ioloop.watched_switches
    assert 'foo' in configs.app_id_cluster_map
    assert 'bar' in services.app_id_cluster_map
    assert 'baz' in switches.app_id_cluster_map
    assert ('app_foo', 'cluster_foo') in configs.event_listeners
    assert ('app_bar', 'cluster_bar') in services.event_listeners
    assert ('app_baz', 'cluster_baz') in switches.event_listeners
    assert configs.default_fail_strategy == configs.FAIL_STRATEGY_RAISE
    IOLoop.clear_instance()
Exemplo n.º 7
0
def test_config_should_get_data_from_cache_in_time_during_startup(
        requests_mock, clear_ioloop_instance, cache_dir):
    old_client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    old_client.install()

    old_client.watched_configs.add_watch("arch.test", 'overall')
    old_client.run()
    assert old_client.connected.wait(1)
    assert_config_value_correct(old_client)
    requests_mock.stop_exception = requests.Timeout
    assert old_client.stop(3)

    client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=cache_dir)
    client.install()

    client.watched_configs.add_watch("arch.test", 'overall')
    client.run()
    begin = time.time()
    for i in range(100):
        assert_config_value_correct(client)
    assert time.time() - begin < 11
    client.stop()
    shutil.rmtree(client.cache_dir)
Exemplo n.º 8
0
def no_cache_initial_client(request, clear_ioloop_instance):
    client = HuskarApiIOLoop('test_url', 'test_token', cache_dir=None)
    client.install()
    request.addfinalizer(lambda: client.stop(3))
    return client