U
    luf+                     @   s   d dl Z d dlZd dlZzd dlmZ W n ek
r@   dZY nX eZd dlZd dlZd dlm	Z	m
Z
mZmZ dZereeZeeZeee Zdd ZG dd de
ZdS )	    N)getrefcount)PYPYBaseZMQTestCaseSkipTest	skip_pypy   xc                 C   sF   t jdk rd}nd}tdD ]$}t| || kr6 dS td qdS )zwait for refcount on an object to drop to an expected value

    Necessary because of the zero-copy gc thread,
    which can take some time to receive its DECREF message.
    )            2   Ng?)sysversion_inforangegrctimesleep)objrcZmy_refsi r   :/tmp/pip-unpacked-wheel-h6ekxre8/zmq/tests/test_message.pyawait_gc   s    
r   c                       s   e Zd Z fddZedd Zdd Zdd Zd	d
 Zdd Z	edd Z
edd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zed)d* Z  ZS )+	TestFramec                    s$   t    tdD ]}t  qd S )Nr   )supertearDownr   gccollect)selfr   	__class__r   r   r   2   s    
zTestFrame.tearDownc                 C   sh   t ddD ]X}d| t }t|}tj|dd}t||d ksDt~t|| t||ks`t~q
dS )z/Message above 30 bytes are never copied by 0MQ.      r
   FcopyN)r   xr   zmqFrameAssertionErrorr   )r   r   sr   mr   r   r   test_above_307   s    
zTestFrame.test_above_30c                 C   sD   t dD ]6}d| t }t|}t|}| }||kstqdS )z+Test the str representations of the Frames.r"   r
   N)r   r%   r&   r'   strencoder(   )r   r   r)   r*   Zm_strZm_str_br   r   r   test_strD   s    
zTestFrame.test_strc                 C   s\   t ddD ]L}d| t }t|}|j}||jks8ttsH||k	sHt||jks
tq
dS )zTest the Frame.bytes property.r   r"   r
   N)r   r%   r&   r'   bytesr(   r   )r   r   r)   r*   br   r   r   
test_bytesM   s    
zTestFrame.test_bytesc                 C   sV   d}|  ttj| tdD ]4}d| d }t|d}||jdkstqdS )z/Test the unicode representations of the Frames.Zasdfr"   r
      §utf8N)	assertRaises	TypeErrorr&   r'   r   r-   r/   decoder(   )r   r)   r   r*   r   r   r   test_unicodeZ   s    zTestFrame.test_unicodec                 C   s<   t dD ].}d| t }t|}t|t|kstqdS )zTest the len of the Frames.r"   r
   N)r   r%   r&   r'   lenr(   )r   r   r)   r*   r   r   r   test_lenc   s    
zTestFrame.test_lenc                 C   s0  t ddD ]}d| t }t| }}tj|dd}|d7 }t||ksNtt|}|d7 }t||kspt|j}t||kst|t|	 kst|t
|kst||j
kst|t
|kst~t||kst~|d8 }t||kst~|d8 }t|| t||kst||ks(t~q
dS )z-Run through a ref counting cycle with a copy.r!   r"   r
   Fr#   r   Nr   r%   r   r&   r'   r(   r$   bufferr,   r-   r/   r   r   r   r)   r   Zrc_0r*   m2bufr   r   r   test_lifecycle1j   s4    

zTestFrame.test_lifecycle1c                 C   s@  t ddD ].}d| t }t| }}tj|dd}|d7 }t||ksNtt|}|d7 }t||kspt|j}t||kst|t|	 kst|t
|kst||j
kst||j
kst|t
|kst~t||kst~|d8 }t||kst~|d8 }t|| t||ks*t||ks8t~q
dS )z7Run through a different ref counting cycle with a copy.r!   r"   r
   Fr#   r   Nr:   r<   r   r   r   test_lifecycle2   s6    

zTestFrame.test_lifecycle2c                 C   sx   t jdddd}|jjrtt |}|jr0t~tdD ]}t  q:tdD ]}|jr^ qjt	
d qP|jsttd S )N   asdfFTr$   trackr   
   皙?)r&   r'   trackerdoner(   MessageTrackerr   r   r   r   r   )r   r*   Zpmr   r   r   r   test_tracker   s    


zTestFrame.test_trackerc                 C   sH   t jddd}|jd kstt|}|jd ks4t| tt j| d S )NrA   F)rC   )r&   r'   rF   r(   r$   r4   
ValueErrorrH   )r   r*   r=   r   r   r   test_no_tracker   s
    
zTestFrame.test_no_trackerc                 C   s   t jdddd}t jdddd}t ||}|jjr8t|jrBt| t j|jd ~t	dD ]}t
  q^| t j|jd |jrt~t	dD ]}t
  q|dd kst|jstd S )NrA   FTrB   s   whodarE   r   )r&   r'   rH   rF   rG   r(   r4   ZNotDonewaitr   r   r   )r   r*   r=   mtr   r   r   r   test_multi_tracker   s     



zTestFrame.test_multi_trackerc                 C   s   d  }tt| dS )ztest using a buffer as input8   §§¶•ªº˜µ¬˚…∆˙åß∂©œ∑´†≈ç√N)r-   r&   r'   
memoryview)r   insr   r   r   test_buffer_in   s    zTestFrame.test_buffer_inc                 C   s&   |  ttjd |  ttjt  dS )ztest using a bad objectr!   N)r4   r5   r&   r'   object)r   r   r   r   test_bad_buffer_in   s    zTestFrame.test_bad_buffer_inc                 C   sH   d  }t|}|j}t|ts&t||jks4t|j|jksDtdS )zreceiving buffered outputrO   N)r-   r&   r'   r;   
isinstancerP   r(   )r   rQ   r*   Zoutbr   r   r   test_buffer_out   s    
zTestFrame.test_buffer_outc                 C   s   d  }t|}t|}|j}|jdks.t|j|fks>t| |ksNtt	|}|jdksdt|j|fkstt| |kstdS )zmemoryview shape inforO   r   N)
r-   r8   r&   r'   r;   ndimr(   shapetobytesrP   )r   datanfZview1Zview2r   r   r   test_memoryview_shape   s    
zTestFrame.test_memoryview_shapec                 C   s   |  tjtj\}}d}t|}||jks0t|j|dd td ||jksVt|j|dd td ||jks|t|j|dd td ||jkst|j|dd td ||jkstt	dD ]}|
 }||kstq||jkstdS )z9ensure that a message remains intact after multiple sendss   messageFr#   rE   T   N)create_bound_pairr&   PAIRr'   r/   r(   sendr   r   r   recv)r   ar0   r)   r*   r   rr   r   r   test_multisend   s(    




zTestFrame.test_multisendc                 C   sB   d}t | t|}|j}| }||ks0t|j|ks>tdS )ztest messages from memoryviews   carrotjuiceN)rP   r&   r'   r;   rY   r(   r/   )r   r)   r*   r>   s2r   r   r   test_memoryview  s    
zTestFrame.test_memoryviewc                 C   s   d}|  tjtj\}}tdD ]}|j|dd |jdd}|j}|j}~tdD ]n}dd|d   }|j|dd |jdd}	| }
|
|kst	||kst	|	j|kst	t
|	jtksTt	qTq d	S )
z$check for clobbering message buffers@                                                                       Fr#   r!      (   rD   N)r_   r&   r`   r   ra   rb   r/   r;   rY   r(   type)r   nullsasbr   r*   mbr>   ffr=   r0   r   r   r   test_noncopying_recv  s"    zTestFrame.test_noncopying_recvc           
      C   s   d}|  tjtj\}}tdD ]}|jt|dd |jdd}t|}tdD ]~}dd|d   }|jt|dd |jdd}t|}	| |kst|j	rt|	 |kst|	j	rtt
|tksRtqRq d	S )
z%test non-copying memmoryview messagesrh   ri   Fr#   r!   rj   rk   rD   N)r_   r&   r`   r   ra   rP   rb   rY   r(   readonlyrl   )
r   rm   rn   ro   r   r*   r>   rq   r=   buf2r   r   r   test_noncopying_memoryview1  s     

z$TestFrame.test_noncopying_memoryviewc                    s\  zddl }ddlm} W n tk
r4   tdY nX |jj  fddtdD }| t	j
t	j
\}}ttdd	g}td
t|d
 D ]}|d| }|D ]L}	|j||	d}
|j|
dd |jdd}|||
j|
j}||
| q|j|dtfdtfdgd}
d|
d< d|
d< d|
d< |j|
dd |jdd}|||
j|
j}||
| qdS )z%test non-copying numpy array messagesr   N)assert_array_equalzrequires numpyc                    s   g | ]} d dqS )r
   r!   r   ).0r   Zrandr   r   
<listcomp>M  s     z/TestFrame.test_buffer_numpy.<locals>.<listcomp>r!   z>i4Br   )dtypeFr#   rc   r0   )cZa32i   g    eAzhello therer|   )numpyZnumpy.testingrv   ImportErrorr   randomrandintr   r_   r&   r`   intfloatr8   emptyra   rb   Z
frombufferr{   ZreshaperX   )r   r}   rv   Zshapesrc   r0   Zdtypesr   rX   dtAmsgrz   r   rx   r   test_buffer_numpyE  s2    zTestFrame.test_buffer_numpyc                 C   s   t d}|jrt| t jt j\}}|ddg | j|dd}|jsNtt  d dkrrt	sr|
t jsrt| j|dd}|jrtt  d dkrt	s|
t jrtdS )	ztest Frame.more attributes   hellos   his   thereFr#   r   r   N)r&   r'   Zmorer(   r_   r`   Zsend_multipartrb   Zzmq_version_infor   getZMORE)r   framern   ro   r   r   r   test_frame_mored  s    



zTestFrame.test_frame_more)__name__
__module____qualname__r   r   r+   r.   r1   r7   r9   r?   r@   rI   rK   rN   rR   rT   rV   r]   re   rg   rr   ru   r   r   __classcell__r   r   r   r   r   1   s2   
		
#
"	
r   )r$   r   r   r   r~   r   r   r&   Z	zmq.testsr   r   r   r   r%   Zrc0rP   vZview_rcr   r   r   r   r   r   <module>   s"   
