B
    8d!                 @   s  d Z ddlmZ ddlmZ ddlZddlZddlZddlm	Z	m
Z
 ddlZddlmZ ejjZddlmZ ddlZddlm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Z"dZ#G dd de$Z%G dd deZ&G dd de&Z'edddZ(dS )zBase class for notebook tests.    )hexlify)contextmanagerN)ThreadEvent)TestCase)patch)IOLoop)Config   )NotebookAppurlencode_unix_socket)url_path_join)TemporaryDirectory   g?c               @   s   e Zd ZdS )TimeoutErrorN)__name__
__module____qualname__ r   r   k/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/nbclassic/tests/launchnotebook.pyr      s   r   c               @   s   e Zd ZdZdZdZdZedd Zedd Z	ed	d
 Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZdS )NotebookTestBasezA base class for tests that need a running notebook.

    This create some empty config and runtime directories
    and then starts the notebook server with them.
    i50  Nz/a%40b/c             C   s   |   d }xtttt D ]|}y| | W nd tk
rV } z|W dd}~X Y q tk
r } z"| j	 szt
d|tt W dd}~X Y qX dS qW tddS )zWait for the server to be alivezapi/contentsNz#The notebook server failed to startz.The notebook server didn't start up correctly.)base_urlrangeintMAX_WAITTIMEPOLL_INTERVAL	fetch_urlModuleNotFoundError	Exceptionnotebook_threadis_aliveRuntimeErrortimesleepr   )clsurl_errorer   r   r   wait_until_alive/   s    

z!NotebookTestBase.wait_until_alivec             C   s$   | j jtd | j  r tddS )z7Wait for the server process to terminate after shutdown)timeoutzUndead notebook serverN)r   joinr   r    r   )r$   r   r   r   wait_until_deadB   s    
z NotebookTestBase.wait_until_deadc             C   s   i }| j rd| j  |d< |S )Nztoken Authorization)token)r$   headersr   r   r   auth_headersI   s    zNotebookTestBase.auth_headersc             C   s
   t | S )N)requestsget)r%   r   r   r   r   P   s    zNotebookTestBase.fetch_urlc             K   s8   | di }||   tj|t|  |f|}|S )zQSend a request to my server

        with authentication and everything.
        r/   )
setdefaultupdater0   r1   requestr   r   )r$   verbpathkwargsr/   responser   r   r   r5   T   s    zNotebookTestBase.requestc             C   s.   | j tjtjt| j dd| j| j| j	dS )Nz.ipython1)HOME
PYTHONPATHZ
IPYTHONDIRZJUPYTER_NO_CONFIGZJUPYTER_CONFIG_DIRZJUPYTER_DATA_DIRZJUPYTER_RUNTIME_DIR)
home_dirospathsepr+   sysr7   pjoin
config_dirdata_dirruntime_dir)r$   r   r   r   get_patch_enva   s    
zNotebookTestBase.get_patch_envc             C   s   g S )Nr   )r$   r   r   r   get_argvm   s    zNotebookTestBase.get_argvc             C   s   t | jdS )N)port)dictrG   )r$   r   r   r   get_bind_argsq   s    zNotebookTestBase.get_bind_argsc                s  t   _ fdd}|d _|d } _|d } _|d } _|d _td 	  _
 j
  tjtj|d	d
g|dd
gd _ j   jpt dj_ttdd _t  fdd}t|d _d j_ j       d S )Nc           
      sZ   t jj jjf|  }yt | W n0 tk
rT } z|jtjkrD W d d }~X Y nX |S )N)	r>   r7   r+   tmp_dirnamemakedirsOSErrorerrnoEEXIST)partsr7   r(   )r$   r   r   tmpx   s    z)NotebookTestBase.setup_class.<locals>.tmphomedataconfigruntimeZ	notebooksz
os.environZsharejupyteretc)ZSYSTEM_JUPYTER_PATHZSYSTEM_CONFIG_PATHz:memory:   asciic                 s   zЈ   } tf dd j j j j jd jd
|  } _dt	j
krx|  dd l}||  dd l}|  dd |_d|j_g |j_|j  d d|j_g |j_t }|j |  W d   |j  X d S )	Nr   FT)
Zport_retriesZopen_browserrB   rC   rD   notebook_dirr   rT   Z
allow_rootr.   asyncioc               S   s   d S )Nr   r   r   r   r   <lambda>       zDNotebookTestBase.setup_class.<locals>.start_thread.<locals>.<lambda>)argv)rI   r   rB   rC   rD   rZ   
url_prefixr.   notebookr@   modulesZ_init_asyncio_patchr[   set_event_loopnew_event_loopnest_asyncioapplyZinit_signallog	propagatehandlersZ
initializerF   r   currentZadd_callbacksetstartZsession_managerclose)Z	bind_argsZappr[   rd   loop)r$   rT   startedr   r   start_thread   s>    

z2NotebookTestBase.setup_class.<locals>.start_thread)targetT)r   rJ   r=   rC   rB   rD   rZ   r   rH   rE   	env_patchrk   Zmultiplejupyter_corepaths
path_patchrT   r	   ZNotebookNotaryZdb_filer   r>   urandomdecoder.   r   r   r   daemonwaitr)   )r$   rQ   rC   rB   rD   ro   r   )r$   rT   rn   r   setup_classu   s0    	




*
zNotebookTestBase.setup_classc             C   sn   | j   |   | j  | j  | j  dd }t|d}d|_|	  |
d | rjtdd S )Nc               S   s   t j   d S )N)zmqContextinstancetermr   r   r   r   cleanup_zmq   s    z4NotebookTestBase.teardown_class.<locals>.cleanup_zmq)rp   T   zFFailed to teardown zmq Context, open sockets likely left lying around.)r`   stopr,   rq   rt   rJ   cleanupr   rw   rk   r+   r    r!   )r$   r~   tr   r   r   teardown_class   s    





zNotebookTestBase.teardown_classc             C   s   d| j  | j S )Nzhttp://localhost:)rG   r_   )r$   r   r   r   r      s    zNotebookTestBase.base_url)r   r   r   __doc__rG   rT   r_   classmethodr)   r,   r0   staticmethodr   r5   rE   rF   rI   ry   r   r   r   r   r   r   r   "   s   Ur   c               @   s@   e Zd Zde  dZedd Zedd Ze	dd Z
d	S )
UNIXSocketNotebookTestBasez/tmp/.notebook.z.sockc             C   s   t | jdS )N)sock)rH   r   )r$   r   r   r   rI      s    z(UNIXSocketNotebookTestBase.get_bind_argsc             C   s   t | j | j S )N)r   r   r_   )r$   r   r   r   r      s    z#UNIXSocketNotebookTestBase.base_urlc          	   C   s0   t jdkrdd l}|  t| S Q R X d S )Nntr   )r>   rK   requests_unixsocketZmonkeypatchr1   r2   )r%   r   r   r   r   r      s    

z$UNIXSocketNotebookTestBase.fetch_urlN)r   r   r   r>   getpidr   r   rI   r   r   r   r   r   r   r   r      s   r   c          
   c   s|   y
d V  W n` t jk
rj } z@|jj}|| ksBtd|  d| |rZ|t|ksZt|W d d }~X Y nX dsxtdd S )NzExpected status z, got FzExpected HTTP error status)r1   	HTTPErrorr9   status_codeAssertionErrorstr)statusmsgr(   Zreal_statusr   r   r   assert_http_error   s    

&r   )N))r   binasciir   
contextlibr   rN   r>   r@   	threadingr   r   r"   Zunittestr   r7   r+   rA   Zunittest.mockr   r1   Ztornado.ioloopr   rz   Zjupyter_core.pathsrr   Ztraitlets.configr	   Znotebookappr   r   utilsr   Zipython_genutils.tempdirr   r   r   r   r   r   r   r   r   r   r   r   <module>   s4    @