U
    •uÄc†  ã                   @   sº   d dl Z e j dd¡r¶d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ G d	d
„ d
ejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdS )é    NZUSE_TWISTEDF)Úsleep)Úwamp)Úunittest)Údefer)Úservicec                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCaseComponentzÝ
        Application code goes here. This is an example component that calls
        a remote procedure on a WAMP peer, subscribes to a topic to receive
        events, and then stops the world after some events.
        c                 C   s0   t j | |¡ |jd | _d| _d| _d| _d S )NÚtestFé   )r   ÚApplicationSessionÚ__init__Úextrar   ÚstopÚ_loglineÚfinished)ÚselfÚconfig© r   úqc:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\autobahn\wamp\test\test_wamp_component.pyr   ,   s
    zCaseComponent.__init__c                 G   sj   t |ƒdkr"d dd„ |D ƒ¡}nt |ƒdkr8|d }nd}d | j| jj|¡}|  jd7  _t|ƒ d S )Nr	   z, c                 s   s   | ]}t |ƒV  qd S ©N)Ústr)Ú.0Úsr   r   r   Ú	<genexpr>5   s     z$CaseComponent.log.<locals>.<genexpr>r   ú-z= : {0:>3} : {1:<20} : {2})ÚlenÚjoinÚformatr   Ú	__class__Ú__name__Úprint)r   ÚargsZsargsÚmsgr   r   r   Úlog3   s    
zCaseComponent.logc                 C   s(   | j s| jj d ¡ d| _ ntdƒ d S )NTzalready finished)r   r   ÚdeferredÚcallbackr   ©r   r   r   r   Úfinish?   s    zCaseComponent.finishN)r   Ú
__module__Ú__qualname__Ú__doc__r   r"   r&   r   r   r   r   r   %   s   r   c                   @   s   e Zd Zejdd„ ƒZdS )ÚCase1_Backendc                 #   s:   ˆ   d¡ ‡ fdd„}ˆ  |d¡V  ˆ   d¡ ˆ  ¡  d S )NÚjoinedc                    s   ˆ   d | |¡¡ | | S )Nzadd2 invoked: {0}, {1}©r"   r   )ÚxÚyr%   r   r   Úadd2M   s    z"Case1_Backend.onJoin.<locals>.add2úcom.mathservice.add2zadd2 registered)r"   Úregisterr&   )r   Údetailsr/   r   r%   r   ÚonJoinH   s
    

zCase1_Backend.onJoinN©r   r'   r(   r   ÚinlineCallbacksr3   r   r   r   r   r*   F   s   r*   c                   @   s   e Zd Zejdd„ ƒZdS )ÚCase1_Frontendc              
   c   sn   |   d¡ z|  ddd¡V }W n2 tk
rP } z|   d |¡¡ W 5 d }~X Y nX |   d |¡¡ |  ¡  d S )Nr+   r0   é   é   zcall error: {0}zcall result: {0})r"   ÚcallÚ	Exceptionr   r&   )r   r2   ÚresÚer   r   r   r3   X   s    
"zCase1_Frontend.onJoinNr4   r   r   r   r   r6   V   s   r6   c                   @   s   e Zd Zejdd„ ƒZdS )ÚCase2_Backendc                 #   sž   ˆ   d¡ ‡ fdd„}‡ fdd„}d‡ fdd	„	}d‡ fdd„	}‡ fdd„}ˆ  |d¡V  ˆ  |d¡V  ˆ  |d¡V  ˆ  |d¡V  ˆ  |d¡V  ˆ   d¡ d S )Nr+   c                      s   ˆ   d¡ d S )Nzping() is invoked©r"   r   r%   r   r   Úpingm   s    
z"Case2_Backend.onJoin.<locals>.pingc                    s   ˆ   d| |¡ | | S )Nzadd2() is invokedr>   )ÚaÚbr%   r   r   r/   q   s    z"Case2_Backend.onJoin.<locals>.add2Úsomebodyr   c                    s   ˆ   d| |¡ d | |¡S )Nzstars() is invokedz{0} starred {1}xr,   ©ÚnickÚstarsr%   r   r   rE   u   s    z#Case2_Backend.onJoin.<locals>.starsé   c                    s(   ˆ   d| |¡ dd„ tdƒD ƒd |… S )Nzorders() is invokedc                 S   s   g | ]}d   |¡‘qS )zProduct {0})r   )r   Úir   r   r   Ú
<listcomp>{   s     z8Case2_Backend.onJoin.<locals>.orders.<locals>.<listcomp>é2   )r"   Úrange)ÚproductÚlimitr%   r   r   Úordersy   s    z$Case2_Backend.onJoin.<locals>.ordersc                     s   ˆ   d| |¡ t| ƒt|ƒgS )Nzarglen() is invoked)r"   r   )r    Úkwargsr%   r   r   Úarglen}   s    z$Case2_Backend.onJoin.<locals>.arglenúcom.arguments.pingúcom.arguments.add2úcom.arguments.starsúcom.arguments.ordersúcom.arguments.arglenzprocedures registered)rB   r   )rF   )r"   r1   )r   r2   r?   r/   rE   rM   rO   r   r%   r   r3   h   s    
zCase2_Backend.onJoinNr4   r   r   r   r   r=   f   s   r=   c                   @   s   e Zd Zejdd„ ƒZdS )ÚCase2_Frontendc              	   c   sª  |   d¡ tdƒV  |  d¡V  |   d¡ |  ddd¡V }|   d |¡¡ |  d	¡V }|   d
 |¡¡ | jd	dd�V }|   d |¡¡ | jd	dd�V }|   d |¡¡ | jd	ddd�V }|   d |¡¡ |  dd¡V }|   d |¡¡ | jdddd�V }|   d |¡¡ |  d¡V }|   d |¡¡ |  dddd¡V }|   d |¡¡ | jddddd�V }|   d |¡¡ | jdddddddd�V }|   d |¡¡ |   d¡ |  ¡  d S )Nr+   r	   rP   zPinged!rQ   r7   r8   z	Add2: {0}rR   zStarred 1: {0}ZHomer)rD   zStarred 2: {0}rF   )rE   zStarred 3: {0}rC   zStarred 4: {0}rS   ZcoffeezOrders 1: {0}é
   )rL   zOrders 2: {0}rT   zArglen 1: {0})r@   rA   ÚczArglen 2: {0}zArglen 3: {0}Z	finishing)r"   r   r9   r   r&   )r   r2   r;   ZstarredrM   Z
arglengthsr   r   r   r3   ‹   s8    



zCase2_Frontend.onJoinNr4   r   r   r   r   rU   ‰   s   rU   c                   @   sR   e Zd Zej d¡dkrdZdd„ Zej	dd„ ƒZ
ej	dd	„ ƒZej	d
d„ ƒZdS )ÚTestRpcÚWAMP_ROUTER_URLNz`Please provide WAMP_ROUTER_URL environment with url to WAMP router to run WAMP integration testsc                 C   s   t j d¡| _d| _d S )NrY   Zrealm1)ÚosÚenvironÚgetÚurlÚrealmr%   r   r   r   ÚsetUpÀ   s    zTestRpc.setUpc                 c   s^   t  ¡ | _t ¡ }|D ]*}tj| jt| d�| j	|d�}| 
|¡ q| ¡  | jV  | ¡  d S )N)r   )r]   r   r^   Úmake)r   ZDeferredr#   r   ZMultiServicer   ZServicer]   Údictr^   ZsetServiceParentZstartServiceZstopService)r   Ú
componentsZappZ	componentrW   r   r   r   Ú
runOneTestÄ   s    
üzTestRpc.runOneTestc                 c   s   |   ttg¡V  d S r   )rc   r*   r6   r%   r   r   r   Ú
test_case1Õ   s    zTestRpc.test_case1c                 c   s   |   ttg¡V  d S r   )rc   r=   rU   r%   r   r   r   Ú
test_case2Ù   s    zTestRpc.test_case2)r   r'   r(   rZ   r[   r\   Úskipr_   r   r5   rc   rd   re   r   r   r   r   rX   º   s   

rX   )rZ   r[   r\   Zautobahn.twisted.utilr   Zautobahn.twistedr   Ztwisted.trialr   Útwisted.internetr   Ztwisted.applicationr   r
   r   r*   r6   r=   rU   ÚTestCaserX   r   r   r   r   Ú<module>   s   !#1