def test_env_changer_new(): assert 'BLAH' not in biodome.environ with biodome.env_change('BLAH', 123): assert biodome.environ.get('BLAH', 0) == 123 assert 'BLAH' not in biodome.environ
def test_send_double(db_fixture, db_pool_session, venus_runner): loop, port = venus_runner message_uuids1 = [str(uuid4()) for i in range(10)] message_uuids2 = [str(uuid4()) for i in range(10)] env1 = dict(SENDER_ITEMS=str(message_uuids1)) env2 = dict(SENDER_ITEMS=str(message_uuids2)) with biodome.env_change('MAX_BATCH_SIZE', 1): proc1 = run_app(port, iterations=10, env=env1) proc2 = run_app(port, iterations=10, env=env2) loop.run_until_complete(asyncio.sleep(3)) if proc1.poll() is None: proc1.kill() if proc2.poll() is None: proc2.kill() # Fetch records from the DB to verify that the log messages arrived. async def get(): # Cannot use the db_pool fixture, because it mutates the # db.DATABASE_POOL global, which is what main.amain *also* does. async with db_pool_session.acquire() as conn: conn: Connection return await conn.fetch('SELECT * FROM logs') records = loop.run_until_complete(get()) pprint(records) logged_message_ids = {r['message'] for r in records} pprint(logged_message_ids) assert logged_message_ids.issuperset(message_uuids1) assert logged_message_ids.issuperset(message_uuids2)
def test_env_changer_existing(): biodome.environ['BLAH'] = 456 with biodome.env_change('BLAH', 123): assert biodome.environ.get('BLAH', 0) == 123 assert biodome.environ.get('BLAH', 0) == 456 del biodome.environ['BLAH']
def test_extra(db_fixture, db_pool_session, venus_runner): loop, port = venus_runner messages = [ dict( message='blah blah blah', correlation_id=str(uuid4()), random_timing_data=1.23, ) for i in range(10) ] env = dict(SENDER_ITEMS=repr(messages)) with biodome.env_change('MAX_BATCH_SIZE', 1): proc = run_app(port, iterations=10, delay=0.2, env=env) loop.run_until_complete(asyncio.sleep(3)) if proc.poll() is None: proc.kill() # Fetch records from the DB to verify that the log messages arrived. async def get(): # Cannot use the db_pool fixture, because it mutates the # db.DATABASE_POOL global, which is what main.amain *also* does. async with db_pool_session.acquire() as conn: conn: Connection return await conn.fetch('SELECT * FROM logs') records = loop.run_until_complete(get()) expected_correlation_ids = {m['correlation_id'] for m in messages} pprint(records) my_data = [r for r in records if str(r['correlation_id']) in expected_correlation_ids] pprint(my_data) rec = my_data[0] assert rec['message'] == 'blah blah blah' data = json.loads(rec['data']) assert data['filename'] == 'sender.py' assert data['pathname'] == 'tests/sender.py' assert data['random_timing_data'] == 1.23
def venus_runner(loop): port = portpicker.pick_unused_port() with biodome.env_change('VENUS_PORT', port), \ io.zmq_context(): venus_main_task = loop.create_task(main.amain(None)) yield loop, port
def test_query(): responses = [] dbname = '34feba8fb61144dfb5cb9d4ecdd08683' sql = 'SELECT * from frame_posting WHERE frame_id = 1' url = f'http://127.0.0.1:8080/{dbname}/{parse.quote_plus(sql)}' printl(url) payload = json.dumps(dict(dbname=dbname, query=sql)) print(1) ctx = Context() print(2) s: Socket = ctx.socket(zmq.ROUTER) s.setsockopt(zmq.ROUTER_MANDATORY, 1) print(3) port = s.bind_to_random_port(addr='tcp://*') print(f'Bound to port: {port}') async def test(): printl(time.perf_counter()) msg = payload async def receiver(): while True: try: print(f'Waiting for reply...') m = await asyncio.wait_for(s.recv_multipart(), .01) break except asyncio.TimeoutError: print('timed out waiting') continue print('got message back') responses.append(m[1].decode()) print(m) return async with server(): t = asyncio.create_task(receiver()) print(f'Sending string: {msg}') while True: try: print('trying to send...') await s.send_multipart([b'NO_IDENTITY', msg.encode()]) break except Exception as e: print(e) await asyncio.sleep(0.1) continue await t with biodome.env_change('TARGET_SERVER_PORT', port), \ biodome.env_change('TARGET_SERVER_URL', '127.0.0.1'): asyncio.run(test()) s.close(1) ctx.destroy() printl(responses) print('leaving test!') assert responses[0] == ( '[(1, 0, 194), (1, 1, 1101), (1, 2, 12), (1, 3, 23), (1, 4, 247), (1, 5, 2), ' '(1, 6, 94), (1, 7, 196), (1, 8, 56), (1, 9, 9), (1, 10, 127), (1, 11, 13), ' '(1, 12, 11), (1, 13, 4), (1, 14, 508), (1, 15, 181), (1, 16, 314), (1, 17, ' '19), (1, 18, 144), (1, 19, 35), (1, 20, 128), (1, 21, 75), (1, 22, 15), (1, ' '23, 1), (1, 24, 426), (1, 25, 2), (1, 26, 160), (1, 27, 102), (1, 28, 9), ' '(1, 29, 245), (1, 30, 3817), (1, 31, 1), (1, 32, 276), (1, 33, 4), (1, 34, ' '23), (1, 35, 98), (1, 36, 940), (1, 37, 2), (1, 38, 1), (1, 39, 57), (1, 40, ' '4), (1, 41, 1734), (1, 42, 1344), (1, 43, 4), (1, 44, 18), (1, 45, 75), (1, ' '46, 3), (1, 47, 17), (1, 48, 350), (1, 49, 177), (1, 50, 313), (1, 51, 1), ' '(1, 52, 2117), (1, 53, 20), (1, 54, 4), (1, 55, 198), (1, 56, 890), (1, 57, ' '3), (1, 58, 290), (1, 59, 4), (1, 60, 92), (1, 61, 2), (1, 62, 774), (1, 63, ' '13), (1, 64, 36), (1, 65, 56), (1, 66, 80), (1, 67, 39), (1, 68, 92), (1, ' '69, 521), (1, 70, 2), (1, 71, 6), (1, 72, 1276), (1, 73, 5099), (1, 74, ' '389), (1, 75, 31), (1, 76, 199), (1, 77, 23), (1, 78, 30), (1, 79, 593), (1, ' '80, 8), (1, 81, 5740)]' )