
    *#                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmc mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ej6                  Z G d dej:                        Zy)z(Unit tests for gsutil seek_ahead_thread.    )absolute_import)print_function)division)unicode_literalsN)queue)range)SeekAheadNameExpansionIterator)SeekAheadResult)SeekAheadThread)UIController)UIThread)	constants)parallelism_framework_util)	unit_utilc                   ,    e Zd ZdZdZd Zd Zd Zd Zy)TestSeekAheadThreadzCUnit tests for SeekAheadThread class and surrounding functionality.   c                 6    G d dt               }t        j                  }|ddf|dz   d|f|dz   ||f|dz  dz   |dz   |dz  fd|d|f|dz  |dz   |dz  ffD ]@  \  }}}t        j                         }t        j
                         }t        j                         }t               }	t        |||	      }
 ||||      }t        |||      }|j                  | j                         |j                  t               |
j                  | j                         |j                         rd|_        | j#                  d|z         | j%                  ||j&                  d	|d
|j&                  d|d       |j)                         }|s-| j#                  d|z         C y)z&Tests cancellation of SeekAheadThread.c                       e Zd ZdZd Zd Zy)JTestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIteratorzCYields dummy results and sends cancellation after some # of yields.c                 J    || _         d| _        || _        d| _        || _        y)a)  Initializes the iterator.

        Args:
          num_iterations: Total number of results to yield.
          num_iterations_before_cancel: Set cancel event before yielding
              on the given iteration.
          cancel_event: threading.Event() to signal SeekAheadThread to stop.
        r   FN)num_iterations_before_canceliterated_resultsnum_iterationscancel_issuedcancel_event)selfr   r   r   s       5platform/gsutil/gslib/tests/test_seek_ahead_thread.py__init__zSTestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIterator.__init__6   s-     -I) !,"(    c              3   B  K   | j                   | j                  k  r| j                  s:| j                   | j                  k\  r!| j                  j                          d| _        t                | xj                   dz  c_         | j                   | j                  k  ry y w)NT   )r   r   r   r   r   setr
   r   s    r   __iter__zSTestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIterator.__iter__F   s     ##d&9&99$$##t'H'HH!!#!%D!
!


1
$
 ##d&9&99s   BBBN__name__
__module____qualname____doc__r   r%    r    r   TrackingCancellationIteratorr   3   s    O) %r    r,   r   r"      )r-   r"   r-   TzLCancellation issued after %s iterations, but SeekAheadThread is still alive.zCancellation issued after z& iterations, SeekAheadThread iterated z results, expected: z	 results.z5Status queue should be empty but contains message: %sN)objectr   NUM_OBJECTS_PER_LIST_PAGE	threadingEventQueuesixStringIOr   r   r   jointhread_wait_timeput_ZERO_TASKS_TO_DO_ARGUMENTis_alive	terminatefailassertEqualr   getvalue)r   r,   noplpr   r   expected_iterationsr   status_queuestreamui_controller	ui_threadseek_ahead_iteratorseek_ahead_threadmessages                 r   testCancellationz$TestSeekAheadThread.testCancellation0   s   %v %: //E	1	1e,uqy%.G	Q	519-y5!U:K	EAIuqy)N+I46I
 __&l[[]l||~f"nm<?i8
6F)*=|*68T22312nnT**+		#	#	%&*#		 <=	> 
2CC ()<)M)M ! !g			I 	?N+r    c                 L    G d dt               }t        j                         }t        j                         }t	        j
                         }t               }t        |||      }d} ||      }t        |||      }	|	j                  | j                         |j                  t               |j                  | j                         |	j                         rd|	_        | j                  d       |j!                         }
|
s| j                  d       | j#                  |
d|z         y)	z0Tests SeekAheadThread providing an object count.c                       e Zd Zd Zd Zy)LTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIteratorc                      || _         d| _        y Nr   )num_resultsyielded)r   rM   s     r   r   zUTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIterator.__init__x   s    &r    c              3      K   | j                   | j                  k  r<t                | xj                   dz  c_         | j                   | j                  k  r;y y w)Nr"   )rN   rM   r
   r$   s    r   r%   zUTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIterator.__iter__|   sC     llT---!
!
,,!
, llT---s   AAAN)r'   r(   r)   r   r%   r+   r    r   SeekAheadResultIteratorrJ   v   s    r    rP   r   TSeekAheadThread is still alive.IStatus queue empty but SeekAheadThread should have posted summary messagez-Estimated work for this command: objects: %s
N)r.   r0   r1   r2   r3   r4   r   r   r   r5   r6   r7   r8   r9   r:   r;   r=   r<   )r   rP   r   r@   rA   rB   rC   num_objectsrD   rE   rF   s              r   testEstimateWithoutSizez+TestSeekAheadThread.testEstimateWithoutSizes   s    	& 	 ??$L;;=L\\^F NMv}=IK1+>'(;\(464001/0NN4(()!!#$(!
ii12ooG
ii " #AKOQr    c           	          G d dt               }t        j                         }t        j                         }t	        j
                         }t               }t        |||      }d}d} |||      }	t        |	||      }
|
j                  | j                         |j                  t               |j                  | j                         |
j                         rd|
_        | j                  d       |j!                         }|s| j                  d       ||z  }| j#                  |d|d	t%        j&                  |      d
       y)z?Tests SeekAheadThread providing an object count and total size.c                       e Zd ZdZd Zd Zy)QTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSizez&Yields dummy result of the given size.c                 .    || _         || _        d| _        y rL   )rS   sizerN   )r   rS   rY   s      r   r   zZTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSize.__init__   s    &	r    c              3      K   | j                   | j                  k  rHt        | j                         | xj                   dz  c_         | j                   | j                  k  rGy y w)N)
data_bytesr"   )rN   rS   r
   rY   r$   s    r   r%   zZTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSize.__iter__   sI     llT---4995
5
,,!
, llT---s   A A%#A%Nr&   r+   r    r   SeekAheadResultIteratorWithSizerW      s    2
r    r\   r   
   TrQ   rR   *Estimated work for this command: objects: , total size: 
N)r.   r0   r1   r2   r3   r4   r   r   r   r5   r6   r7   r8   r9   r:   r;   r=   r<   r   MakeHumanReadable)r   r\   r   r@   rA   rB   rC   rS   object_sizerD   rE   rF   
total_sizes                r   testEstimateWithSizez(TestSeekAheadThread.testEstimateWithSize   s   &  ??$L;;=L\\^F NMv}=IKK9["'(;\(464001/0NN4(()!!#$(!
ii12ooG
ii " # {*J	i11*=	?@r    c           	          | j                         }d}d}t        |      D ]*  }| j                  |dt        |      z  d|z         ||z  }, t	        ddd|gd      }t        j                         }t        j                         }t        j                         }t               }	t        |||	      }
t        |||      }|j                  | j                         |j                  t                |
j                  | j                         |j#                         rd|_        | j'                  d	       |j)                         }|s| j'                  d
       | j+                  |d|dt-        j.                  |      d       y)z/Tests SeekAheadThread with an actual directory.r   r   zobj%s   a)tmpdir	file_namecontentscpNTrQ   rR   r^   r_   r`   )CreateTempDirr   CreateTempFilestrr	   r0   r1   r2   r3   r4   r   r   r   r5   r6   r7   r8   r9   r:   r;   r=   r<   r   ra   )r   rg   	num_filesrc   irD   r   r@   rA   rB   rC   rE   rF   s                r   testWithLocalFilesz&TestSeekAheadThread.testWithLocalFiles   sc   !FIJ 9
$+c!f$4#'!8  - Aoj	  9ax' ??$L;;=L\\^F NMv}=I'(;\(464001/0NN4(()!!#$(!
ii12ooG
ii " # 		I//
;	=>r    N)	r'   r(   r)   r*   r6   rG   rT   rd   rp   r+   r    r   r   r   *   s'    K AF#QJ.@`)>r    r   )r*   
__future__r   r   r   r   r0   r3   	six.movesr   r2   r   gslib.name_expansionr	   gslib.seek_ahead_threadr
   r   gslib.tests.testcaseteststestcasegslib.ui_controllerr   r   gslib.utilsr   r   r   ZERO_TASKS_TO_DO_ARGUMENTr8   GsUtilUnitTestCaser   r+   r    r   <module>r|      sd    / & %  '  
 $  ? 3 3 ' ' , ( ! 2 ! 88 G>(55 G>r    