o
    ;i                     @   s   d dl Z d dlmZ d dlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ er8dd	lmZ G d
d de jZG dd de jZG dd dZdS )    N)TracebackType)TYPE_CHECKING
CollectionOptionalType   )Channel)Server)
H2Protocol)	CodecBaseStatusDetailsCodecBase)	IServablec                   @   sR   e Zd ZdejfddZdefddZdddZdd	d
Z	dddZ
dddZdS )_Serverreturnc                 C      t NNotImplementedErrorself r   C/home/ubuntu/.local/lib/python3.10/site-packages/grpclib/testing.pyget_loop      z_Server.get_loopc                 C   r   r   r   r   r   r   r   
is_serving   r   z_Server.is_servingNc                       t r   r   r   r   r   r   start_serving      z_Server.start_servingc                    r   r   r   r   r   r   r   serve_forever   r   z_Server.serve_foreverc                 C      d S r   r   r   r   r   r   close   r   z_Server.closec                    s   d S r   r   r   r   r   r   wait_closed    r   z_Server.wait_closedr   N)__name__
__module____qualname__asyncioAbstractEventLoopr   boolr   r   r   r    r!   r   r   r   r   r      s    


r   c                       sb   e Zd Zdeddf fddZdeddfddZdeddfd	d
ZdefddZ	dddZ
  ZS )_InMemoryTransportprotocolr   Nc                    s   t    t | _|| _d S r   )super__init__r&   get_event_loop_loop	_protocol)r   r*   	__class__r   r   r,   &   s   


z_InMemoryTransport.__init__datac                 C   s    | j j s| j | d S d S r   )r/   
connection
is_closingdata_receivedr   r2   r   r   r   _write_soon.   s   z_InMemoryTransport._write_soonc                 C   s   |r| j | j| d S d S r   )r.   	call_soonr7   r6   r   r   r   write2   s   z_InMemoryTransport.writec                 C   s   dS )NFr   r   r   r   r   r4   6   r   z_InMemoryTransport.is_closingc                 C   r   r   r   r   r   r   r   r    9   r   z_InMemoryTransport.closer"   )r#   r$   r%   r
   r,   bytesr7   r9   r(   r4   r    __classcell__r   r   r0   r   r)   $   s    r)   c                	   @   st   e Zd ZdZ		dded dee dee ddfdd	Zde	fd
dZ
deee  dee dee ddfddZdS )
ChannelFora  Manages specially initialised :py:class:`~grpclib.client.Channel`
    with an in-memory transport to a :py:class:`~grpclib.server.Server`

    Example:

    .. code-block:: python3

        class Greeter(GreeterBase):
            ...

        greeter = Greeter()

        async with ChannelFor([greeter]) as channel:
            stub = GreeterStub(channel)
            response = await stub.SayHello(HelloRequest(name='Dr. Strange'))
            assert response.message == 'Hello, Dr. Strange!'
    Nservicesr   codecstatus_details_codecr   c                 C   s   || _ || _|| _dS )a  
        :param services: list of services you want to test

        :param codec: instance of a codec to encode and decode messages,
            if omitted ``ProtoCodec`` is used by default

        :param status_details_codec: instance of a status details codec to
            encode and decode error details in a trailing metadata, if omitted
            ``ProtoStatusDetailsCodec`` is used by default
        N)	_services_codec_status_details_codec)r   r=   r>   r?   r   r   r   r,   O   s   
zChannelFor.__init__c                    s   t | j| j| jd| _t | j_| jj | j_| j	 | _
t| j| jd| _| j	 | j_| jjt| j
 | j
t| jj | jS )z>
        :return: :py:class:`~grpclib.client.Channel`
        )r>   r?   )r	   r@   rA   rB   _serverr   r.   create_future_server_closed_fut_protocol_factory_server_protocolr   _channelr/   connection_mader)   r   r   r   r   
__aenter__c   s*   

zChannelFor.__aenter__exc_typeexc_valexc_tbc                    sT   | j jd us	J | j jd  | j   | jd  | j  | j I d H  d S r   )rH   r/   connection_lostr    rG   rC   r!   )r   rK   rL   rM   r   r   r   	__aexit__~   s   

zChannelFor.__aexit__)NN)r#   r$   r%   __doc__r   r   r   r   r,   r   rJ   r   BaseExceptionr   rO   r   r   r   r   r<   =   s.    

r<   )r&   typesr   typingr   r   r   r   clientr   serverr	   r*   r
   encoding.baser   r   _typingr   AbstractServerr   	Transportr)   r<   r   r   r   r   <module>   s    