a
    ñ»j  ã                   @   sv   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZ G dd„ dejƒZedkrre ¡  dS )	é    N)ÚMockÚpatch)Úrecv)ÚSSLWantReadError)ÚWebSocketTimeoutExceptionÚ"WebSocketConnectionClosedExceptionc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚSocketBugsTestz(Test bugs found in socket handling logicc              	   C   sž   t ƒ }tƒ |j_d|j_tdƒ�j}t ƒ }||_g |j_|  t	¡�}t|dƒ W d  ƒ n1 s`0    Y  |  
dt|jƒ¡ W d  ƒ n1 s�0    Y  dS )a  
        BUG #5 FIX VERIFICATION: Test SSLWantReadError timeout now raises correct exception

        Bug was in _socket.py:100-101 - SSLWantReadError except block returned None implicitly
        Fixed: Now properly handles timeout with WebSocketTimeoutException
        ç      ð?úselectors.DefaultSelectoréd   Nú%Connection timed out waiting for data©r   r   r   Úside_effectÚ
gettimeoutÚreturn_valuer   ÚselectÚassertRaisesr   ÚassertInÚstrÚ	exception©ÚselfÚ	mock_sockÚmock_selector_classÚmock_selectorÚcm© r   úFE:\soccer\ml_env\Lib\site-packages\websocket/tests/test_socket_bugs.pyÚ6test_bug_implicit_none_return_from_ssl_want_read_fixed$   s    

(zESocketBugsTest.test_bug_implicit_none_return_from_ssl_want_read_fixedc              	   C   s¨   t ƒ }ttjdƒ}||j_d|j_tdƒ�j}t ƒ }||_g |j	_|  
t¡�}t|dƒ W d  ƒ n1 sj0    Y  |  dt|jƒ¡ W d  ƒ n1 sš0    Y  dS )a  
        BUG #5 FIX VERIFICATION: Test that socket.error with EAGAIN now handles timeout correctly

        Bug was in _socket.py:102-105 - socket.error except block returned None implicitly
        Fixed: Now properly handles timeout with WebSocketTimeoutException
        z Resource temporarily unavailabler	   r
   r   Nr   )r   ÚOSErrorÚerrnoÚEAGAINr   r   r   r   r   r   r   r   r   r   r   )r   r   Zeagain_errorr   r   r   r   r   r   Ú5test_bug_implicit_none_return_from_socket_error_fixed:   s    
(zDSocketBugsTest.test_bug_implicit_none_return_from_socket_error_fixedc              	   C   sž   t ƒ }tƒ |j_d|j_tdƒ�j}t ƒ }||_g |j_|  t	¡�}t|dƒ W d  ƒ n1 s`0    Y  |  
dt|jƒ¡ W d  ƒ n1 s�0    Y  dS )zÿ
        BUG #6 FIX VERIFICATION: Test that selector timeout now raises correct exception type

        Bug was in _socket.py:115 returning None for timeout, treated as connection error
        Fixed: Now raises WebSocketTimeoutException directly
        r	   r
   r   Nr   r   r   r   r   r   Ú3test_bug_wrong_exception_for_selector_timeout_fixedU   s    

(zBSocketBugsTest.test_bug_wrong_exception_for_selector_timeout_fixedc                 C   sŒ   t ƒ }d|j_t d¡|j_|  t¡�}t|dƒ W d  ƒ n1 sF0    Y  ddl	}|j
dkrv|  dt|jƒ¡ n|  dt|jƒ¡ dS )zJ
        Test that socket.timeout exceptions are properly handled
        r	   zOperation timed outr   Nr   )é   é
   zConnection timed out)r   r   r   ÚsocketÚtimeoutr   r   r   r   ÚsysÚversion_infor   r   r   )r   r   r   r(   r   r   r   Ú&test_socket_timeout_exception_handlingp   s    (
z5SocketBugsTest.test_socket_timeout_exception_handlingc                 C   sˆ   t ƒ }tƒ dg|j_d|j_tdƒ�P}t ƒ }||_dg|j_t|dƒ}|  |d¡ |j	 
¡  |j 
¡  W d  ƒ n1 sz0    Y  dS )zCTest the correct behavior when SSLWantReadError is properly handleds   data after retryr	   r
   Tr   N)r   r   r   r   r   r   r   r   ZassertEqualÚregisterZassert_called)r   r   r   r   Úresultr   r   r   Ú)test_correct_ssl_want_read_retry_behavior‰   s    



z8SocketBugsTest.test_correct_ssl_want_read_retry_behaviorN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r#   r*   r-   r   r   r   r   r   !   s   r   Ú__main__)r    r&   ZunittestZunittest.mockr   r   Zwebsocket._socketr   Zwebsocket._ssl_compatr   Zwebsocket._exceptionsr   r   ZTestCaser   r.   Úmainr   r   r   r   Ú<module>   s   ~