a
    ¦ˆc£  ã                   @   sŒ   d Z ddlZddlZddlmZ ddlmZmZmZ ddlm	Z	 dZ
G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ de	ejƒZdS )aÇ   Tests for dynamic notifiers with `dispatch='new'`.

Dynamic notifiers created with the `dispatch='new'` option dispatch event
notifications on a new thread. The class handling the dispatch,
`NewTraitChangeNotifyWrapper`, is a subclass of `TraitChangeNotifyWrapper`.
Most of the functionality of the class is thus already covered by the
`TestDynamicNotifiers` test case, and we only need to test that the
notification really occurs on a separate thread.

é    N)Úmock)ÚFloatÚ	HasTraitsÚList)ÚUnittestToolsg      $@c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚRememberThreadszq
    Context manager that behaves like Thread, but remembers created
    threads so that they can be joined.
    c                 C   s
   g | _ d S ©N)Ú_threads©Úself© r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/tests/test_new_notifiers.pyÚ__init__%   s    zRememberThreads.__init__c                 O   s    t j|i |¤Ž}| j |¡ |S r   )Ú	threadingÚThreadr	   Úappend)r   ÚargsÚkwargsÚthreadr   r   r   Ú__call__(   s    zRememberThreads.__call__c                 C   s   | S r   r   r
   r   r   r   Ú	__enter__-   s    zRememberThreads.__enter__c                 G   s4   | j }|r0| ¡ }|jtd� | ¡ rtdƒ‚qd S )N©ÚtimeoutzFailed to join thread)r	   ÚpopÚjoinÚSAFETY_TIMEOUTÚis_aliveÚRuntimeError)r   Zexc_argsÚthreadsr   r   r   r   Ú__exit__0   s    zRememberThreads.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r       s
   r   c                   @   s   e Zd ZeZdS )ÚFooN)r    r!   r"   r   Úfoor   r   r   r   r$   :   s   r$   c                   @   s   e Zd Zeƒ Zdd„ ZdS )ÚReceiverc                 C   s
   t | jƒS r   )ÚboolÚnotificationsr
   r   r   r   ÚnotifiedA   s    zReceiver.notifiedN)r    r!   r"   r   r(   r)   r   r   r   r   r&   >   s   r&   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestNewNotifiersz4 Tests for dynamic notifiers with `dispatch='new'`. c              	      sò   t ƒ ‰ ‡ fdd„}tƒ }|j|ddd� tƒ �\}tjd|d�}|� d|_W d   ƒ n1 s^0    Y  | jˆ d	t jt	d
� W d   ƒ n1 s�0    Y  ˆ j
}|  t|ƒd¡ |  |d dd … |dddf¡ t ¡ j}|  ||d d ¡ d S )Nc                    s(   t  ¡ j}|| |||f}ˆ j |¡ d S r   )r   Úcurrent_threadÚidentr(   r   )ÚobjÚnameÚoldÚnewZ	thread_idÚevent©Zreceiverr   r   Úon_foo_notificationsL   s    
zSTestNewNotifiers.test_notification_on_separate_thread.<locals>.on_foo_notificationsr%   r0   )Údispatchztraits.trait_notifiers.Thread)r0   é   Znotifications_itemsr   é   r   )r&   r$   Zon_trait_changer   r   Úpatchr%   ZassertEventuallyTruer)   r   r(   ÚassertEqualÚlenr   r+   r,   ZassertNotEqual)r   r3   r-   Zremember_threadsZpatcherr(   Zthis_thread_idr   r2   r   Ú$test_notification_on_separate_threadI   s(    þ$þ$ 
z5TestNewNotifiers.test_notification_on_separate_threadN)r    r!   r"   r#   r:   r   r   r   r   r*   F   s   r*   )r#   r   Zunittestr   Z
traits.apir   r   r   Ztraits.testing.unittest_toolsr   r   Úobjectr   r$   r&   ZTestCaser*   r   r   r   r   Ú<module>   s   
