o
    b?f                     @   s  d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZ ddlmZ d	ZG d
d dZG dd dZG dd 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ZG dd deZG dd deZdd Zeede   eede   G d d! d!Z!G d"d# d#ej"Z#G d$d% d%ej"Z$G d&d' d'Z%G d(d) d)e	Z&G d*d+ d+Z'G d,d- d-ej"Z(dS ).z&
Tests for twisted.enterprise.adbapi.
    N)DictOptional)
ConnectionConnectionLostConnectionPoolTransaction)defer
interfacesreactor)FailurerequireModule)unittestz%
CREATE TABLE simple (
  x integer
)
c                   @   s   e Zd ZU dZi Zeeef ed< e	
eddu rdZdd Zdd Zd	d
 Zd3d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d%d& Zd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#dS )4ADBAPITestBasez,
    Test the asynchronous DB-API code.
    openfun_calledN5ADB-API requires threads, no way to test without themc                 C   s&   |    | j| jd| _| j  dS )zR
        Set up the database and create a connection pool pointing at it.
        )
cp_openfunN)startDBmakePoolopenfundbpoolstartself r   :/usr/lib/python3/dist-packages/twisted/test/test_adbapi.py
extraSetUp(   s   zADBAPITestBase.extraSetUpc                    4    j d}| fdd | fdd |S )NDROP TABLE simplec                    
    j  S Nr   closeresr   r   r   <lambda>2      
 z)ADBAPITestBase.tearDown.<locals>.<lambda>c                          S r    stopDBr#   r   r   r   r%   3       r   runOperationaddCallbackr   dr   r   r   tearDown0      zADBAPITestBase.tearDownc                 C   s   d| j |< d S )NT)r   r   connr   r   r   r   6   s   zADBAPITestBase.openfunc                 C   s&   |s
|  | j d S | || j d S r    )
assertTruer   assertInr2   r   r   r   checkOpenfunCalled9   s   z!ADBAPITestBase.checkOpenfunCalledc                    s    j t} jr*| j | j | j | j | fdd | j	 | j
 | j | j | j | j | j | j |S )Nc                    r'   r    )flushLoggedErrorsr#   r   r   r   r%   F   r*   z*ADBAPITestBase.test_pool.<locals>.<lambda>)r   r,   simple_table_schematest_failuresr-   _testPool_1_1_testPool_1_2_testPool_1_3_testPool_1_4_testPool_2_testPool_3_testPool_4_testPool_5_testPool_6_testPool_7_testPool_8_testPool_9r.   r   r   r   	test_pool?   s    zADBAPITestBase.test_poolc                    ,   t  jjd}| fdddd  |S )Nselect * from NOTABLEc                    
     dS Nzno exceptionfailr#   r   r   r   r%   S   r&   z.ADBAPITestBase._testPool_1_1.<locals>.<lambda>c                 S      d S r    r   fr   r   r   r%   S       r   maybeDeferredr   runQueryaddCallbacksr   r$   r/   r   r   r   r:   Q      zADBAPITestBase._testPool_1_1c                    rG   )Nzdeletexxx from NOTABLEc                    rI   rJ   rK   r#   r   r   r   r%   X   r&   z.ADBAPITestBase._testPool_1_2.<locals>.<lambda>c                 S   rM   r    r   rN   r   r   r   r%   X   rP   )r   rR   r   r,   rT   rU   r   r   r   r;   V   rV   zADBAPITestBase._testPool_1_2c                    .   t  jj j}| fdddd  |S )Nc                    rI   rJ   rK   r#   r   r   r   r%   ]   r&   z.ADBAPITestBase._testPool_1_3.<locals>.<lambda>c                 S   rM   r    r   rN   r   r   r   r%   ]   rP   )r   rR   r   runInteractionbad_interactionrT   rU   r   r   r   r<   [      zADBAPITestBase._testPool_1_3c                    rW   )Nc                    rI   rJ   rK   r#   r   r   r   r%   b   r&   z.ADBAPITestBase._testPool_1_4.<locals>.<lambda>c                 S   rM   r    r   rN   r   r   r   r%   b   rP   )r   rR   r   runWithConnectionbad_withConnectionrT   rU   r   r   r   r=   `   rZ   zADBAPITestBase._testPool_1_4c                    *   d} j |} fdd}|| |S )Nselect count(1) from simplec                    (     t| d d dkd    d S )Nr   zInteraction not rolled back)r4   intr6   rowr   r   r   _checkj   s   z*ADBAPITestBase._testPool_2.<locals>._checkr   rS   r-   r   r$   sqlr/   rc   r   r   r   r>   e   s
   
zADBAPITestBase._testPool_2c                    sl   d}g }t  jD ]}d| }| j| q	t|} fdd}||  fdd}|| |S )Nr^   z insert into simple(x) values(%d)c                    s   d} j |}|S )Nselect x from simple order by x)r   rS   )r$   rf   r/   r   r   r   _selectz   s   z+ADBAPITestBase._testPool_3.<locals>._selectc                    sZ     t|  jkd t jD ]}  t| | dkd   | | d |kd qd S )NzWrong number of rows   Wrong size rowr   zValues not returned.)r4   lennum_iterationsrange)rowsir   r   r   rc      s
   z*ADBAPITestBase._testPool_3.<locals>._check)rm   rl   appendr   r,   r   gatherResultsr-   )r   r$   rf   insertsro   r/   rh   rc   r   r   r   r?   q   s   


zADBAPITestBase._testPool_3c                    $    j  j}| fdd |S )Nc                         | dS NdoneassertEqualr#   r   r   r   r%          z,ADBAPITestBase._testPool_4.<locals>.<lambda>)r   rX   interactionr-   rU   r   r   r   r@         zADBAPITestBase._testPool_4c                    rs   )Nc                    rt   ru   rw   r#   r   r   r   r%      ry   z,ADBAPITestBase._testPool_5.<locals>.<lambda>)r   r[   withConnectionr-   rU   r   r   r   rA      r{   zADBAPITestBase._testPool_5c                 C   s   | j | j}|S r    )r   r[   close_withConnectionrU   r   r   r   rB      s   zADBAPITestBase._testPool_6c                    sV   g }t  jD ]}d| }| j| qtj|dd} fdd}|| |S )Nz!select x from simple where x = %dTfireOnOneErrbackc                    s4   t  jD ]} | | d d d |kd qd S )Nri   r   zValue not returned)rm   rl   r4   )resultro   r   r   r   rc      s   "z*ADBAPITestBase._testPool_7.<locals>._check)rm   rl   rp   r   rS   r   DeferredListr-   )r   r$   dsro   rf   dlistrc   r   r   r   rC      s   
zADBAPITestBase._testPool_7c                 C   s@   g }t | jD ]}d| }|| j| qtj|dd}|S )Nzdelete from simple where x = %dTr~   )rm   rl   rp   r   r,   r   r   )r   r$   r   ro   rf   r   r   r   r   rD      s   zADBAPITestBase._testPool_8c                    r]   )Nr^   c                    r_   )Nr   z)Didn't successfully delete table contents)r4   r`   checkConnectra   r   r   r   rc      s   z*ADBAPITestBase._testPool_9.<locals>._checkrd   re   r   r   r   rE      s
   
zADBAPITestBase._testPool_9c                 C   s   | j  }| | | }|d |d | }| t|d | t|d d | |d d d |d |d | t| d |  | j 	| dS )z/Check the connect/disconnect synchronous calls.zinsert into simple(x) values(1)zselect x from simpleri   r   zdelete from simpleN)
r   connectr6   cursorexecutefetchallrx   rk   r"   
disconnect)r   r3   cursr$   r   r   r   r      s   





zADBAPITestBase.checkConnectc                 C   s^   | d t| jD ]}| }| t|dkd | |d |kd q
| | d dS )Nrg   ri   rj   r   Value not returned.zToo many rowsrv   )r   rm   rl   fetchoner4   rk   assertIsNone)r   transactionro   rb   r   r   r   rz      s   
zADBAPITestBase.interactionc                 C   s   | j r|d |d d S )Nzinsert into simple(x) values(0)rH   )can_rollbackr   )r   r   r   r   r   rY         
zADBAPITestBase.bad_interactionc                 C   sl   |  }z,|d t| jD ]}| }| t|dkd | |d |kd qW |  dS |  w )Nrg   ri   rj   r   r   rv   )r   r   rm   rl   r   r4   rk   r"   )r   r3   r   ro   rb   r   r   r   r|      s   

zADBAPITestBase.withConnectionc                 C   s   |   d S r    )r"   r2   r   r   r   r}      s   z#ADBAPITestBase.close_withConnectionc                 C   s,   |  }z|d W |  d S |  w )NrH   )r   r   r"   )r   r3   r   r   r   r   r\      s   z!ADBAPITestBase.bad_withConnectionr    )$__name__
__module____qualname____doc__r   r   objectbool__annotations__r	   IReactorThreadsr
   skipr   r0   r   r6   rF   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   r   rz   rY   r|   r}   r\   r   r   r   r   r      s8   
 
		r   c                   @   sd   e Zd ZdZeeddu r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S )ReconnectTestBasez;
    Test the asynchronous DB-API code with reconnect.
    Nr   c                 C   sF   | j du r
td|   | jdd| j d| _| j  | jtS )z
        Skip the test if C{good_sql} is unavailable.  Otherwise, set up the
        database, create a connection pool pointed at it, and set up a simple
        schema in it.
        Nzno good sql for reconnect testri   T)cp_maxcp_reconnectcp_good_sql)	good_sqlr   SkipTestr   r   r   r   r,   r8   r   r   r   r   r     s   


zReconnectTestBase.extraSetUpc                    r   )Nr   c                    r   r    r!   r#   r   r   r   r%     r&   z,ReconnectTestBase.tearDown.<locals>.<lambda>c                    r'   r    r(   r#   r   r   r   r%     r*   r+   r.   r   r   r   r0     r1   zReconnectTestBase.tearDownc                 C   sP   t d }|| j || j | js|| j || j || j |S r    )	r   succeedr-   _testPool_1r>   early_reconnectr?   r@   rA   r.   r   r   r   rF     s   
zReconnectTestBase.test_poolc                    r]   )Nr^   c                          t| d d dkd d S Nr   zTable not emptyr4   r`   ra   r   r   r   rc   %      z-ReconnectTestBase._testPool_1.<locals>._checkrd   re   r   r   r   r   !  
   
zReconnectTestBase._testPool_1c                 C   s   t | jj d   d S )Nr   )listr   connectionsvaluesr"   )r   r$   r   r   r   r>   +  s   zReconnectTestBase._testPool_2c                    s0   d}t  jj|}| fdddd  |S )Nr^   c                    rI   rJ   rK   r#   r   r   r   r%   2  r&   z/ReconnectTestBase._testPool_3.<locals>.<lambda>c                 S   rM   r    r   rN   r   r   r   r%   2  rP   rQ   r   r$   rf   r/   r   r   r   r?   /  s   zReconnectTestBase._testPool_3c                    r]   )Nr^   c                    r   r   r   ra   r   r   r   rc   9  r   z-ReconnectTestBase._testPool_4.<locals>._checkrd   re   r   r   r   r@   5  r   zReconnectTestBase._testPool_4c                    s<       d}t jj|}| fdd fdd |S )NrH   c                    rI   rJ   rK   r#   r   r   r   r%   D  r&   z/ReconnectTestBase._testPool_5.<locals>.<lambda>c                    s     | tS r    )assertFalsecheckr   rN   r   r   r   r%   E  s    )r7   r   rR   r   rS   rT   r   r   r   r   rA   ?  s   

zReconnectTestBase._testPool_5)r   r   r   r   r	   r   r
   r   r   r0   rF   r   r>   r?   r@   rA   r   r   r   r   r      s    


r   c                   @   s   e Zd ZU dZdZee ed< dZdZ	dZ
dZdZdZdZdZdZejZee ed< dZdZdZdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )DBTestConnectora>  
    A class which knows how to test for the presence of
    and establish a connection to a relational database.

    To enable test cases  which use a central, system database,
    you must create a database named DB_NAME with a user DB_USER
    and password DB_PASS with full access rights to database DB_NAME.
    NTEST_PREFIXtwisted_testTr   2   c                 C   s6   |   | _t| j |  std| j |  S )Nz%s: Cannot access db)	mktempDB_DIRosmkdircan_connectr   r   r   r   r   r   r   r   setUpi  s
   
zDBTestConnector.setUpc                 C      t  )zXReturn true if this database is present on the system
        and can be used in a test.NotImplementedErrorr   r   r   r   r   p  s   zDBTestConnector.can_connectc                 C      dS )z+Take any steps needed to bring database up.Nr   r   r   r   r   r   u     zDBTestConnector.startDBc                 C   r   )zBring database down, if needed.Nr   r   r   r   r   r)   y  r   zDBTestConnector.stopDBc                 K   s,   |   \}}| }|| t|i |S )z;Create a connection pool with additional keyword arguments.)getPoolArgscopyupdater   )r   newkwargskwr   r   r   r   }  s   
zDBTestConnector.makePoolc                 C   r   )zReturn a tuple (args, kw) of list and keyword arguments
        that need to be passed to ConnectionPool to create a connection
        to this database.r   r   r   r   r   r     s   zDBTestConnector.getPoolArgs)r   r   r   r   r   r   strr   DB_NAMEDB_USERDB_PASSr   nulls_oktrailing_spaces_okr   r9   escape_slashesr   r   r   	can_clearrl   r   r   r   r)   r   r   r   r   r   r   r   J  s,   
 
r   c                   @   4   e Zd ZdZdZdZdZdd Zdd Zd	d
 Z	dS )SQLite3ConnectorzB
    Connector that uses the stdlib SQLite3 database support.
    SQLite3Fri   c                 C      t dd u rdS dS )Nsqlite3FTr   r   r   r   r   r        zSQLite3Connector.can_connectc                 C   6   t j| j| j| _t j| jrt | j d S d S r    r   pathjoinr   r   databaseexistsunlinkr   r   r   r   r        zSQLite3Connector.startDBc                 C      d}| j ddd}||fS )N)r   ri   Fr   r   check_same_threadr   r   r   r   r   r   r   r        zSQLite3Connector.getPoolArgsN
r   r   r   r   r   r   rl   r   r   r   r   r   r   r   r         r   c                   @   r   )PySQLite2ConnectorzA
    Connector that uses pysqlite's SQLite database support.
    	pysqlite2Fri   c                 C   r   )Npysqlite2.dbapi2FTr   r   r   r   r   r     r   zPySQLite2Connector.can_connectc                 C   r   r    r   r   r   r   r   r     r   zPySQLite2Connector.startDBc                 C   r   )N)r   ri   Fr   r   r   r   r   r   r     r   zPySQLite2Connector.getPoolArgsNr   r   r   r   r   r     r   r   c                   @       e Zd ZdZdd Zdd ZdS )PyPgSQLConnectorPyPgSQLc                 C   s`   zddl m} W n
 ty   Y dS w z|j| j| j| jd}|  W dS  ty/   Y dS w )Nr   )PgSQLFr   userpasswordT)pyPgSQLr   BaseExceptionr   r   r   r   r"   )r   r   r3   r   r   r   r     s   zPyPgSQLConnector.can_connectc                 C       d}| j | j| jdd}||fS )N)zpyPgSQL.PgSQLr   r   r   r   cp_minr   r   r   r   r   r   r   r        zPyPgSQLConnector.getPoolArgsNr   r   r   r   r   r   r   r   r   r   r         r   c                   @   r   )PsycopgConnectorPsycopgc                 C   \   zdd l }W n
 ty   Y dS w z|j| j| j| jd}|  W dS  ty-   Y dS w )Nr   Fr   T)psycopgr   r   r   r   r   r"   )r   r   r3   r   r   r   r        zPsycopgConnector.can_connectc                 C   r   )N)r   r   r   r   r   r   r   r   r     r   zPsycopgConnector.getPoolArgsNr   r   r   r   r   r     r   r   c                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )MySQLConnectorMySQLFc                 C   r   )Nr   Fdbr   passwdT)MySQLdbr   r   r   r   r   r"   )r   r   r3   r   r   r   r     r   zMySQLConnector.can_connectc                 C   s   d}| j | j| jd}||fS )N)r   r   r   r   r   r   r   r     s   zMySQLConnector.getPoolArgsN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   sD   e Zd ZdZdZdZdZdZdZdd Z	dd Z
d	d
 Zdd ZdS )FirebirdConnectorFirebirdFN   c                 C   s<   t dd u rdS z|   |   W dS  ty   Y dS w )NkinterbasdbFT)r   r   r)   r   r   r   r   r   r     s   zFirebirdConnector.can_connectc                 C   sf   dd l }tj| jtj| _t| jtj	tj
 tj  d}|| j| j| jf; }||}|  d S )Nr   z,create database "%s" user "%s" password "%s")r  r   r   r   r   r   r   chmodstatS_IRWXUS_IRWXGS_IRWXOr   r   create_databaser"   )r   r  rf   r3   r   r   r   r   &  s   
zFirebirdConnector.startDBc                 C   s    d}| j d| j| jd}||fS )N)r  	127.0.0.1r   hostr   r   r   r   r   r   r   r   0  s   zFirebirdConnector.getPoolArgsc                 C   s,   dd l }|j| jd| j| jd}|  d S )Nr   r	  r
  )r  r   r   r   r   drop_database)r   r  r3   r   r   r   r)   :  s   zFirebirdConnector.stopDB)r   r   r   r   r9   r   r   r   rl   r   r   r   r)   r   r   r   r   r     s    


r   c                    s   t tttttg}i }|D ]0  j| }G  fddd | tj}||_	t
 dr8d j dd |g |_|||< q|| dS )aQ  
    Make a test case for every db connector which can connect.

    @param base: Base class for test case. Additional base classes
                 will be a DBConnector subclass and unittest.TestCase
    @param suffix: A suffix used to create test case names. Prefixes
                   are defined in the DBConnector subclasses.
    c                       s   e Zd Z jZdS )zmakeSQLTests.<locals>.testcaseN)r   r   r   r   	connclassr   r   testcase[  s    
r  r   .r   N)r   r   r   r   r   r   r   r   TestCaser   hasattrr   r   splitr   )basesuffixglobals
connectorstestsnamer  r   r  r   makeSQLTestsF  s$   



r  ADBAPITestsReconnectTestsc                   @   s0   e Zd ZdZdZdZdd Zdd Zdd Zd	S )
FakePoolz
    A fake L{ConnectionPool} for tests.

    @ivar connectionFactory: factory for making connections returned by the
        C{connect} method.
    @type connectionFactory: any callable
    Tc                 C   s
   || _ d S r    connectionFactory)r   r   r   r   r   __init__}  s   
zFakePool.__init__c                 C   s   |   S )zB
        Return an instance of C{self.connectionFactory}.
        r  r   r   r   r   r     s   zFakePool.connectc                 C   r   )z
        Do nothing.
        Nr   )r   
connectionr   r   r   r     rP   zFakePool.disconnectN)	r   r   r   r   	reconnectnoisyr!  r   r   r   r   r   r   r  q  s    r  c                   @      e Zd ZdZdd ZdS )ConnectionTestsz,
    Tests for the L{Connection} class.
    c                 C   sb   G dd d}t |}t|}| t|j | t}| t|d | |d j	j
d d dS )z|
        If an error happens during rollback, L{ConnectionLost} is raised but
        the original error is logged.
        c                   @      e Zd Zdd ZdS )zIConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaisec                 S      t dNproblem!RuntimeErrorr   r   r   r   rollback     zRConnectionTests.test_rollbackErrorLogged.<locals>.ConnectionRollbackRaise.rollbackN)r   r   r   r-  r   r   r   r   ConnectionRollbackRaise      r/  ri   r   r*  N)r  r   assertRaisesr   r-  r7   r,  rx   rk   valuer   )r   r/  poolr"  errorsr   r   r   test_rollbackErrorLogged  s   
z(ConnectionTests.test_rollbackErrorLoggedN)r   r   r   r   r5  r   r   r   r   r&        r&  c                   @   r%  )TransactionTestsz-
    Tests for the L{Transaction} class.
    c                 C   s`   G dd d}t d}t|| }|  | t}| t|d | |d jjd d dS )z
        If the cursor creation raises an error in L{Transaction.reopen}, it
        reconnects but log the error occurred.
        c                   @   r   )zNTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaiser   c                 S   rM   r    r   r   r   r   r   r#       zXTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.reconnectc                 S   s$   | j dkr|  j d7  _ tdd S )Nr   ri   r*  )countr,  r   r   r   r   r     s   
zUTransactionTests.test_reopenLogErrorIfReconnect.<locals>.ConnectionCursorRaise.cursorN)r   r   r   r9  r#  r   r   r   r   r   ConnectionCursorRaise  s    r:  Nri   r   r*  )	r  r   reopenr7   r,  rx   rk   r2  r   )r   r:  r3  r   r4  r   r   r   test_reopenLogErrorIfReconnect  s   
z/TransactionTests.test_reopenLogErrorIfReconnectN)r   r   r   r   r<  r   r   r   r   r7    r6  r7  c                   @   r'  )NonThreadPoolc                 O   sB   d}z	||i |}W n t y   d}t }Y nw ||| d S )NTF)	Exceptionr   )r   onResultrO   ar   successr   r   r   r   callInThreadWithCallback  s   
z&NonThreadPool.callInThreadWithCallbackN)r   r   r   rB  r   r   r   r   r=    r0  r=  c                   @   s   e Zd ZdZe Zdd ZdS )DummyConnectionPoolz'
    A testable L{ConnectionPool};
    c                 C   s
   t | _dS )z*
        Don't forward init call.
        N)r
   _reactorr   r   r   r   r!    s   
zDummyConnectionPool.__init__N)r   r   r   r   r=  
threadpoolr!  r   r   r   r   rC    s    rC  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )EventReactora  
    Partial L{IReactorCore} implementation with simple event-related
    methods.

    @ivar _running: A C{bool} indicating whether the reactor is pretending
        to have been started already or not.

    @ivar triggers: A C{list} of pending system event triggers.
    c                 C   s   || _ g | _d S r    )_runningtriggers)r   runningr   r   r   r!    s   
zEventReactor.__init__c                 C   s   | j r|  d S | dd|S )Nafterstartup)rG  addSystemEventTrigger)r   functionr   r   r   callWhenRunning  r   zEventReactor.callWhenRunningc                 C   s   |||f}| j | |S r    )rH  rp   )r   phaseeventtriggerhandler   r   r   rL    s   
z"EventReactor.addSystemEventTriggerc                 C   s   | j | d S r    )rH  remove)r   rR  r   r   r   removeSystemEventTrigger  s   z%EventReactor.removeSystemEventTriggerN)r   r   r   r   r!  rN  rL  rT  r   r   r   r   rF    s    
rF  c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )ConnectionPoolTestsz+
    Unit tests for L{ConnectionPool}.
    c                    sR   G dd d}dd }t  }||_||} |t} fdd}|| |S )z
        If rollback fails, L{ConnectionPool.runWithConnection} raises the
        original exception and log the error of the rollback.
        c                   @      e Zd Zdd Zdd ZdS )z]ConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                 S   rM   r    r   r   r3  r   r   r   r!    r8  zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__c                 S   r(  r)  r+  r   r   r   r   r-    r.  zfConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollbackNr   r   r   r!  r-  r   r   r   r   r/        r/  c                 S   r(  Nfoo
ValueError)r"  r   r   r   raisingFunction
  r.  zUConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.raisingFunctionc                    6     t} t|d  |d jjd d d S Nri   r   r*  r7   r,  rx   rk   r2  r   ignoredr4  r   r   r   cbFailed     
zNConnectionPoolTests.test_runWithConnectionRaiseOriginalError.<locals>.cbFailed)rC  r   r[   assertFailurer]  r-   )r   r/  r^  r3  r/   rd  r   r   r   (test_runWithConnectionRaiseOriginalError  s   

z<ConnectionPoolTests.test_runWithConnectionRaiseOriginalErrorc                 C   sV   G dd d}t  }||  | t}| t|d | |d jjd d dS )z;
        L{ConnectionPool._close} logs exceptions.
        c                   @   r'  )zDConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaisec                 S   r(  r)  r+  r   r   r   r   r"      r.  zJConnectionPoolTests.test_closeLogError.<locals>.ConnectionCloseRaise.closeN)r   r   r   r"   r   r   r   r   ConnectionCloseRaise  r0  rh  ri   r   r*  N)rC  _closer7   r,  rx   rk   r2  r   )r   rh  r3  r4  r   r   r   test_closeLogError  s   
z&ConnectionPoolTests.test_closeLogErrorc                    sf   G dd d}G dd d}dd }t  }||_||_||} |t} fdd}|| |S )	z
        If rollback fails, L{ConnectionPool.runInteraction} raises the
        original exception and log the error of the rollback.
        c                   @   rV  )z^ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaisec                 S   rM   r    r   rW  r   r   r   r!  1  r8  zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.__init__c                 S   r(  r)  r+  r   r   r   r   r-  4  r.  zgConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.ConnectionRollbackRaise.rollbackNrX  r   r   r   r   r/  0  rY  r/  c                   @   r'  )zWConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransactionc                 S   rM   r    r   )r   r3  r"  r   r   r   r!  8  r8  z`ConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.DummyTransaction.__init__N)r   r   r   r!  r   r   r   r   DummyTransaction7  r0  rk  c                 S   r(  rZ  r\  )r   r   r   r   r^  ;  r.  zVConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.raisingFunctionc                    r_  r`  ra  rb  r   r   r   rd  E  re  zOConnectionPoolTests.test_runWithInteractionRaiseOriginalError.<locals>.cbFailed)rC  r   transactionFactoryrX   rf  r]  r-   )r   r/  rk  r^  r3  r/   rd  r   r   r   )test_runWithInteractionRaiseOriginalError*  s   

z=ConnectionPoolTests.test_runWithInteractionRaiseOriginalErrorc                 C   D   t d}td|d}| |jdd|jfg |  | |j dS )z
        If L{ConnectionPool.close} is called without L{ConnectionPool.start}
        having been called, the pool's startup event is cancelled.
        Ftwisted.test.test_adbapi
cp_reactorrJ  rK  N)rF  r   rx   rH  _startr"   r   r   r
   r3  r   r   r   test_unstartedCloseM  
   z'ConnectionPoolTests.test_unstartedClosec                 C   rn  )z
        If L{ConnectionPool.close} is called after it has been started, but
        not by its shutdown trigger, the shutdown trigger is cancelled.
        Tro  rp  duringshutdownN)rF  r   rx   rH  
finalCloser"   r   rs  r   r   r   test_startedCloseZ  ru  z%ConnectionPoolTests.test_startedCloseN)	r   r   r   r   rg  rj  rm  rt  ry  r   r   r   r   rU    s    #rU  ))r   r   r  typingr   r   twisted.enterprise.adbapir   r   r   r   twisted.internetr   r	   r
   twisted.python.failurer   twisted.python.reflectr   twisted.trialr   r8   r   r   r   r   r   r   r   r   r   r  r  r  r  r&  r7  r=  rC  rF  rU  r   r   r   r   <module>   s<    ]PA4$