o
    fQ                     @   s   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 G dd dejZG dd	 d	ejZ	G d
d dejZ
G dd dejZdd ZedkrTejdd dS dS )    N)DummyOptions)DummyPConfig)DummyProcess)
DummyEventc                   @   sL   e Z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 )"EventSubscriptionNotificationTestsc                 C      ddl m} g |jd d < d S Nr   events
supervisorr
   	callbacksselfr
    r   O/var/www/html/venv/lib/python3.10/site-packages/supervisor/tests/test_events.pysetUp
      z(EventSubscriptionNotificationTests.setUpc                 C   r   r   r   r   r   r   r   tearDown   r   z+EventSubscriptionNotificationTests.tearDownc                 C   s,   ddl m} |d d  | |jdg d S Nr   r	   )NN)r   r
   	subscribeassertEqualr   r   r   r   r   test_subscribe   s   z1EventSubscriptionNotificationTests.test_subscribec                 C   s@   ddl m} g d|jd d < |dd | |jddg d S )Nr   r	   )   r   )   r      r   r   r   r   )r   r
   r   unsubscriber   r   r   r   r   test_unsubscribe   s   z3EventSubscriptionNotificationTests.test_unsubscribec                 C   s6   ddl m} dg|jd d < |  | |jg  d S r   )r   r
   r   clearr   r   r   r   r   
test_clear   s   z-EventSubscriptionNotificationTests.test_clearc                    sN   ddl m} g   fdd}t|fg|jd d < |t  |  dg d S )Nr   r	   c                         d d S Nr   appendeventLr   r   callback&      zEEventSubscriptionNotificationTests.test_notify_true.<locals>.callbackr   r   r
   r   r   notifyr   )r   r
   r*   r   r(   r   test_notify_true#   s   z3EventSubscriptionNotificationTests.test_notify_truec                    sZ   ddl m} g   fdd}G dd d}||fg|jd d < |t  |  g  d S )Nr   r	   c                    r"   r#   r$   r&   r(   r   r   r*   /   r+   zFEventSubscriptionNotificationTests.test_notify_false.<locals>.callbackc                   @      e Zd ZdS )zJEventSubscriptionNotificationTests.test_notify_false.<locals>.AnotherEventN__name__
__module____qualname__r   r   r   r   AnotherEvent1       r4   )r   r
   r   r-   r   r   )r   r
   r*   r4   r   r(   r   test_notify_false,   s   z4EventSubscriptionNotificationTests.test_notify_falsec                    s^   ddl m} g   fdd}G dd dt}t|fg|jd d < ||  |  dg d S )Nr   r	   c                    r"   r#   r$   r&   r(   r   r   r*   :   r+   zMEventSubscriptionNotificationTests.test_notify_via_subclass.<locals>.callbackc                   @   r/   )zSEventSubscriptionNotificationTests.test_notify_via_subclass.<locals>.ASubclassEventNr0   r   r   r   r   ASubclassEvent<   r5   r7   r   r,   )r   r
   r*   r7   r   r(   r   test_notify_via_subclass7   s   z;EventSubscriptionNotificationTests.test_notify_via_subclassN)r1   r2   r3   r   r   r   r   r!   r.   r6   r8   r   r   r   r   r   	   s    	r   c                   @   s   e Z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#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1S )2TestEventTypesc                 C   F   ddl m} |ddd}| |jd | |jd | |jd d S )Nr   ProcessLogEventr   r   r   )supervisor.eventsr<   r   processpiddata)r   r<   instr   r   r   test_ProcessLogEvent_attributesD   
   z.TestEventTypes.test_ProcessLogEvent_attributesc                 C   ,   ddl m} ddl m} | t|| d S )Nr   r;   Event)r=   r<   rF   
assertTrue
issubclass)r   r<   rF   r   r   r    test_ProcessLogEvent_inheritanceK   
   z/TestEventTypes.test_ProcessLogEvent_inheritancec                 C   T   ddl m} |ddd}| |jd | |jd | |jd | |jd d S )Nr   ProcessLogStdoutEventr   r   r   stdout)r=   rM   r   r>   r?   r@   channel)r   rM   rA   r   r   r   %test_ProcessLogStdoutEvent_attributesR      z4TestEventTypes.test_ProcessLogStdoutEvent_attributesc                 C   rD   )Nr   rL   r;   )r=   rM   r<   rG   rH   )r   rM   r<   r   r   r   &test_ProcessLogStdoutEvent_inheritanceZ   rJ   z5TestEventTypes.test_ProcessLogStdoutEvent_inheritancec                 C   rK   )Nr   ProcessLogStderrEventr   r   r   stderr)r=   rT   r   r>   r?   r@   rO   )r   rT   rA   r   r   r   %test_ProcessLogStderrEvent_attributesa   rQ   z4TestEventTypes.test_ProcessLogStderrEvent_attributesc                 C   rD   )Nr   rS   r;   )r=   rT   r<   rG   rH   )r   rT   r<   r   r   r   &test_ProcessLogStderrEvent_inheritancei   rJ   z5TestEventTypes.test_ProcessLogStderrEvent_inheritancec                 C   r:   )Nr   ProcessCommunicationEventr   r   r   )r=   rY   r   r>   r?   r@   )r   rY   rA   r   r   r   )test_ProcessCommunicationEvent_attributesp   rC   z8TestEventTypes.test_ProcessCommunicationEvent_attributesc                 C   rD   )Nr   rX   rE   )r=   rY   rF   rG   rH   )r   rY   rF   r   r   r   *test_ProcessCommunicationEvent_inheritancew   rJ   z9TestEventTypes.test_ProcessCommunicationEvent_inheritancec                 C   rK   )Nr   ProcessCommunicationStdoutEventr   r   r   rN   )r=   r]   r   r>   r?   r@   rO   )r   r]   rA   r   r   r   /test_ProcessCommunicationStdoutEvent_attributes~   rQ   z>TestEventTypes.test_ProcessCommunicationStdoutEvent_attributesc                 C   rD   )Nr   r\   rX   )r=   r]   rY   rG   rH   )r   r]   rY   r   r   r   0test_ProcessCommunicationStdoutEvent_inheritance      z?TestEventTypes.test_ProcessCommunicationStdoutEvent_inheritancec                 C   rK   )Nr   ProcessCommunicationStderrEventr   r   r   rU   )r=   rb   r   r>   r?   r@   rO   )r   rb   rA   r   r   r   /test_ProcessCommunicationStderrEvent_attributes   rQ   z>TestEventTypes.test_ProcessCommunicationStderrEvent_attributesc                 C   rD   )Nr   ra   rX   )r=   rb   rY   rG   rH   )r   rb   rY   r   r   r   0test_ProcessCommunicationStderrEvent_inheritance   r`   z?TestEventTypes.test_ProcessCommunicationStderrEvent_inheritancec                 C   s6   ddl m} |dd}| |jd | |jd d S )Nr   RemoteCommunicationEventr   r   )r=   rf   r   typer@   )r   rf   rA   r   r   r   (test_RemoteCommunicationEvent_attributes   s   
z7TestEventTypes.test_RemoteCommunicationEvent_attributesc                 C   rD   )Nr   re   rE   )r=   rf   rF   rG   rH   )r   rf   rF   r   r   r   )test_RemoteCommunicationEvent_inheritance   rJ   z8TestEventTypes.test_RemoteCommunicationEvent_inheritancec                 C   sX   ddl m} t }t|ddd}t|}t }|||}| |j| | |j| d S )Nr   EventRejectedEventprocess1/bin/process1)	r=   rk   r   r   r   r   r   r>   r'   )r   rk   optionspconfig1r>   rejected_eventr'   r   r   r   "test_EventRejectedEvent_attributes   s   
z1TestEventTypes.test_EventRejectedEvent_attributesc                 C   rD   )Nr   rj   rE   )r=   rk   rF   assertFalserH   )r   rk   rF   r   r   r   3test_EventRejectedEvent_does_not_inherit_from_event   rJ   zBTestEventTypes.test_EventRejectedEvent_does_not_inherit_from_eventc                 C   s0   ddl m} |j|j|jfD ]}| | qd S r   )r   r
   SupervisorStateChangeEventSupervisorRunningEventSupervisorStoppingEvent$_test_one_SupervisorStateChangeEventr   r
   klassr   r   r   $test_all_SupervisorStateChangeEvents   s   z3TestEventTypes.test_all_SupervisorStateChangeEventsc                 C   s    ddl m} | t|| d S )Nr   )rt   )r=   rt   rG   rH   )r   ry   rt   r   r   r   rw      s   z3TestEventTypes._test_one_SupervisorStateChangeEventc              	   C   sH   ddl m} |j|j|j|j|j|j|j|j	|j
f	D ]}| | qd S r   )r   r
   ProcessStateEventProcessStateStoppedEventProcessStateExitedEventProcessStateFatalEventProcessStateBackoffEventProcessStateRunningEventProcessStateUnknownEventProcessStateStoppingEventProcessStateStartingEvent_test_one_ProcessStateEventrx   r   r   r   test_all_ProcessStateEvents   s   z*TestEventTypes.test_all_ProcessStateEventsc                 C   s   ddl m} ddlm} | t|| t }t|ddd}t|}|||j	}| 
|j| | 
|j|j	 | 
|jd d S )Nr   ProcessStates)r{   rl   rm   T)supervisor.statesr   r=   r{   rG   rH   r   r   r   STARTINGr   r>   
from_stateexpected)r   ry   r   r{   rn   ro   r>   rA   r   r   r   r      s   z*TestEventTypes._test_one_ProcessStateEventc                 C   s4   ddl m} |j|j|j|jfD ]}| | qd S r   )r   r
   	TickEvent
Tick5EventTick60EventTick3600Event_test_one_TickEventrx   r   r   r   test_all_TickEvents   s   z"TestEventTypes.test_all_TickEventsc                 C   sF   ddl m} | t|| |dd}| |jd | |jd d S )Nr   )r   r   r   )r=   r   rG   rH   r   whensupervisord)r   ry   r   rA   r   r   r   r      s
   
z"TestEventTypes._test_one_TickEventc                 C   &   ddl m} |d}| |jd d S )Nr   ProcessGroupAddedEvent	myprocess)r=   r   r   group)r   r   rA   r   r   r   &test_ProcessGroupAddedEvent_attributes      z5TestEventTypes.test_ProcessGroupAddedEvent_attributesc                 C   r   )Nr   ProcessGroupRemovedEventr   )r=   r   r   r   )r   r   rA   r   r   r   (test_ProcessGroupRemovedEvent_attributes   r   z7TestEventTypes.test_ProcessGroupRemovedEvent_attributesN)r1   r2   r3   rB   rI   rP   rR   rV   rW   rZ   r[   r^   r_   rc   rd   rh   ri   rq   rs   rz   rw   r   r   r   r   r   r   r   r   r   r   r9   C   s2    
	
r9   c                   @   s   e Z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S ) TestSerializationsc                 C   sv   | d}|d }d}i }t|dkr|d }|r7ztdd |  D }W ||fS  ty6   td| w ||fS )N
r    r   c                 S   s   g | ]}| d dqS ):r   )split).0xr   r   r   
<listcomp>
  s    z3TestSerializations._deserialize.<locals>.<listcomp>z'headerdata %r could not be deserialized)r   lendict
ValueErrorAssertionError)r   serializationr@   
headerdatapayloadheadersr   r   r   _deserialize  s$   

zTestSerializations._deserializec                       t  }t|ddd t }ddlm} G  fddd}||_||dd}| | \}}| |d	 d| | |d
 d| | |d d| | |d d S )Nrl   rm   r   rL   c                          e Zd Z ZdS )z=TestSerializations.test_plog_stdout_event.<locals>.DummyGroupNr1   r2   r3   configr   ro   r   r   
DummyGroup      r   r   yoprocessname	groupnamer?   1)	r   r   r   r=   rM   r   r   r   r   )r   rn   rl   rM   r   r'   r   r   r   r   r   test_plog_stdout_event     z)TestSerializations.test_plog_stdout_eventc                    r   )Nrl   rm   r   rS   c                       r   )z=TestSerializations.test_plog_stderr_event.<locals>.DummyGroupNr   r   r   r   r   r   %  r   r   r   r   r   r   r?   r   )	r   r   r   r=   rT   r   r   r   r   )r   rn   rl   rT   r   r'   r   r   r   r   r   test_plog_stderr_event   r   z)TestSerializations.test_plog_stderr_eventc                    r   )Nrl   rm   r   r\   c                       r   )z>TestSerializations.test_pcomm_stdout_event.<locals>.DummyGroupNr   r   r   r   r   r   4  r   r   r   r   r   r   r?   r   )	r   r   r   r=   r]   r   r   r   r   )r   rn   rl   r]   r   r'   r   r   r   r   r   test_pcomm_stdout_event/  r   z*TestSerializations.test_pcomm_stdout_eventc                    s   t  }t|ddd t }G  fddd}||_ddlm} ||dd}| | \}}| |d	 d| | |d
 d| | |d d| | |d d S )Nrl   rm   c                       r   )z>TestSerializations.test_pcomm_stderr_event.<locals>.DummyGroupNr   r   r   r   r   r   B  r   r   r   ra   r   r   r   r   r?   r   )	r   r   r   r   r=   rb   r   r   r   )r   rn   rl   r   rb   r'   r   r   r   r   r   test_pcomm_stderr_event>  s   z*TestSerializations.test_pcomm_stderr_eventc                 C   sJ   ddl m} |dd}| | \}}| |d d| | |d d S )Nr   re   foobarrg   )r=   rf   r   r   r   )r   rf   r'   r   r   r   r   r   test_remote_comm_eventM  s
   
z)TestSerializations.test_remote_comm_eventc                 C   F   ddl m} |d}| | \}}| |d d | |d d S )Nr   r   r   r   r   )r=   r   r   r   r   )r   r   r'   r   r   r   r   r   test_process_group_added_eventT  
   z1TestSerializations.test_process_group_added_eventc                 C   r   )Nr   r   r   r   r   )r=   r   r   r   r   )r   r   r'   r   r   r   r   r    test_process_group_removed_event[  r   z3TestSerializations.test_process_group_removed_eventc           
         s   ddl m} ddlm} |j|jfD ]Q}t }t|ddd G  fddd}t }||_	|||j
}| | \}}	| t|d | |d	 d | |d
 d | |d d | |	d qd S )Nr   r   r	   rl   rm   c                       r   )zUTestSerializations.test_process_state_events_without_extra_values.<locals>.DummyGroupNr   r   r   r   r   r   l  r   r   r   r   r   r   r   r   )r   r   r   r
   r~   r   r   r   r   r   r   r   r   r   r   
r   r   r
   ry   rn   r   rl   r'   r   r   r   r   r   .test_process_state_events_without_extra_valuesb  s(   zATestSerializations.test_process_state_events_without_extra_valuesc           
         s   ddl m} ddlm} |j|j|jfD ]\}t }t|ddd G  fddd}t	 }||_
d|_|||j}| | \}}	| t|d	 | |d
 d | |d d | |d d | |d d | |	d qd S )Nr   r   r	   rl   rm   c                       r   )zITestSerializations.test_process_state_events_with_pid.<locals>.DummyGroupNr   r   r   r   r   r     r   r   r      r   r   r   r   r?   r   r   )r   r   r   r
   r   r|   r   r   r   r   r   r?   r   r   r   r   r   r   r   r   r   "test_process_state_events_with_pidx  s.   z5TestSerializations.test_process_state_events_with_pidc           
         sD  ddl m} ddlm} |j|jfD ]}t }t|ddd G  fddd}t }||_	|||j
}| | \}}	| t|d | |d	 d | |d
 d | |d d | |d d | |	d d|_|||j
}| | \}}	| |d d d|_|||j
}| | \}}	| |d d qd S )Nr   r   r	   rl   rm   c                       r   )zUTestSerializations.test_process_state_events_starting_and_backoff.<locals>.DummyGroupNr   r   r   r   r   r     r   r   r   r   r   r   r   tries0r   r   r   r   2)r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   backoffr   r   r   r   .test_process_state_events_starting_and_backoff  s:   zATestSerializations.test_process_state_events_starting_and_backoffc           	         s   ddl m} ddlm} t }t|ddd t }G  fddd}||_d|_|j	||j
d	d
}| | \}}| t|d | |d d | |d d | |d d | |d d | |d d | |d d S )Nr   r	   r   rl   rm   c                       r   )zOTestSerializations.test_process_state_exited_event_expected.<locals>.DummyGroupNr   r   r   r   r   r     r   r   r   Tr      r   r   r?   r   r   r   r   r   r   r
   r   r   r   r   r   r   r?   r}   r   r   r   r   r   	r   r
   r   rn   rl   r   r'   r   r   r   r   r   (test_process_state_exited_event_expected  (   z;TestSerializations.test_process_state_exited_event_expectedc           	         s   ddl m} ddlm} t }t|ddd t }G  fddd}||_d|_|j	||j
d	d
}| | \}}| t|d | |d d | |d d | |d d | |d d | |d d | |d d S )Nr   r	   r   rl   rm   c                       r   )zQTestSerializations.test_process_state_exited_event_unexpected.<locals>.DummyGroupNr   r   r   r   r   r     r   r   r   Fr   r   r   r   r?   r   r   r   r   r   r   r   r   r   r   r   *test_process_state_exited_event_unexpected  r   z=TestSerializations.test_process_state_exited_event_unexpectedc                 C   sB   ddl m} | }| | \}}| |i  | |d d S )Nr   r	   r   )r   r
   ru   r   r   r   )r   r
   r'   r   r   r   r   r   test_supervisor_sc_event  s
   z+TestSerializations.test_supervisor_sc_eventc                 C   s^   ddl m} |j|j|jfD ]}|dd}| | \}}| |ddi | |d qd S )Nr   r	   r   r   r   r   r   )r   r
   r   r   r   r   r   r   )r   r
   ry   r'   r   r   r   r   r   test_tick_events  s   
z#TestSerializations.test_tick_eventsN)r1   r2   r3   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s     r   c                   @   s   e Zd Zdd Zdd ZdS )TestUtilityFunctionsc                 C   s8   ddl m} |jj D ]\}}| ||| qd S r   )r   r
   
EventTypes__dict__itemsr   getEventNameByType)r   r
   namevaluer   r   r   test_getEventNameByType  s   z,TestUtilityFunctions.test_getEventNameByTypec                 C   sd   ddl m} | t|jd G dd d|j}z|d| | |jj|u  W |j`d S |j`w )Nr   r	   FOOc                   @   r/   )z4TestUtilityFunctions.test_register.<locals>.FooEventNr0   r   r   r   r   FooEvent  r5   r   )	r   r
   rr   hasattrr   rF   registerrG   r   )r   r
   r   r   r   r   test_register  s   z"TestUtilityFunctions.test_registerN)r1   r2   r3   r   r   r   r   r   r   r     s    r   c                   C   s   t tjt S )N)unittestfindTestCasessysmodulesr1   r   r   r   r   
test_suite   s   r   __main__)defaultTest)r   r   supervisor.tests.baser   r   r   r   TestCaser   r9   r   r   r   r1   mainr   r   r   r   <module>   s     : > p