|
| 1 | +import logging |
| 2 | +from typing import Dict, List, Literal, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +from docarray import BaseDoc, DocList |
| 6 | + |
| 7 | +from jina import Client, Deployment, Executor, requests |
| 8 | +from jina.helper import random_port |
| 9 | + |
| 10 | + |
| 11 | +class PortGetter: |
| 12 | + def __init__(self): |
| 13 | + self.ports = { |
| 14 | + "http": { |
| 15 | + True: random_port(), |
| 16 | + False: random_port(), |
| 17 | + }, |
| 18 | + "grpc": { |
| 19 | + True: random_port(), |
| 20 | + False: random_port(), |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + def get_port(self, protocol: Literal["http", "grpc"], include_gateway: bool) -> int: |
| 25 | + return self.ports[protocol][include_gateway] |
| 26 | + |
| 27 | + @property |
| 28 | + def gateway_ports(self) -> List[int]: |
| 29 | + return [self.ports["http"][True], self.ports["grpc"][True]] |
| 30 | + |
| 31 | + @property |
| 32 | + def no_gateway_ports(self) -> List[int]: |
| 33 | + return [self.ports["http"][False], self.ports["grpc"][False]] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(scope='module') |
| 37 | +def port_getter() -> callable: |
| 38 | + getter = PortGetter() |
| 39 | + return getter |
| 40 | + |
| 41 | + |
| 42 | +class DictDoc(BaseDoc): |
| 43 | + data: dict |
| 44 | + |
| 45 | + |
| 46 | +class MetadataExecutor(Executor): |
| 47 | + @requests(on="/get-metadata-headers") |
| 48 | + def post_endpoint( |
| 49 | + self, |
| 50 | + docs: DocList[DictDoc], |
| 51 | + parameters: Optional[Dict] = None, |
| 52 | + metadata: Optional[Dict] = None, |
| 53 | + **kwargs, |
| 54 | + ) -> DocList[DictDoc]: |
| 55 | + return DocList[DictDoc]([DictDoc(data=metadata)]) |
| 56 | + |
| 57 | + @requests(on='/stream-metadata-headers') |
| 58 | + async def stream_task( |
| 59 | + self, doc: DictDoc, metadata: Optional[dict] = None, **kwargs |
| 60 | + ) -> DictDoc: |
| 61 | + for k, v in sorted((metadata or {}).items()): |
| 62 | + yield DictDoc(data={k: v}) |
| 63 | + |
| 64 | + yield DictDoc(data={"DONE": "true"}) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.fixture(scope='module') |
| 68 | +def deployment_no_gateway(port_getter: PortGetter) -> Deployment: |
| 69 | + |
| 70 | + with Deployment( |
| 71 | + uses=MetadataExecutor, |
| 72 | + protocol=["http", "grpc"], |
| 73 | + port=port_getter.no_gateway_ports, |
| 74 | + include_gateway=False, |
| 75 | + ) as dep: |
| 76 | + yield dep |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture(scope='module') |
| 80 | +def deployment_gateway(port_getter: PortGetter) -> Deployment: |
| 81 | + |
| 82 | + with Deployment( |
| 83 | + uses=MetadataExecutor, |
| 84 | + protocol=["http", "grpc"], |
| 85 | + port=port_getter.gateway_ports, |
| 86 | + include_gateway=False, |
| 87 | + ) as dep: |
| 88 | + yield dep |
| 89 | + |
| 90 | + |
| 91 | +@pytest.fixture(scope='module') |
| 92 | +def deployments(deployment_gateway, deployment_no_gateway) -> Dict[bool, Deployment]: |
| 93 | + return { |
| 94 | + True: deployment_gateway, |
| 95 | + False: deployment_no_gateway, |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize('include_gateway', [False, True]) |
| 100 | +def test_headers_in_http_metadata( |
| 101 | + include_gateway, port_getter: PortGetter, deployments |
| 102 | +): |
| 103 | + port = port_getter.get_port("http", include_gateway) |
| 104 | + data = { |
| 105 | + "data": [{"text": "test"}], |
| 106 | + "parameters": { |
| 107 | + "parameter1": "value1", |
| 108 | + }, |
| 109 | + } |
| 110 | + logging.info(f"Posting to {port}") |
| 111 | + client = Client(port=port, protocol="http") |
| 112 | + resp = client.post( |
| 113 | + on=f'/get-metadata-headers', |
| 114 | + inputs=DocList([DictDoc(data=data)]), |
| 115 | + headers={ |
| 116 | + "header1": "value1", |
| 117 | + "header2": "value2", |
| 118 | + }, |
| 119 | + return_type=DocList[DictDoc], |
| 120 | + ) |
| 121 | + assert resp[0].data['header1'] == 'value1' |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.asyncio |
| 125 | +@pytest.mark.parametrize('include_gateway', [False, True]) |
| 126 | +async def test_headers_in_http_metadata_streaming( |
| 127 | + include_gateway, port_getter: PortGetter, deployments |
| 128 | +): |
| 129 | + client = Client( |
| 130 | + port=port_getter.get_port("http", include_gateway), |
| 131 | + protocol="http", |
| 132 | + asyncio=True, |
| 133 | + ) |
| 134 | + data = {"data": [{"text": "test"}], "parameters": {"parameter1": "value1"}} |
| 135 | + chunks = [] |
| 136 | + |
| 137 | + async for doc in client.stream_doc( |
| 138 | + on=f'/stream-metadata-headers', |
| 139 | + inputs=DictDoc(data=data), |
| 140 | + headers={ |
| 141 | + "header1": "value1", |
| 142 | + "header2": "value2", |
| 143 | + }, |
| 144 | + return_type=DictDoc, |
| 145 | + ): |
| 146 | + chunks.append(doc) |
| 147 | + assert len(chunks) > 2 |
| 148 | + |
| 149 | + assert DictDoc(data={'header1': 'value1'}) in chunks |
| 150 | + assert DictDoc(data={'header2': 'value2'}) in chunks |
0 commit comments