o
    M,h                     @   s|   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r<e  dS 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      t  }t |j_d|j_td6}t  }||_g |j_| t	}t|d W d   n1 s0w   Y  | 
dt|j W d   dS 1 sIw   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.DefaultSelectord   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   l/var/www/www-root/data/www/bot.pdev.uz/venv/lib/python3.10/site-packages/websocket/tests/test_socket_bugs.py6test_bug_implicit_none_return_from_ssl_want_read_fixed$      

"zESocketBugsTest.test_bug_implicit_none_return_from_ssl_want_read_fixedc              	   C   s   t  }ttjd}||j_d|j_td6}t  }||_g |j	_| 
t}t|d W d   n1 s5w   Y  | dt|j W d   dS 1 sNw   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   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   r	   )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   r    zBSocketBugsTest.test_bug_wrong_exception_for_selector_timeout_fixedc                 C   s   t  }d|j_td|j_| t}t|d W d   n1 s#w   Y  ddl	}|j
dkr<| dt|j dS | 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)}t  }||_dg|j_t|d}| |d |j	
  |j
  W d   dS 1 s>w   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   assertEqualregister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-   r2   r   r   r   r   r   !   s    r   __main__)r"   r)   unittestunittest.mockr   r   websocket._socketr   websocket._ssl_compatr   websocket._exceptionsr   r   TestCaser   r3   mainr   r   r   r   <module>   s   ~