
    *#                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJ	r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJs  Jr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  \R6                  r " S S\R:                  5      rg)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                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)
TestSeekAheadThread*   zCUnit tests for SeekAheadThread class and surrounding functionality.   c                 P    " S S[         5      n[        R                  nUSS4US-   SU4US-   X"4US-  S-   US-   US-  4SUSU4US-  US-   US-  44 GHM  u  p4n[        R                  " 5       n[
        R
                  " 5       n[        R                  " 5       n[        5       n	[        XxU	5      n
U" X4U5      n[        XU5      nUR                  U R                  5        UR                  [        5        U
R                  U R                  5        UR                  5       (       a  SUl        U R#                  SU-  5        U R%                  X[R&                  S	U< S
UR&                  < SU< S35        UR)                  5       nU(       d  GM9  U R#                  SU-  5        GMP     g)z&Tests cancellation of SeekAheadThread.c                   $    \ rS rSrSrS rS rSrg)JTestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIterator3   zCYields dummy results and sends cancellation after some # of yields.c                 D    X l         SU l        Xl        SU l        X0l        g)zInitializes 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__STestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIterator.__init__6   s'     -I) !,"(    c              3   T  #    U R                   U R                  :  a  U R                  (       d;  U R                   U R                  :  a!  U R                  R                  5         SU l        [        5       v   U =R                   S-  sl         U R                   U R                  :  a  M  g g 7f)NT   )r   r   r   r   r   setr
   r   s    r    __iter__STestSeekAheadThread.testCancellation.<locals>.TrackingCancellationIterator.__iter__F   s     ##d&9&99$$##t'H'HH!!#!%D!
!


1
$
 ##d&9&99s   B"B(&B()r   r   r   r   r   N__name__
__module____qualname____firstlineno____doc__r!   r(   __static_attributes__ r#   r    TrackingCancellationIteratorr   3   s    O) %r#   r2   r   r%      )r3   r%   r3   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   r2   noplpr   r   expected_iterationsr   status_queuestreamui_controller	ui_threadseek_ahead_iteratorseek_ahead_threadmessages                 r    testCancellation$TestSeekAheadThread.testCancellation0   s   %v %: //E	1	1e,uqy%.G	Q	519-y5!U:K	EAIuqy)N+I6I
 __&l[[]l||~f"nm<?i8
F)*=*68T22312nnT**+		#	#	%	%&*#		 <=	> 
CC ()<)M)M ! !g			I 	?N+r#   c                 b    " S S[         5      n[        R                  " 5       n[        R                  " 5       n[        R
                  " 5       n[        5       n[        X4U5      nSnU" U5      n[        XU5      n	U	R                  U R                  5        UR                  [        5        UR                  U R                  5        U	R                  5       (       a  SU	l        U R                  S5        UR!                  5       n
U
(       d  U R                  S5        U R#                  U
SU-  5        g)	z0Tests SeekAheadThread providing an object count.c                        \ rS rSrS rS rSrg)LTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIteratorv   c                     Xl         SU l        g Nr   num_resultsyielded)r   rV   s     r    r!   UTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIterator.__init__x   s    &r#   c              3      #    U R                   U R                  :  a>  [        5       v   U =R                   S-  sl         U R                   U R                  :  a  M=  g g 7f)Nr%   )rW   rV   r
   r'   s    r    r(   UTestSeekAheadThread.testEstimateWithoutSize.<locals>.SeekAheadResultIterator.__iter__|   sB     llT---!
!
,,!
, llT---s   AAArU   N)r+   r,   r-   r.   r!   r(   r0   r1   r#   r    SeekAheadResultIteratorrQ   v   s    r#   r[   r   TSeekAheadThread is still alive.IStatus queue empty but SeekAheadThread should have posted summary messagez-Estimated work for this command: objects: %s
N)r4   r6   r7   r8   r9   r:   r   r   r   r;   r<   r=   r>   r?   r@   rA   rC   rB   )r   r[   r   rF   rG   rH   rI   num_objectsrJ   rK   rL   s              r    testEstimateWithoutSize+TestSeekAheadThread.testEstimateWithoutSizes   s    	& 	 ??$L;;=L\\^F NM}=IK1+>'(;(464001/0NN4(()!!##$(!
ii12ooG
ii " #AKOQr#   c           	          " S S[         5      n[        R                  " 5       n[        R                  " 5       n[        R
                  " 5       n[        5       n[        X4U5      nSnSnU" Xx5      n	[        XU5      n
U
R                  U R                  5        UR                  [        5        UR                  U R                  5        U
R                  5       (       a  SU
l        U R                  S5        UR!                  5       nU(       d  U R                  S5        Xx-  nU R#                  USU< S	[$        R&                  " U5      < S
35        g)z?Tests SeekAheadThread providing an object count and total size.c                   $    \ rS rSrSrS rS rSrg)QTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSize   z&Yields dummy result of the given size.c                 *    Xl         X l        SU l        g rT   r^   sizerW   )r   r^   rg   s      r    r!   ZTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSize.__init__   s    &	r#   c              3      #    U R                   U R                  :  aG  [        U R                  S9v   U =R                   S-  sl         U R                   U R                  :  a  MF  g g 7f)N)
data_bytesr%   )rW   r^   r
   rg   r'   s    r    r(   ZTestSeekAheadThread.testEstimateWithSize.<locals>.SeekAheadResultIteratorWithSize.__iter__   sH     llT---4995
5
,,!
, llT---s   AA%#A%rf   Nr*   r1   r#   r    SeekAheadResultIteratorWithSizerc      s    2
r#   rl   r   
   Tr\   r]   *Estimated work for this command: objects: , total size: 
N)r4   r6   r7   r8   r9   r:   r   r   r   r;   r<   r=   r>   r?   r@   rA   rC   rB   r   MakeHumanReadable)r   rl   r   rF   rG   rH   rI   r^   object_sizerJ   rK   rL   
total_sizes                r    testEstimateWithSize(TestSeekAheadThread.testEstimateWithSize   s   &  ??$L;;=L\\^F NM}=IKK9"'(;(464001/0NN4(()!!##$(!
ii12ooG
ii " # *J	i11*=	?@r#   c           	         U R                  5       nSnSn[        U5       H'  nU R                  US[        U5      -  SU-  S9  X4-  nM)     [	        SSSU/S5      n[
        R                  " 5       n[        R                  " 5       n[        R                  " 5       n[        5       n	[        XxU	5      n
[        XVU5      nUR                  U R                  5        UR                  [         5        U
R                  U R                  5        UR#                  5       (       a  SUl        U R'                  S	5        UR)                  5       nU(       d  U R'                  S
5        U R+                  USU< S[,        R.                  " U5      < S35        g)z/Tests SeekAheadThread with an actual directory.r   r   zobj%s   a)tmpdir	file_namecontentscpNTr\   r]   rn   ro   rp   )CreateTempDirr   CreateTempFilestrr	   r6   r7   r8   r9   r:   r   r   r   r;   r<   r=   r>   r?   r@   rA   rC   rB   r   rq   )r   rx   	num_filesrs   irJ   r   rF   rG   rH   rI   rK   rL   s                r    testWithLocalFiles&TestSeekAheadThread.testWithLocalFiles   s`   !FIJ 9
$+c!f$4#'!8  - oj	  9ax' ??$L;;=L\\^F NM}=I'(;(464001/0NN4(()!!##$(!
ii12ooG
ii " # 		I//
;	=>r#   r1   N)r+   r,   r-   r.   r/   r<   rM   r_   rt   r   r0   r1   r#   r    r   r   *   s'    K AF#QJ.@`)>r#   r   )r/   
__future__r   r   r   r   r6   r9   	six.movesr   r8   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_ARGUMENTr>   GsUtilUnitTestCaser   r1   r#   r    <module>r      sd    / & %  '  
 $  ? 3 3 ' ' , ( ! 2 ! 88 G>(55 G>r#   