o
    h                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddl
mZmZmZ ddlmZ dd Zejdd ZeG d	d
 d
e	jZdS )ao  Test script to find circular references.

Circular references are not leaks per se, because they will eventually
be GC'd. However, on CPython, they prevent the reference-counting fast
path from being used and instead rely on the slower full GC. This
increases memory footprint and CPU overhead, so we try to eliminate
circular references created by normal operation.
    N)webgen
httpclient)skipNotCPythonc                    sD    fddg  g t  t tt| t  |   S )a  Find circular references in a list of objects.

    The garbage list contains objects that participate in a cycle,
    but also the larger set of objects kept alive by that cycle.
    This function finds subsets of those objects that make up
    the cycle(s).
    c                    s   | D ]G}t |}|vrq|v rq|v r*|d  }||  | q| | t|   | | qd S N)idindexappendaddgcget_referentspopremove)levelitemitem_id	candidatefoundgarbage_idsinnerstack	stack_idsvisited_ids P/var/www/vscode/kcb/lib/python3.10/site-packages/tornado/test/circlerefs_test.pyr   "   s$   




z'find_circular_references.<locals>.inner)setmapr   )garbager   r   r   find_circular_references   s   	r   c               	   c   s:   t   t   t t jt jB  dV  z}t } tj	}| t_	z	t   W |t_	n|t_	w t j
dd }g t j
dd< t|dkrNW t d t   dS t|D ]9}| d |D ]}| dt|  q[|D ] }t|tjr| d|j  | dt|  t| qjqR~t|  t d t   w )zERaise AssertionError if the wrapped code creates garbage with cycles.Nr   z!
==========
 Circular 
==========z
    z	
Locals: z
Traceback: )r   disablecollect	set_debugDEBUG_STATSDEBUG_SAVEALLioStringIOsysstderrr   lenenabler   writerepr
isinstancetypes	FrameTypef_locals	tracebackprint_stackAssertionErrorgetvalue)f
old_stderrr   circularr   r   r   r   assert_no_cycle_garbage@   sB   





r8   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )CircleRefsTestc              	      s   G  fdddt  | t5}t "  d} d} d}||_||_||_||_~~W d    n1 s8w   Y  W d    n1 sGw   Y  | dt|j	 | dt|j	 | dt|j	 | 
d	t|j	 d S )
Nc                       s    e Zd Z fddZdd ZdS )z)CircleRefsTest.test_known_leak.<locals>.Cc                    s   || _ d | _d | _d | _d S r   )nameabc)selfr:   Cr   r   __init__m   s   
z2CircleRefsTest.test_known_leak.<locals>.C.__init__c                 S   s   d| j  S )Nzname=)r:   r>   r   r   r   __repr__s   s   z2CircleRefsTest.test_known_leak.<locals>.C.__repr__N)__name__
__module____qualname__rA   rC   r   r?   r   r   r@   l   s    r@   r;   r<   r=   Circularz
    name=az
    name=bz
    name=c)objectassertRaisesr3   r8   r<   r=   r;   assertInstr	exceptionassertNotIn)r>   cmr;   r<   r=   r   r?   r   test_known_leakj   s$   
zCircleRefsTest.test_known_leakc                    s   t d|fg}tj \}}tj|}|| t	 }t
  |d| dI d H  W d    n1 s9w   Y  |  |  |  d S )N/zhttp://127.0.0.1:)r   Applicationtornadotestingbind_unused_port
httpserver
HTTPServer
add_socketr   AsyncHTTPClientr8   fetchclosestop)r>   handler_classappsocketportserverclientr   r   r   run_handler   s   
zCircleRefsTest.run_handlerc                 C   &   G dd dt j}t| | d S )Nc                   @      e Zd Zdd ZdS )z1CircleRefsTest.test_sync_handler.<locals>.Handlerc                 S   s   |  d d S Nok
)r+   rB   r   r   r   get   s   z5CircleRefsTest.test_sync_handler.<locals>.Handler.getNrD   rE   rF   rg   r   r   r   r   Handler       ri   r   RequestHandlerasynciorunrb   r>   ri   r   r   r   test_sync_handler      z CircleRefsTest.test_sync_handlerc                 C   rc   )Nc                   @   rd   )z=CircleRefsTest.test_finish_exception_handler.<locals>.Handlerc                 S   s
   t dre   )r   FinishrB   r   r   r   rg      s   
zACircleRefsTest.test_finish_exception_handler.<locals>.Handler.getNrh   r   r   r   r   ri      rj   ri   rk   ro   r   r   r   test_finish_exception_handler   rq   z,CircleRefsTest.test_finish_exception_handlerc                 C   rc   )Nc                   @   s   e Zd Zejdd ZdS )z1CircleRefsTest.test_coro_handler.<locals>.Handlerc                 s   s    t dV  | d d S Ng{Gz?rf   rm   sleepr+   rB   r   r   r   rg      s   z5CircleRefsTest.test_coro_handler.<locals>.Handler.getN)rD   rE   rF   r   	coroutinerg   r   r   r   r   ri      s    ri   rk   ro   r   r   r   test_coro_handler   s   z CircleRefsTest.test_coro_handlerc                 C   rc   )Nc                   @   rd   )z2CircleRefsTest.test_async_handler.<locals>.Handlerc                    s    t dI d H  | d d S rt   ru   rB   r   r   r   rg      s   z6CircleRefsTest.test_async_handler.<locals>.Handler.getNrh   r   r   r   r   ri      rj   ri   rk   ro   r   r   r   test_async_handler   s   z!CircleRefsTest.test_async_handlerc              	      s   dd l }|jd=G fdddt}|   fdd}t  t|  W d    n1 s2w   Y  W d    d S W d    d S 1 sJw   Y  d S )Nr      c                       s    e Zd Z Zejjdd ZdS )z4CircleRefsTest.test_run_on_executor.<locals>.Factoryc                 S   s   d S r   r   rB   r   r   r   rn      s   z8CircleRefsTest.test_run_on_executor.<locals>.Factory.runN)rD   rE   rF   executorrR   
concurrentrun_on_executorrn   r   )thread_poolr   r   Factory   s    r   c                     s"   t dD ]	}   I d H  qd S )N   )rangern   )i)factoryr   r   main   s   z1CircleRefsTest.test_run_on_executor.<locals>.main)concurrent.futuresfuturesThreadPoolExecutorrH   r8   rm   rn   )r>   r|   r   r   r   )r   r~   r   test_run_on_executor   s   "z#CircleRefsTest.test_run_on_executorN)
rD   rE   rF   rO   rb   rp   rs   rx   ry   r   r   r   r   r   r9   h   s    "	r9   )__doc__rm   
contextlibr   r%   r'   r1   r.   typingunittestrR   r   r   r   tornado.test.utilr   r   contextmanagerr8   TestCaser9   r   r   r   r   <module>   s$    	'
'