|
| 1 | +import multiprocessing |
| 2 | +import time |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from docarray import DocumentArray |
| 7 | +from docarray.helper import random_port |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + 'conn_config', |
| 12 | + [ |
| 13 | + (dict(protocol='grpc'), 'grpc://127.0.0.1:$port/'), |
| 14 | + (dict(protocol='grpc'), 'grpc://127.0.0.1:$port'), |
| 15 | + (dict(protocol='websocket'), 'websocket://127.0.0.1:$port'), |
| 16 | + # (dict(protocol='http'), 'http://127.0.0.1:$port'), this somehow does not work on GH workflow |
| 17 | + ], |
| 18 | +) |
| 19 | +@pytest.mark.parametrize('show_pbar', [True, False]) |
| 20 | +def test_post_to_a_flow(show_pbar, conn_config): |
| 21 | + from jina import Flow |
| 22 | + |
| 23 | + def start_flow(stop_event, **kwargs): |
| 24 | + """start a blocking Flow.""" |
| 25 | + with Flow(**kwargs) as f: |
| 26 | + f.block(stop_event=stop_event) |
| 27 | + print('bye') |
| 28 | + |
| 29 | + e = multiprocessing.Event() # create new Event |
| 30 | + |
| 31 | + p = random_port() |
| 32 | + t = multiprocessing.Process( |
| 33 | + name='Blocked-Flow', |
| 34 | + target=start_flow, |
| 35 | + args=(e,), |
| 36 | + kwargs={**conn_config[0], 'port': p}, |
| 37 | + ) |
| 38 | + t.start() |
| 39 | + |
| 40 | + time.sleep(1) |
| 41 | + |
| 42 | + da = DocumentArray.empty(100) |
| 43 | + try: |
| 44 | + da.post(conn_config[1].replace('$port', str(p))) |
| 45 | + except: |
| 46 | + raise |
| 47 | + finally: |
| 48 | + e.set() |
| 49 | + t.join() |
| 50 | + time.sleep(1) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + 'hub_uri', ['jinahub://Hello', 'jinahub+docker://Hello', 'jinahub+sandbox://Hello'] |
| 55 | +) |
| 56 | +def test_post_with_jinahub(hub_uri): |
| 57 | + da = DocumentArray.empty(100) |
| 58 | + da.post(hub_uri) |
| 59 | + |
| 60 | + |
| 61 | +def test_post_bad_scheme(): |
| 62 | + da = DocumentArray.empty(100) |
| 63 | + with pytest.raises(ValueError): |
| 64 | + da.post('haha') |
| 65 | + |
| 66 | + |
| 67 | +def test_endpoint(): |
| 68 | + from jina import Executor, requests, Flow |
| 69 | + |
| 70 | + class MyExec(Executor): |
| 71 | + @requests(on='/foo') |
| 72 | + def foo(self, docs: DocumentArray, **kwargs): |
| 73 | + docs.texts = ['foo'] * len(docs) |
| 74 | + |
| 75 | + @requests(on='/bar') |
| 76 | + def bar(self, docs: DocumentArray, **kwargs): |
| 77 | + docs.texts = ['bar'] * len(docs) |
| 78 | + |
| 79 | + def start_flow(stop_event, **kwargs): |
| 80 | + """start a blocking Flow.""" |
| 81 | + with Flow(**kwargs).add(uses=MyExec) as f: |
| 82 | + f.block(stop_event=stop_event) |
| 83 | + |
| 84 | + e = multiprocessing.Event() # create new Event |
| 85 | + |
| 86 | + p = random_port() |
| 87 | + t = multiprocessing.Process( |
| 88 | + name='Blocked-Flow', target=start_flow, args=(e,), kwargs={'port': p} |
| 89 | + ) |
| 90 | + t.start() |
| 91 | + |
| 92 | + time.sleep(1) |
| 93 | + N = 100 |
| 94 | + da = DocumentArray.empty(N) |
| 95 | + try: |
| 96 | + assert da.post(f'grpc://0.0.0.0:{p}/')[:, 'text'] == [''] * N |
| 97 | + assert da.post(f'grpc://0.0.0.0:{p}/foo').texts == ['foo'] * N |
| 98 | + assert da.post(f'grpc://0.0.0.0:{p}/bar').texts == ['bar'] * N |
| 99 | + except: |
| 100 | + raise |
| 101 | + finally: |
| 102 | + e.set() |
| 103 | + t.join() |
0 commit comments