def test_client_on_error(protocol): # In this particular test, when you write two tests in a row, you are testing the following case: # # You are testing exception in client's callback, not error in client's request generator # 1. The exception breaks the `async for req in stub.Call(req_iter)` on the client # 2. Server probably has something hold in the stream # 3. Restart the client, keep server untouched. # 4. Now, server stucks (because it considers the last connection wasn't end yet) def validate(x): raise NotImplementedError with Flow(protocol=protocol).add() as f: t = 0 try: f.index( from_ndarray(np.random.random([5, 4])), on_done=validate, continue_on_error=False, ) except BadClientCallback: # bad client callback will break the `async for req in stub.Call(req_iter)` t = 1 # now query the gateway again, make sure gateway's channel is still usable f.index( from_ndarray(np.random.random([5, 4])), on_done=validate, continue_on_error=True, ) assert t == 1
def test_add_needs_inspect(tmpdir): f1 = (Flow().add(name='executor0', needs='gateway').add( name='executor1', needs='gateway').inspect().needs(['executor0', 'executor1'])) with f1: _ = f1.index(from_ndarray(np.random.random([5, 5]))) f2 = Flow.load_config('yaml/flow-v1.0-syntax.yml') with f2: _ = f2.index(from_ndarray(np.random.random([5, 5]))) assert f1 == f2
async def run_async_flow_5s(protocol): with Flow(protocol=protocol, asyncio=True).add(uses=Wait5s) as f: async for r in f.index( from_ndarray(np.random.random([num_docs, 4])), on_done=validate, ): assert isinstance(r.response, Response)
async def run_async_flow_5s(protocol): with Flow(protocol=protocol, asyncio=True, timeout_send=6000).add(uses=Wait5s) as f: async for r in f.index( from_ndarray(np.random.random([num_docs, 4])), on_done=validate, ): assert isinstance(r, DocumentArray)
async def test_run_async_flow(protocol, mocker, flow_cls): r_val = mocker.Mock() with flow_cls(protocol=protocol, asyncio=True).add() as f: async for r in f.index(from_ndarray(np.random.random([num_docs, 4])), on_done=r_val): assert isinstance(r.response, Response) validate_callback(r_val, validate)
def test_flow_needs_all(protocol): f = Flow(protocol=protocol).add(name='p1', needs='gateway').needs_all(name='r1') assert f._deployment_nodes['r1'].needs == {'p1'} f = (Flow(protocol=protocol).add(name='p1', needs='gateway').add( name='p2', needs='gateway').add(name='p3', needs='gateway').needs( needs=['p1', 'p2'], name='r1').needs_all(name='r2')) assert f._deployment_nodes['r2'].needs == {'p3', 'r1'} with f: f.index(from_ndarray(np.random.random([10, 10]))) f = (Flow(protocol=protocol).add(name='p1', needs='gateway').add( name='p2', needs='gateway').add(name='p3', needs='gateway').needs( needs=['p1', 'p2'], name='r1').needs_all(name='r2').add(name='p4', needs='r2')) assert f._deployment_nodes['r2'].needs == {'p3', 'r1'} assert f._deployment_nodes['p4'].needs == {'r2'} with f: f.index(from_ndarray(np.random.random([10, 10]))) _validate_flow(f)
def test_return_results_sync_flow(protocol, on_done): with Flow(protocol=protocol).add() as f: da = f.index( from_ndarray(np.random.random([10, 2])), on_done=on_done, ) if on_done is None: assert isinstance(da, DocumentArray) assert len(da) == 10 for doc in da: assert isinstance(doc, Document) else: assert da is None _validate_flow(f)
def test_flow_on_error_callback(protocol): f = Flow(protocol=protocol).add(uses=DummyCrafterNotImplemented) hit = [] def f1(*args): hit.append('done') def f2(*args): hit.append('error') def f3(*args): hit.append('always') with f: f.index( from_ndarray(np.random.random([10, 10])), on_done=f1, on_error=f2, on_always=f3, ) assert hit == ['error', 'always'] hit.clear()
async def test_return_results_async_flow(protocol, flow_cls): with flow_cls(protocol=protocol, asyncio=True).add() as f: async for r in f.index(from_ndarray(np.random.random([10, 2]))): assert isinstance(r, DocumentArray)
async def test_return_results_async_flow(return_results, protocol, flow_cls): with flow_cls(protocol=protocol, asyncio=True, return_results=return_results).add() as f: async for r in f.index(from_ndarray(np.random.random([10, 2]))): assert isinstance(r.response, Response)