import asyncio
import time

import aiohttp


async def wait_tika_startup(session: aiohttp.ClientSession, port, timeout=10.0):
    timeout += time.perf_counter()
    while True:
        try:
            async with session.get(f"http://127.0.0.1:{port}/version", timeout=5.0) as r:
                if r.status == 200 and (await r.text()).startswith("Apache Tika "):
                    break
        except aiohttp.ClientError:
            if time.perf_counter() > timeout:
                raise
            await asyncio.sleep(0.75)
        else:
            assert time.perf_counter() <= timeout
