def test_keep_alive(default_service_entry, default_service_endpoint):
    client, service, server = _setup()
    keepalive = DirectoryRegistrationKeepAlive(client)
    name = default_service_entry.name
    assert name not in service.service_entries
    keepalive.start(name, default_service_entry.type, default_service_entry.authority,
                    default_service_endpoint.host_ip, default_service_endpoint.port)
    assert name in service.service_entries

    with keepalive:
        # Just have some statement inside the "with" context.
        assert service.service_entries[name] == default_service_entry

    # Now that we've exited the "with" block, the internal thread should have ended and the service
    # should be unregistered.
    assert not keepalive.is_alive()
    assert name not in service.service_entries
def test_keep_alive_update(default_service_entry, default_service_endpoint):
    client, service, server = _setup()
    interval_seconds = 0.1
    keepalive = DirectoryRegistrationKeepAlive(client, rpc_interval_seconds=interval_seconds)
    name = default_service_entry.name
    assert name not in service.service_entries

    client.register(default_service_entry.name, default_service_entry.type,
                    default_service_entry.authority, default_service_endpoint.host_ip,
                    default_service_endpoint.port)

    new_authority = default_service_entry.authority + 'woo-hoo'
    keepalive.start(name, default_service_entry.type, new_authority,
                    default_service_endpoint.host_ip, default_service_endpoint.port)
    assert service.service_entries[name].authority == new_authority

    # Make sure the thread is still alive after a few loops.
    time.sleep(interval_seconds * 3)
    assert keepalive.is_alive()