ó
Û¤[c           @   s  d  Z  d d l Z y d d l m Z Wn! e k
 rI d d l m Z n Xd d l m Z d d l Z d d l m	 Z	 d d l
 Z
 d d l m Z d d l m Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j f d „  ƒ  YZ e d k re j ƒ  n  d S(   s*    Tests for zmq shell / display publisher. iÿÿÿÿN(   t   Queue(   t   Thread(   t   Int(   t   ZMQDisplayPublisher(   t   Sessiont   NoReturnDisplayHookc           B   s   e  Z d  Z d Z d „  Z RS(   s¢   
    A dummy DisplayHook which allows us to monitor
    the number of times an object is called, but which
    does *not* return a message when it is called.
    i    c         C   s   |  j  d 7_  d  S(   Ni   (   t
   call_count(   t   selft   obj(    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   __call__   s    (   t   __name__t
   __module__t   __doc__R   R	   (    (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR      s   t   ReturnDisplayHookc           B   s   e  Z d  Z d „  Z RS(   s’   
    A dummy DisplayHook with the same counting ability
    as its base class, but which also returns the same
    message when it is called.
    c         C   s   t  t |  ƒ j | ƒ | S(   N(   t   superR   R	   (   R   R   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR	   )   s    (   R
   R   R   R	   (    (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR   #   s   t   CounterSessionc           B   s#   e  Z d  Z e d ƒ Z d „  Z RS(   s   
    This is a simple subclass to allow us to count
    the calls made to the session object by the display
    publisher.
    i    c         O   s,   |  j  d 7_  t t |  ƒ j | | Ž  d S(   su   
        A trivial override to just augment the existing call
        with an increment to the send counter.
        i   N(   t
   send_countR   R   t   send(   R   t   argst   kwargs(    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR   6   s    (   R
   R   R   R   R   R   (    (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR   .   s   t   ZMQDisplayPublisherTestsc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   s6   
    Tests the ZMQDisplayPublisher in zmqshell.py
    c         C   sU   t  j ƒ  |  _ |  j j t  j ƒ |  _ t ƒ  |  _ t d |  j d |  j ƒ |  _ d  S(   Nt   sessiont
   pub_socket(	   t   zmqt   Contextt   contextt   sockett   PUBR   R   R   t   disp_pub(   R   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   setUpD   s    	c         C   s+   |  j  j ƒ  |  j j ƒ  |  j j ƒ  d S(   s¿   
        We need to close the socket in order to proceed with the
        tests.
        TODO - There is still an open file handler to '/dev/null',
        presumably created by zmq.
        N(   R   t   clear_outputR   t   closeR   t   term(   R   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   tearDownN   s    c         C   s:   |  j  j |  j k s t ‚ |  j  j |  j k s6 t ‚ d S(   s   
        Since there's no explicit constructor, here we confirm
        that keyword args get assigned correctly, and override
        the defaults.
        N(   R   R   t   AssertionErrorR   R   (   R   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   test_display_publisher_creationY   s    c            s¨   ˆ j  j g  k s t ‚ d „  } ˆ j  j | ƒ ˆ j  j | g k sL t ‚ t ƒ  ‰  ‡  ‡ f d †  } t d | ƒ } | j ƒ  ˆ  j d d ƒ } | g  k s¤ t ‚ d S(   s„   
        Confirms that the thread_local attribute is correctly
        initialised with an empty list for the display hooks
        c         S   s   |  S(   N(    (   t   msg(    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   hookh   s    c              s   ˆ  j  ˆ j j ƒ d  S(   N(   t   putR   t   _hooks(    (   t   qR   (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   set_thread_hooksn   s    t   targett   timeouti
   N(   R   R'   R"   t   register_hookR    R   t   startt   get(   R   R%   R)   t   tt   thread_hooks(    (   R(   R   s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   test_thread_local_hooksb   s    		
c         C   sS   t  d d ƒ } |  j j d k s' t ‚ |  j j | ƒ |  j j d k sO t ‚ d S(   sc   
        Publish should prepare the message and eventually call
        `send` by default.
        t   ai   i    N(   t   dictR   R   R"   R   t   publish(   R   t   data(    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   test_publishu   s    c         C   s–   t  d d ƒ } t ƒ  } |  j j | ƒ | j d k s= t ‚ |  j j d k sU t ‚ |  j j | ƒ | j d k sz t ‚ |  j j d k s’ t ‚ d S(   sé   
        If a hook is installed, and on calling the object
        it does *not* return a message, then we assume that
        the message has been consumed, and should not be
        processed (`sent`) in the normal manner.
        R2   i   i    N(	   R3   R   R   R,   R   R"   R   R   R4   (   R   R5   R%   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   test_display_hook_halts_send   s    	c         C   s–   t  d d ƒ } t ƒ  } |  j j | ƒ | j d k s= t ‚ |  j j d k sU t ‚ |  j j | ƒ | j d k sz t ‚ |  j j d k s’ t ‚ d S(   sã   
        If a hook is installed and on calling the object
        it returns a new message, then we assume that this
        is just a message transformation, and the message
        should be sent in the usual manner.
        R2   i   i    N(	   R3   R   R   R,   R   R"   R   R   R4   (   R   R5   R%   (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt#   test_display_hook_return_calls_send’   s    	c         C   s  t  d d ƒ } t ƒ  } |  j j | ƒ | j d k s= t ‚ |  j j d k sU t ‚ |  j j | ƒ | j d k sz t ‚ |  j j d k s’ t ‚ |  j j	 | ƒ } |  j j | ƒ |  j
 | ƒ | j d k sÖ t ‚ |  j j d k sî t ‚ |  j j	 | ƒ } |  j | ƒ d S(   s`   
        Once a hook is unregistered, it should not be called
        during `publish`.
        R2   i   i    N(   R3   R   R   R,   R   R"   R   R   R4   t   unregister_hookt
   assertTruet   assertFalse(   R   R5   R%   t   firstt   second(    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   test_unregister_hook¥   s    	(   R
   R   R   R   R!   R#   R1   R6   R7   R8   R>   (    (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyR   ?   s   	
					
		t   __main__(   R   t   ost   queueR    t   ImportErrort	   threadingR   t   unittestt	   traitletsR   R   t   ipykernel.zmqshellR   t   jupyter_client.sessionR   t   objectR   R   R   t   TestCaseR   R
   t   main(    (    (    s=   lib/python2.7/site-packages/ipykernel/tests/test_zmq_shell.pyt   <module>   s"   