
    ai<k                        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 d dlmZm	Z	 d dl
mZ d dl
mZ d dl
mZ  ej        d          dfZd	Zd
 d d dZd d d dZddZddZddZddZd ZddZ G d de          Zd Ze	j                            edd           d             Ze	j                            edd           d             Ze	j                            edd            d!             Zd" Z e	j                            edd#           d$             Z!e	j                            edd%           d&             Z"e	j                            edd'           d(             Z#d) Z$e	j                            edd*           d+             Z%e	j                            edd,           d-             Z&e	j                            edd.           d/             Z'd0 Z(d1 Z)d2 Z*d3 Z+d4 Z,d5 Z-d6 Z.d7 Z/d8 Z0e	j                            edd9           d:             Z1e	j                            edd;           d<             Z2d= Z3e	j                            edd>           d?             Z4e	j                            edd@           dA             Z5dB Z6e	j                            ed          dC             Z7e	j                            ed          dD             Z8dE Z9e	j                            eddF           dG             Z:e	j                            eddH           dI             Z;dJ Z<e	j                            eddK           dL             Z=e	j                            eddM           dN             Z>dO Z?e	j                            eddP           dQ             Z@dR ZAe	j                            eddS           dT             ZBe	j                            eddU           dV             ZCdW ZDe	j                            eddX           dY             ZEe	j                            eddZ           d[             ZFe	j                            edd\           d]             ZGd^ ZHd_ ZId` ZJda ZKdb ZLdc ZMdd ZNde ZOdf ZPdg ZQe	j                            eddh           di             ZRe	j                            eddj           dk             ZSdl ZTdm ZUe	j                            eddn           do             ZVe	j                            eddp           dq             ZWdr ZXds ZYdt ZZdu Z[dv Z\dw Z]dx Z^dy Z_dz Z`d{ Zad| Zbd} Zcd~ Zdd Zee	j                            edd           d             Zfe	j                            edd           d             Zgd Zhd Zid Zjd Zkd Zld Zmd Znd Zod Zpd Zqd Zrd Zsd Ztd Zud Zvd Zwd Zxd Zyd Zzd Z{d Z|d Z}d Z~d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Ze	j                            edd           d             Ze	j                            edd           d             Zd Zd Ze	j                            ed          d             Ze	j                            ed          d             Ze	j                            ed          d             ZdS )    N)	timedelta)SkipTestmock)StatsClient)TCPStatsClient)UnixSocketStatsClient	localhost  z
tmp.socketc                     | j         S N)sendtoxs    \/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/statsd/tests.py<lambda>r      s    QX     c                     | j         S r   sendallr   s    r   r   r      s    QY r   c                     | j         S r   r   r   s    r   r   r      s    ai r   )udptcpunixc                 \    t          j        t                              |           |          S r   r   callstrencoder   addrs     r   r   r      s    49SZZ]]D99 r   c                 `    t          j        t                              | dz                       S N
r   r   s     r   r   r      s     49SZZD%9%9:: r   c                 `    t          j        t                              | dz                       S r"   r   r   s     r   r   r      s     DIcjjT&:&:;; r   c                 &    | |k    s
J |            d S r    )abmsgs      r   eq_r*   "   s    666366666r   Fc                     |st           d         }|st           d         }t          ||| |          }t          j                    |_        |S )Nr      )hostportprefixipv6)ADDRr   r   Mock_sock)r/   r    r.   r0   scs        r   _udp_clientr5   &   sL     Aw Aw	$T&t	D	D	DBy{{BHIr   c                     |st           d         }|st           d         }t          ||| ||          }t          j                    |_        |S )Nr   r,   )r-   r.   r/   timeoutr0   )r1   r   r   r2   r3   )r/   r    r.   r7   r0   r4   s         r   _tcp_clientr8   0   sU     Aw Aw	TVW!
# 
# 
#By{{BHIr   c                 j    |st           }t          ||           }t          j                    |_        |S )N)socket_pathr/   )UNIX_SOCKETr   r   r2   r3   )r/   r:   r4   s      r   _unix_socket_clientr<   ;   s3     "!	;v	F	F	FBy{{BHIr   c                 
   t          |         |           }t          |j        |           |j        d         d                             d          }t          j        d|d|d          }|                    |          sJ d S )Nr   ascii^z:\d+|$)send_methodr*   
call_count	call_argsdecoderecompilematch)sockcountprotostartendsendvalueexps           r   _timer_checkrP   D   s    ud##DN1a ''00E
**uuuccc2
3
3C99Ur   c                     t          |         |           }t          |j        |           |st          }|+t          |j        t          |         ||                     d S d S r   )rA   r*   rB   r1   rC   make_val)rH   rI   rJ   valr    rM   s         r   _sock_checkrT   L   sq    ud##D 
NUOC&&	
 	
 	
 	
 	
 r   c                   $    e Zd ZdZd Zd Zd ZdS )assert_raisesa  A context manager that asserts a given exception was raised.

    >>> with assert_raises(TypeError):
    ...     raise TypeError

    >>> with assert_raises(TypeError):
    ...     raise ValueError
    AssertionError: ValueError not in ['TypeError']

    >>> with assert_raises(TypeError):
    ...     pass
    AssertionError: No exception raised.

    Or you can specify any of a number of exceptions:

    >>> with assert_raises(TypeError, ValueError):
    ...     raise ValueError

    >>> with assert_raises(TypeError, ValueError):
    ...     raise KeyError
    AssertionError: KeyError not in ['TypeError', 'ValueError']

    You can also get the exception back later:

    >>> with assert_raises(TypeError) as cm:
    ...     raise TypeError('bad type!')
    >>> cm.exception
    TypeError('bad type!')
    >>> cm.exc_type
    TypeError
    >>> cm.traceback
    <traceback @ 0x3323ef0>

    Lowercase name because that it's a class is an implementation detail.

    c                     || _         d S r   )exc_cls)selfrX   s     r   __init__zassert_raises.__init__~   s    r   c                     | S r   r&   )rY   s    r   	__enter__zassert_raises.__enter__   s    r   c                     |s
J d            || j         v s#J |j        dd | j         D                         || _        || _        || _        dS )NzNo exception raised.z not in c                     g | ]	}|j         
S r&   )__name__).0es     r   
<listcomp>z*assert_raises.__exit__.<locals>.<listcomp>   s    <<<!1:<<<r   T)rX   r_   exc_type	exception	traceback)rY   typrN   tbs       r   __exit__zassert_raises.__exit__   sp    ******dl"""LLL<<t|<<<<%>""" tr   N)r_   
__module____qualname____doc__rZ   r\   rh   r&   r   r   rV   rV   X   sL        # #J    	 	 	 	 	r   rV   c                 x   |                      d           t          | j        d|d           |                      dd           t          | j        d|d           |                      dd           t          | j        d	|d
           |                      ddd           t          | j        d|d           d S )Nfoor,   foo:1|c)rS   
      zfoo:10|c333333?   z	foo:1.2|c      ?rate   zfoo:10|c|@0.5incrrT   r3   clrJ   s     r   
_test_incrr{      s    GGENNN!U	2222GGE2!U
3333GGE3!U4444GGE2CG   !U888888r   randomc                      dS Nr&   r&   r   r   r   r          R r   c                  B    t                      } t          | d           dS )StatsClient.incr works.r   N)r5   r{   rz   s    r   test_incr_udpr      #     
Br5r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )TCPStatsClient.incr works.r   N)r8   r{   r   s    r   test_incr_tcpr      r   r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )r   r   N)r<   r{   r   s    r   test_incr_unix_socketr      %     
		Br6r   c                 p   |                      d           t          | j        d|d           |                      dd           t          | j        d|d           |                      dd           t          | j        d|d	           |                      ddd
           t          | j        d|d           d S )Nrm   r,   zfoo:-1|cro   rp   z	foo:-10|crq   rr   z
foo:-1.2|crs   rt   rv   zfoo:-1|c|@0.5)decrrT   r3   ry   s     r   
_test_decrr      s    GGENNN!UJ///GGE2!UK000GGE3!UL111GGE13G!UO44444r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )zStatsClient.decr works.r   N)r5   r   r   s    r   test_decr_udpr      r   r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )TCPStatsClient.decr works.r   N)r8   r   r   s    r   test_decr_tcpr      r   r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS r   r   N)r<   r   r   s    r   test_decr_unix_socketr      r   r   c                    |                      dd           t          | j        d|d           |                      dd           t          | j        d|d           |                      ddd	
           t          | j        d|d           d S )Nrm      r,   foo:30|grq   rp   z	foo:1.2|gF   rs   rt   rr   zfoo:70|g|@0.5gaugerT   r3   ry   s     r   _test_gauger      s    HHUB!UJ///HHUC!UK000HHUBSH!!!!UO44444r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )zStatsClient.gauge works.r   N)r5   r   r   s    r   test_gauge_udpr      #     
BEr   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS )zTCPStatsClient.gauge works.r   N)r8   r   r   s    r   test_gauge_tcpr      r   r   c                      dS r~   r&   r&   r   r   r   r      r   r   c                  B    t                      } t          | d           dS r   )r<   r   r   s    r   test_gauge_unix_socketr      s%     
		BFr   c                 d    |                      dd           t          | j        d|d|           d S )Nrm   r   r,   r   r    r   rz   rJ   r    s      r   
_test_ipv6r      s7    HHUB!UJT::::::r   c                  Z    d} t          | d         d          }t          |d|            dS )z$StatsClient can use to IPv6 address.z::1r
   r   r   r   Tr    r0   r   N)r5   r   r    rz   s     r   test_ipv6_udpr      5    D	$q'	-	-	-Br5$r   c                  Z    d} t          | d         d          }t          |d|            dS )z'TCPStatsClient can use to IPv6 address.r   r   Tr   r   N)r8   r   r   s     r   test_ipv6_tcpr     r   r   c                 b    |                      d           t          | j        d|d|           d S )Nrm   r,   rn   r   rw   r   s      r   _test_resolutionr   
  s2    GGENNN!UID999999r   c                       t          d          )Nz#IPv6 resolution is broken on Travis)r   r5   r   r   s    r   test_ipv6_resolution_udpr     s    
8
9
99r   c                  J    t          dd          } t          | dd           d S )Nr	   Tr   r   r   r8   r   r   s    r   test_ipv6_resolution_tcpr     s-    	+D	1	1	1BR 344444r   c                  H    t          d          } t          | dd           d S )Nr	   r   r   	127.0.0.1r
   )r5   r   r   s    r   test_ipv4_resolution_udpr     +    	+	&	&	&BR 344444r   c                  H    t          d          } t          | dd           d S )Nr	   r   r   r   r   r   s    r   test_ipv4_resolution_tcpr     r   r   c                 B     d} fd}|D ]\  }} |||           d S )N))   z+12)iz-13)rq   z+1.2)gz-1.3c                     j                                                              d| d           t          j         dd|z             d S )Nrm   Tdeltar,   zfoo:%s|g)r3   
reset_mockr   rT   )numresultrz   rJ   s     r   _checkz!_test_gauge_delta.<locals>._check,  sP    

4(((BHa
V(;<<<<<r   r&   )rz   rJ   testsr   r   r   s   ``    r   _test_gauge_deltar   $  s]    E= = = = = =
   VsF r   c                      dS r~   r&   r&   r   r   r   r   5  r   r   c                  B    t                      } t          | d           dS )z*StatsClient.gauge works with delta values.r   Nr5   r   r   s    r   test_gauge_delta_udpr   5  #     
Bb%     r   c                      dS r~   r&   r&   r   r   r   r   <  r   r   c                  B    t                      } t          | d           dS )z-TCPStatsClient.gauge works with delta values.r   Nr8   r   r   s    r   test_gauge_delta_tcpr   <  r   r   c                 b    |                      ddd           t          | j        dd           d S )Nrm   Fr   r,   zfoo:0|g
foo:-5|gr   ry   s     r   _test_gauge_absolute_negativer   C  s5    HHUBeH$$$!011111r   c                      dS r~   r&   r&   r   r   r   r   H  r   r   c                  B    t                      } t          | d           dS )z5StatsClient.gauge works with absolute negative value.r   Nr   r   s    r    test_gauge_absolute_negative_udpr   H  r   r   c                      dS r~   r&   r&   r   r   r   r   O  r   r   c                  B    t                      } t          | d           dS )z8TCPStatsClient.gauge works with absolute negative value.r   Nr   r   s    r    test_gauge_absolute_negative_tcpr   O  r   r   c                     d|_         |                     dddd           t          | j        d|d           d|_         |                     dd	dd           t          | j        d|d           d S )
Nr   rm   rs   F)ru   r   r,   zfoo:0|g
foo:-1|grp   )return_valuer   rT   r3   )rz   rJ   mock_randoms      r   "_test_gauge_absolute_negative_rater   V  sz    !KHHUBSH...!U$7888 KHHUBSH...!U$788888r   c                 D    t                      }t          |d|            dS )z>StatsClient.gauge works with absolute negative value and rate.r   N)r5   r   r   rz   s     r   %test_gauge_absolute_negative_rate_udpr   a  %     
B&r5+>>>>>r   c                 D    t                      }t          |d|            dS )zATCPStatsClient.gauge works with absolute negative value and rate.r   N)r8   r   r   s     r   %test_gauge_absolute_negative_rate_tcpr   h  r   r   c                 p   |                      dd           t          | j        d|d           |                      dd           t          | j        d|d           |                      dd           t          | j        d	|d
           |                      ddd           t          | j        d|d           d S )Nrm   ro   r,   zfoo:10|sgffffff@rp   z	foo:2.3|sbarrr   z	foo:bar|srs   rv   zfoo:2.3|s|@0.5)setrT   r3   ry   s     r   	_test_setr   o  s    FF5"!UJ///FF5#!UK000FF5%!UK000FF5#s!U$455555r   c                      dS r~   r&   r&   r   r   r   r   }  r   r   c                  B    t                      } t          | d           dS )zStatsClient.set works.r   N)r5   r   r   s    r   test_set_udpr   }  #     
Bb%r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )zTCPStatsClient.set works.r   N)r8   r   r   s    r   test_set_tcpr     r   r   c                    |                      dd           t          | j        d|d           |                      dd           t          | j        d|d           |                      ddd	           t          | j        d
|d           d S )Nrm   d   r,   zfoo:100.000000|msi^  rp   zfoo:350.000000|msrs   rt   rr   zfoo:100.000000|ms|@0.5)timingrT   r3   ry   s     r   _test_timingr     s    IIeS!U$7888IIeS!U$7888IIeSsI###!U$<=====r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )zStatsClient.timing works.r   N)r5   r   r   s    r   test_timing_udpr     #     
BUr   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )zTCPStatsClient.timing works.r   N)r8   r   r   s    r   test_timing_tcpr    r  r   c                     t                      } d}|                     dt          d                     t          | j        d|d           |                     dt          d                     t          | j        d|d	           d S )
Nr   rm   g      ?)secondsr,   zfoo:1500.000000|ms)daysrp   zfoo:129600000.000000|ms)r5   r   r   rT   r3   ry   s     r   test_timing_supports_timedeltar    s    	BEIIeYs+++,,,!U$8999IIeYC((()))!U$=>>>>>r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )z#UnixSocketStatsClient.timing works.r   N)r<   r   r   s    r   test_timing_unix_socketr    s%     
		BVr   c                 J     d} fd}|D ]\  }\  }}} |||||           d S )N))rn   )rm   z1|cr,   )zbar:50|ms|@0.5)r   z50|msrs   )zbaz:23|g)bazz23|gr,   c           	          t           j                            t          dd           5  t	          |                     |||                     d d d            d S # 1 swxY w Y   d S )Nr|   c                      dS r~   r&   r&   r   r   r   z/_test_prepare.<locals>._check.<locals>.<lambda>  s     r   )r   patchobjectr|   r*   _prepare)osvrrz   s       r   r   z_test_prepare.<locals>._check  s    Zvx<< 	) 	)2;;q!Q''(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   &AA #A r&   )rz   rJ   r   r   r  r  r  r  s   `       r   _test_preparer    sa    E) ) ) ) )   9Aq!q!Q r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )z!Test StatsClient._prepare method.r   N)r5   r  r   s    r   test_prepare_udpr    #     
B"er   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )z$Test TCPStatsClient._prepare method.r   N)r8   r  r   s    r   test_prepare_tcpr    r  r   c                 ^    |                      d           t          | j        d|d           d S )Nr   r,   zfoo.bar:1|crw   ry   s     r   _test_prefixr     s-    GGENNN!UM22222r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  F    t          d          } t          | d           dS )r   rm   r/   r   N)r5   r   r   s    r   test_prefix_udpr$    *     
E	"	"	"BUr   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  F    t          d          } t          | d           dS )r   rm   r#  r   N)r8   r   r   s    r   test_prefix_tcpr(    r%  r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  F    t          d          } t          | d           dS )z!UnixSocketStatsClient.incr works.rm   r#  r   N)r<   r   r   s    r   test_prefix_unix_socketr+    s*     
E	*	*	*BVr   c                     |                      d          5  	 d d d            n# 1 swxY w Y   t          | j        d|dd           d S Nrm   r,   mstimerrP   r3   ry   s     r   _test_timer_managerr1    s    	%                 1eUD11111s   $((c                  B    t                      } t          | d           dS )z)StatsClient.timer can be used as manager.r   N)r5   r1  r   s    r   test_timer_manager_udpr3    !    	BE"""""r   c                  B    t                      } t          | d           dS )z,TCPStatsClient.timer can be used as manager.r   N)r8   r1  r   s    r   test_timer_manager_tcpr6    r4  r   c                    |                      d          d             }|                      d          d             }t          ddg |dd                     t          | j        d|dd           t          ddg |dd                     t          | j        d|dd           t          d	d
g |d
d	                     t          | j        d|dd           d S )Nrm   c                 
    | |gS r   r&   r'   r(   s     r   rm   z"_test_timer_decorator.<locals>.foo      1vr   r   c                 
    || gS r   r&   r9  s     r   r   z"_test_timer_decorator.<locals>.bar  r:  r   rv   rp   r,   r.        rr   r0  r*   rP   r3   rz   rJ   rm   r   s       r   _test_timer_decoratorr@     s    XXe__  _ XXe__  _
 AAq		1eUD111AAq		1eUD111AAq		1eUD11111r   c                  B    t                      } t          | d           dS )z3StatsClient.timer is a thread-safe decorator (UDP).r   N)r5   r@  r   s    r   test_timer_decorator_udprB    !    	B"e$$$$$r   c                  B    t                      } t          | d           dS )z3StatsClient.timer is a thread-safe decorator (TCP).r   N)r8   r@  r   s    r   test_timer_decorator_tcprE    rC  r   c                     |                      d          5 }t          |j        d            d d d            n# 1 swxY w Y   t          |j        t                    sJ d S )Nwoo)r0  r*   r.  
isinstancefloat)rz   rJ   r   s      r   _test_timer_capturerJ  !  s    	% FFIt              fi'''''''s   8<<c                  B    t                      } t          | d           dS )z6You can capture the output of StatsClient.timer (UDP).r   N)r5   rJ  r   s    r   test_timer_capture_udprL  '  r4  r   c                  B    t                      } t          | d           dS )z6You can capture the output of StatsClient.timer (TCP).r   N)r8   rJ  r   s    r   test_timer_capture_tcprN  -  r4  r   c                     |                      dd          5  	 d d d            n# 1 swxY w Y   t          | j        d|dd           d S )Nrm   rs   rt   r,   zms|@0.5r/  ry   s     r   _test_timer_context_raterP  3  s    	%c	"	"                 1eUI66666s   &**c                      dS r~   r&   r&   r   r   r   r   :  r   r   c                  B    t                      } t          | d           dS )z3StatsClient.timer can be used as manager with rate.r   N)r5   rP  r   s    r   test_timer_context_rate_udprS  :  #     
BR'''''r   c                      dS r~   r&   r&   r   r   r   r   A  r   r   c                  B    t                      } t          | d           dS )z6TCPStatsClient.timer can be used as manager with rate.r   N)r8   rP  r   s    r   test_timer_context_rate_tcprW  A  rT  r   c                      t                      } t          j        d d          } |                     d          |          }t	          d |                       t          | j        dddd           dS )	zDTCPStatsClient.timer can be used as decorator on a partial function.c                     | | z  S r   r&   r   s    r   r   z7test_timer_decorator_partial_function.<locals>.<lambda>L  s
    a!e r   rp   rm   rv   r,   r   ms|@0.1N)r8   	functoolspartialr0  r*   rP   r3   )rz   rm   funcs      r   %test_timer_decorator_partial_functionr^  H  si    	B

OOQ
/
/C288E??3D4466NNN1eUI66666r   c                 V   |                      dd          d             }|                      dd          dd	            }t          dd
g |d
d                     t          | j        d|dd           t          g d |d                     t          | j        d|dd           d S )Nrm   g?rt   c                 
    || gS r   r&   r9  s     r   rm   z'_test_timer_decorator_rate.<locals>.fooU  r:  r   r   g?rp   rr   c                     ||| gS r   r&   )r'   r(   cs      r   r   z'_test_timer_decorator_rate.<locals>.barY  s    1ayr   rv   r,   rZ  )rr   rp   r=  r=  zms|@0.2)rp   rr   r>  r?  s       r   _test_timer_decorator_raterc  T  s    XXe#X   XXe#X    AAq		1eUI666			33q661eUI66666r   c                      dS r~   r&   r&   r   r   r   r   d  r   r   c                  B    t                      } t          | d           dS )z5StatsClient.timer can be used as decorator with rate.r   N)r5   rc  r   s    r   test_timer_decorator_rate_udprf  d  #     
Br5)))))r   c                      dS r~   r&   r&   r   r   r   r   k  r   r   c                  B    t                      } t          | d           dS )z8TCPStatsClient.timer can be used as decorator with rate.r   N)r8   rc  r   s    r   test_timer_decorator_rate_tcprj  k  rg  r   c                    t          t          j                  5  |                     d          5  t          j                    # 1 swxY w Y   	 d d d            n# 1 swxY w Y   t	          | j        d|dd           d S r-  )rV   socketr7   r0  rP   r3   ry   s     r   _test_timer_context_exceptionsrm  r  s    	v~	&	& # #XXe__ 	# 	#."""	# 	# 	# 	# 	# 	# 	# 	# 	## # # # # # # # # # # # # # # 1eUD11111s.   AAA	AA	AA #A c                  B    t                      } t          | d           d S Nr   )r5   rm  r   s    r   !test_timer_context_exceptions_udprp  z  !    	B"2u-----r   c                  B    t                      } t          | d           d S Nr   )r8   rm  r   s    r   !test_timer_context_exceptions_tcprt    rq  r   c                     |                      d          d             }t          t                    5   |             d d d            n# 1 swxY w Y   t          | j        d|dd           d S )Nrm   c                      t                      r   )
ValueErrorr&   r   r   rm   z-_test_timer_decorator_exceptions.<locals>.foo  s    llr   r,   r.  )r0  rV   rw  rP   r3   )rz   rJ   rm   s      r    _test_timer_decorator_exceptionsrx    s    XXe__  _ 
z	"	"                 1eUD11111s   A

AAc                  0   t          j                    } t          j        |            dt                      }fd}||_        |                    d          fd            }|                      |                       |                                  d S )NFc                  $    du s
J d            d S )NTz'_send called before coroutine completedr&   )_alreadys    r   _sendz-test_coroutine_timer_decorator.<locals>._send  s    $ Ir   r   c                  D   K   t          j        d           d {V  d d S )Nr   T)asynciosleep)r|  s   r   innerz-test_coroutine_timer_decorator.<locals>.inner  s5       mAtr   )r  new_event_loopset_event_loopr5   r}  r0  run_until_completeclose)
event_looprz   r}  r  r|  s       @r   test_coroutine_timer_decoratorr    s    '))J:&&&G	BJ J J J J BHXXe__    _ !!%%''***r   c                  B    t                      } t          | d           d S ro  )r5   rx  r   s    r   #test_timer_decorator_exceptions_udpr    !    	B$R/////r   c                  B    t                      } t          | d           d S rs  )r8   rx  r   s    r   #test_timer_decorator_exceptions_tcpr    r  r   c                     |                      d                                          }|                                 t          | j        d|dd           d S r-  r0  rK   stoprP   r3   rz   rJ   ts      r   _test_timer_objectr    sH    
AFFHHH1eUD11111r   c                  B    t                      } t          | d           dS )zStatsClient.timer works.r   N)r5   r  r   s    r   test_timer_object_udpr    !    	Br5!!!!!r   c                  B    t                      } t          | d           dS )zTCPStatsClient.timer works.r   N)r8   r  r   s    r   test_timer_object_tcpr    r  r   c                    |                      d                                          }|                    d           t          | j        d|           |                                 t          | j        d|dd           d S )Nrm   F)rM   r   r,   r.  )r0  rK   r  rT   r3   rM   rP   r  s      r   _test_timer_object_no_sendr    sp    
AFFF!U###FFHHH1eUD11111r   c                  B    t                      } t          | d           dS )z'Stop StatsClient.timer without sending.r   N)r5   r  r   s    r   test_timer_object_no_send_udpr    !    	Br5)))))r   c                  B    t                      } t          | d           dS )z*Stop TCPStatsClient.timer without sending.r   N)r8   r  r   s    r   test_timer_object_no_send_tcpr    r  r   c                     |                      dd          }|                                 |                                 t          | j        d|dd           d S )Nrm   rs   rt   r,   zms@0.5r  r  s      r   _test_timer_object_rater    sO    
S!!AGGIIIFFHHH1eUH55555r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )z"StatsClient.timer works with rate.r   N)r5   r  r   s    r   test_timer_object_rate_udpr    #     
BB&&&&&r   c                      dS r~   r&   r&   r   r   r   r     r   r   c                  B    t                      } t          | d           dS )z%TCPStatsClient.timer works with rate.r   N)r8   r  r   s    r   test_timer_object_rate_tcpr    r  r   c                     |                      d                                          }|                                 t          t                    5  |                                 d d d            d S # 1 swxY w Y   d S Nrm   )r0  rK   r  rV   RuntimeErrorrM   rz   r  s     r    _test_timer_object_no_send_twicer    s    
AFFHHH	|	$	$  	                 s   A22A69A6c                  @    t                      } t          |            dS )z>StatsClient.timer raises RuntimeError if send is called twice.N)r5   r  r   s    r   #test_timer_object_no_send_twice_udpr        	B$R(((((r   c                  @    t                      } t          |            dS )zATCPStatsClient.timer raises RuntimeError if send is called twice.N)r8   r  r   s    r   #test_timer_object_no_send_twice_tcpr    r  r   c                    |                      d          5 }|j        J t          t                    5  |                                 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                      d                                          }|j        J t          t                    5  |                                 d d d            d S # 1 swxY w Y   d S )Nrm   r   )r0  r.  rV   r  rM   rK   r  s     r   _test_timer_send_without_stopr    s   	% At|||<(( 	 	FFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	              
 	A4<<<	|	$	$  	                 sF   A,A	A,A	A,A	A,,A03A0;CC!$C!c                  @    t                      } t          |            dS )z=StatsClient.timer raises error if send is called before stop.N)r5   r  r   s    r    test_timer_send_without_stop_udpr        	B!"%%%%%r   c                  @    t                      } t          |            dS )z@TCPStatsClient.timer raises error if send is called before stop.N)r8   r  r   s    r    test_timer_send_without_stop_tcpr    r  r   c                     t          t                    5  |                     d                                           d d d            d S # 1 swxY w Y   d S r  )rV   r  r0  r  r   s    r   %_test_timer_object_stop_without_startr    s    	|	$	$  
                 s   (A

AAc                  @    t                      } t          |            dS )z>StatsClient.timer raises error if stop is called before start.N)r5   r  r   s    r   (test_timer_object_stop_without_start_udpr         	B)"-----r   c                  @    t                      } t          |            dS )zATCPStatsClient.timer raises error if stop is called before start.N)r8   r  r   s    r   (test_timer_object_stop_without_start_tcpr  &  r  r   c                    |                                  }|                    d           |                    d           |                    dd           |                                 t          | j        d|d           d S )Nrm   r   r  i@  r,   z"foo:1|c
bar:-1|c
baz:320.000000|ms)pipelinerx   r   r   rM   rT   r3   rz   rJ   pipes      r   _test_pipeliner  ,  sq    ;;==DIIeIIeKKsIIKKK!U$JKKKKKr   c                  B    t                      } t          | d           dS )zStatsClient.pipeline works.r   N)r5   r  r   s    r   test_pipeline_udpr  5  !    	B2ur   c                  B    t                      } t          | d           dS )zTCPStatsClient.pipeline works.r   N)r8   r  r   s    r   test_pipeline_tcpr  ;  r  r   c                     |                                  }|                                 t          | j        d|           d S )Nr   )r  rM   rT   r3   r  s      r   _test_pipeline_nullr  A  s6    ;;==DIIKKK!U#####r   c                  B    t                      } t          | d           dS )z1Ensure we don't error on an empty pipeline (UDP).r   N)r5   r  r   s    r   test_pipeline_null_udpr  G  r4  r   c                  B    t                      } t          | d           dS )z1Ensure we don't error on an empty pipeline (TCP).r   N)r8   r  r   s    r   test_pipeline_null_tcpr  M  r4  r   c                    |                                  5 }|                    d           |                    d           |                    dd           d d d            n# 1 swxY w Y   t	          | j        d|d           d S )Nrm   r   r     r,   zfoo:1|c
bar:-1|c
baz:15|g)r  rx   r   r   rT   r3   r  s      r   _test_pipeline_managerr  S  s    	 $		%		%

5"               !U$ABBBBBs   AA""A&)A&c                  B    t                      } t          | d           dS )z,StatsClient.pipeline can be used as manager.r   N)r5   r  r   s    r   test_pipeline_manager_udpr  [  !    	B2u%%%%%r   c                  B    t                      } t          | d           dS )z/TCPStatsClient.pipeline can be used as manager.r   N)r8   r  r   s    r   test_pipeline_manager_tcpr  a  r  r   c                     |                                  5 }|                    d          5  	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t          | j        d|dd           d S r-  r  r0  rP   r3   r  s      r   _test_pipeline_timer_managerr  g  s    	 $ZZ 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	               1eUD11111s.   A9A=	A =	AAAc                  B    t                      } t          | d           dS )z8Timer manager can be retrieve from UDP Pipeline manager.r   N)r5   r  r   s    r   test_pipeline_timer_manager_udpr  n  !    	B U+++++r   c                  B    t                      } t          | d           dS )z8Timer manager can be retrieve from TCP Pipeline manager.r   N)r8   r  r   s    r   test_pipeline_timer_manager_tcpr  t  r  r   c                     |                                  5 }|                    d          d             } |             d d d            n# 1 swxY w Y   t          | j        d|dd           d S )Nrm   c                      d S r   r&   r&   r   r   rm   z+_test_pipeline_timer_decorator.<locals>.foo|  s    Dr   r,   r.  r  )rz   rJ   r  rm   s       r   _test_pipeline_timer_decoratorr  z  s    	 $	E			 	 
			              
 1eUD11111s   )A

AAc                  B    t                      } t          | d           dS )z.UDP Pipeline manager can be used as decorator.r   N)r5   r  r   s    r   !test_pipeline_timer_decorator_udpr    !    	B"2u-----r   c                  B    t                      } t          | d           dS )z.TCP Pipeline manager can be used as decorator.r   N)r8   r  r   s    r   !test_pipeline_timer_decorator_tcpr    r  r   c                 0   |                                  5 }|                    d                                          }|                                 t	          | j        d|           d d d            n# 1 swxY w Y   t          | j        d|dd           d S )Nrm   r   r,   r.  )r  r0  rK   r  rT   r3   rP   )rz   rJ   r  r  s       r   _test_pipeline_timer_objectr    s    	 ($JJu##%%	BHa'''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( 1eUD11111s   AA33A7:A7c                  B    t                      } t          | d           dS )z&Timer from UDP Pipeline manager works.r   N)r5   r  r   s    r   test_pipeline_timer_object_udpr    !    	BE*****r   c                  B    t                      } t          | d           dS )z&Timer from TCP Pipeline manager works.r   N)r8   r  r   s    r   test_pipeline_timer_object_tcpr    r  r   c                    |                                  5 }|                    d           t          dt          |j                             d d d            n# 1 swxY w Y   t          dt          |j                             d S )Nrm   r,   r   )r  rx   r*   len_stats)rz   r  s     r   _test_pipeline_emptyr    s    	 !$		%As4;   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 3t{s   8AA Ac                  @    t                      } t          |            dS )z4Pipelines should be empty after a send() call (UDP).N)r5   r  r   s    r   test_pipeline_empty_udpr        	Br   c                  @    t                      } t          |            dS )z4Pipelines should be empty after a send() call (TCP).N)r8   r  r   s    r   test_pipeline_empty_tcpr    r  r   c                     |                                  5 }|                    ddd           |                    d           d d d            n# 1 swxY w Y   t          | j        d|d           d S )Nrm   iFr   r   r,   zfoo:0|g
foo:-10|g
bar:1|c)r  r   rx   rT   r3   r  s      r   &_test_pipeline_negative_absolute_gauger    s    	 $

5#U
+++		%               !U$ABBBBBs   .AAAc                  B    t                      } t          | d           dS )z8Negative absolute gauges use an internal pipeline (UDP).r   N)r5   r  r   s    r   )test_pipeline_negative_absolute_gauge_udpr    !    	B*2u55555r   c                  B    t                      } t          | d           dS )z8Negative absolute gauges use an internal pipeline (TCP).r   N)r8   r  r   s    r   )test_pipeline_negative_absolute_gauge_tcpr    r  r   c                 J     dd} fd}|D ]\  }} |||           d S )Nl   rrd}))r   zfoo:1234568901234|g)rx   zfoo:1234568901234|c)r   zfoo:1234568901234.000000|msc                     j                                           t          |           d           t          j         d|           d S )Nrm   r,   )r3   r   getattrrT   )methodr   rz   r   rJ   s     r   r   z!_test_big_numbers.<locals>._check  sO    
FE3'''BHa/////r   r&   )rz   rJ   r   r   r  r   r   s   ``    @r   _test_big_numbersr    sh    
CE0 0 0 0 0 0 0
    vv r   c                  B    t                      } t          | d           dS )z!Test big numbers with UDP client.r   N)r5   r  r   s    r   test_big_numbers_udpr    !    	Bb%     r   c                  B    t                      } t          | d           dS )z!Test big numbers with TCP client.r   N)r8   r  r   s    r   test_big_numbers_tcpr    r  r   c                 `    |                      dd           t          | j        d|           d S )Nrm   rs   rt   r   rw   ry   s     r   _test_rate_no_sendr
    s2    GGEG!U#####r   c                      dS Nrp   r&   r&   r   r   r   r         Q r   c                  B    t                      } t          | d           dS )z?Rate below random value prevents sending with StatsClient.incr.r   N)r5   r
  r   s    r   test_rate_no_send_udpr    #     
Br5!!!!!r   c                      dS r  r&   r&   r   r   r   r     r  r   c                  B    t                      } t          | d           dS )zBRate below random value prevents sending with TCPStatsClient.incr.r   N)r8   r
  r   s    r   test_rate_no_send_tcpr    r  r   c                      t                      } t          j                    | j        j        _        |                     d           t          | j        ddd           dS )z.Socket error on StatsClient should be ignored.rm   r,   r   rn   N)r5   rl  r7   r3   r   side_effectrx   rT   r   s    r   test_socket_errorr    sJ    	B"(."2"2BHOGGENNN!UI.....r   c                     t                      } |                                 }t          d          D ]}|                    d           |                                 t          d| j        j        j                   t          | j        j        j
        d         d         d                   dk    sJ t          | j        j        j
        d         d         d                   dk    sJ dS )zBPipelines shouldn't send packets larger than 512 bytes (UDP only).    sixteen_char_strrp   r   i   r,   N)r5   r  rangerx   rM   r*   r3   r   rB   r  call_args_list)r4   r  r   s      r   test_pipeline_packet_sizer    s    	B;;==D2YY & &		$%%%%IIKKK28?%&&&rx-a03A6773>>>>rx-a03A6773>>>>>>r   rl  c                 p   d}t          |d         |d                   }|                    d           t          d|j        j        j                   t          j        |j        j        _        t          t          j                  5  |                    d           ddd           dS # 1 swxY w Y   dS )z9Socket errors in TCPStatsClient should be raised to user.)r   i  r   r,   )r    r.   rm   N)
r8   rx   r*   r3   r   rB   rl  errorr  rV   )mock_socketr    rz   s      r   !test_tcp_raises_exception_to_userr     s     D	$q'Q	0	0	0BGGENNN28&'''#)<BH 	v|	$	$  
                 s   B++B/2B/c                     d}t          |          }|                    d           |j        j                            |           dS )z2Timeout on TCPStatsClient should be set on socket.A  r7   rm   N)r   rx   r3   
settimeoutassert_called_once_withr  test_timeoutrz   s      r   test_tcp_timeoutr(    sG     L		-	-	-BGGENNNH//=====r   c                     d}t          t          |          }|                    d           |j        j                            |           dS )z9Timeout on UnixSocketStatsClient should be set on socket.r"  r#  rm   N)r   r;   rx   r3   r$  r%  r&  s      r   test_unix_socket_timeoutr*  $  sI     L	{L	A	A	ABGGENNNH//=====r   r   )NNNF)NNNNF)NN)r  r[  r|   rE   rl  datetimer   unittestr   r   statsdr   r   r   gethostbynamer1   r;   rA   rR   r*   r5   r8   r<   rP   rT   r  rV   r{   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r   r$  r(  r+  r1  r3  r6  r@  rB  rE  rJ  rL  rN  rP  rS  rW  r^  rc  rf  rj  rm  rp  rt  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r  r   r(  r*  r&   r   r   <module>r/     s         				        # # # # # # # #       ! ! ! ! ! ! ( ( ( ( ( ( 	[))40
   :9::;;               	
 	
 	
 	
6 6 6 6 6F 6 6 6r9 9 9 68ZZ00  10 68ZZ00  10 68ZZ00  105 5 5 68ZZ00  10 68ZZ00  10 68ZZ00  105 5 5 68ZZ00  10 68ZZ00  10 68ZZ00  10; ; ;
          : : :
5 5 55 5 5
5 5 5
5 5 5
  " 68ZZ00! ! 10! 68ZZ00! ! 10!2 2 2
 68ZZ00! ! 10! 68ZZ00! ! 10!9 9 9 68$$? ? %$? 68$$? ? %$?6 6 6 68ZZ00  10 68ZZ00  10> > > 68ZZ00  10 68ZZ00  10? ? ? 68ZZ00  10   68ZZ00  10 68ZZ00  103 3 3
 68ZZ00  10 68ZZ00  10 68ZZ00  102 2 2# # ## # #2 2 2*% % %% % %( ( (# # ## # #7 7 7 68ZZ00( ( 10( 68ZZ00( ( 10(	7 	7 	77 7 7  68ZZ00* * 10* 68ZZ00* * 10*2 2 2. . .
. . .
2 2 2  00 0 0
0 0 0
2 2 2" " "" " "2 2 2* * ** * *6 6 6 68ZZ00' ' 10' 68ZZ00' ' 10'  ) ) )) ) )	 	 	& & && & &  
. . .. . .L L L    $ $ $# # ## # #C C C& & && & &2 2 2, , ,, , ,2 2 2. . .. . .2 2 2+ + ++ + +      C C C6 6 66 6 6  $! ! !! ! !$ $ $
 68YY//" " 0/" 68YY//" " 0/"/ / /
? 
? 
? 68$$  %$ 68$$> > %$> 68$$> > %$> > >r   