<s> import logging <EOL> import threading <EOL> import unittest <EOL> from pykka import ActorRegistry , ThreadingActor <EOL> from tests import TestLogHandler <EOL> from tests . actor_test import ( <EOL> EarlyFailingActor , FailingOnFailureActor , LateFailingActor ) <EOL> class LoggingNullHandlerTest ( unittest . TestCase ) : <EOL> def test_null_handler_is_added_to_avoid_warnings ( self ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> handler_names = [ h . __class__ . __name__ for h in logger . handlers ] <EOL> self . assert_ ( '<STR_LIT>' in handler_names ) <EOL> class ActorLoggingTest ( object ) : <EOL> def setUp ( self ) : <EOL> self . on_stop_was_called = self . event_class ( ) <EOL> self . on_failure_was_called = self . event_class ( ) <EOL> self . actor_ref = self . AnActor . start ( <EOL> self . on_stop_was_called , self . on_failure_was_called ) <EOL> self . actor_proxy = self . actor_ref . proxy ( ) <EOL> self . log_handler = TestLogHandler ( logging . DEBUG ) <EOL> self . root_logger = logging . getLogger ( ) <EOL> self . root_logger . addHandler ( self . log_handler ) <EOL> def tearDown ( self ) : <EOL> self . log_handler . close ( ) <EOL> ActorRegistry . stop_all ( ) <EOL> def test_unexpected_messages_are_logged ( self ) : <EOL> self . actor_ref . ask ( { '<STR_LIT>' : '<STR_LIT:message>' } ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT>' ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . log_handler . messages [ '<STR_LIT>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % self . actor_ref , <EOL> log_record . getMessage ( ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ) <EOL> def test_exception_is_logged_when_returned_to_caller ( self ) : <EOL> try : <EOL> self . actor_proxy . raise_exception ( ) . get ( ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except Exception : <EOL> pass <EOL> self . log_handler . wait_for_message ( '<STR_LIT>' ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . log_handler . messages [ '<STR_LIT>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % self . actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> self . assertEqual ( Exception , log_record . exc_info [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , str ( log_record . exc_info [ <NUM_LIT:1> ] ) ) <EOL> def test_exception_is_logged_when_not_reply_requested ( self ) : <EOL> self . on_failure_was_called . clear ( ) <EOL> self . actor_ref . tell ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . on_failure_was_called . wait ( <NUM_LIT:5> ) <EOL> self . assertTrue ( self . on_failure_was_called . is_set ( ) ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT:error>' ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . log_handler . messages [ '<STR_LIT:error>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT:error>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % self . actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> self . assertEqual ( Exception , log_record . exc_info [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , str ( log_record . exc_info [ <NUM_LIT:1> ] ) ) <EOL> def test_base_exception_is_logged ( self ) : <EOL> self . log_handler . reset ( ) <EOL> self . on_stop_was_called . clear ( ) <EOL> self . actor_ref . tell ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . on_stop_was_called . wait ( <NUM_LIT:5> ) <EOL> self . assertTrue ( self . on_stop_was_called . is_set ( ) ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT>' , num_messages = <NUM_LIT:3> ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:3> , len ( self . log_handler . messages [ '<STR_LIT>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % self . actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> def test_exception_in_on_start_is_logged ( self ) : <EOL> self . log_handler . reset ( ) <EOL> start_event = self . event_class ( ) <EOL> actor_ref = self . EarlyFailingActor . start ( start_event ) <EOL> start_event . wait ( <NUM_LIT:5> ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT:error>' ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . log_handler . messages [ '<STR_LIT:error>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT:error>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> def test_exception_in_on_stop_is_logged ( self ) : <EOL> self . log_handler . reset ( ) <EOL> stop_event = self . event_class ( ) <EOL> actor_ref = self . LateFailingActor . start ( stop_event ) <EOL> stop_event . wait ( <NUM_LIT:5> ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT:error>' ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . log_handler . messages [ '<STR_LIT:error>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT:error>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> def test_exception_in_on_failure_is_logged ( self ) : <EOL> self . log_handler . reset ( ) <EOL> failure_event = self . event_class ( ) <EOL> actor_ref = self . FailingOnFailureActor . start ( failure_event ) <EOL> actor_ref . tell ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> failure_event . wait ( <NUM_LIT:5> ) <EOL> self . log_handler . wait_for_message ( '<STR_LIT:error>' , num_messages = <NUM_LIT:2> ) <EOL> with self . log_handler . lock : <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . log_handler . messages [ '<STR_LIT:error>' ] ) ) <EOL> log_record = self . log_handler . messages [ '<STR_LIT:error>' ] [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> '<STR_LIT>' % actor_ref , <EOL> log_record . getMessage ( ) ) <EOL> class AnActor ( object ) : <EOL> def __init__ ( self , on_stop_was_called , on_failure_was_called ) : <EOL> super ( AnActor , self ) . __init__ ( ) <EOL> self . on_stop_was_called = on_stop_was_called <EOL> self . on_failure_was_called = on_failure_was_called <EOL> def on_stop ( self ) : <EOL> self . on_stop_was_called . set ( ) <EOL> def on_failure ( self , exception_type , exception_value , traceback ) : <EOL> self . on_failure_was_called . set ( ) <EOL> def on_receive ( self , message ) : <EOL> if message . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> return self . raise_exception ( ) <EOL> elif message . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> raise BaseException ( ) <EOL> else : <EOL> super ( AnActor , self ) . on_receive ( message ) <EOL> def raise_exception ( self ) : <EOL> raise Exception ( '<STR_LIT:foo>' ) <EOL> def ConcreteActorLoggingTest ( actor_class , event_class ) : <EOL> class C ( ActorLoggingTest , unittest . TestCase ) : <EOL> class AnActor ( AnActor , actor_class ) : <EOL> pass <EOL> class EarlyFailingActor ( EarlyFailingActor , actor_class ) : <EOL> pass <EOL> class LateFailingActor ( LateFailingActor , actor_class ) : <EOL> pass <EOL> class FailingOnFailureActor ( FailingOnFailureActor , actor_class ) : <EOL> pass <EOL> C . event_class = event_class <EOL> C . __name__ = '<STR_LIT>' % actor_class . __name__ <EOL> return C <EOL> ThreadingActorLoggingTest = ConcreteActorLoggingTest ( <EOL> ThreadingActor , threading . Event ) <EOL> try : <EOL> import gevent . event <EOL> from pykka . gevent import GeventActor <EOL> GeventActorLoggingTest = ConcreteActorLoggingTest ( <EOL> GeventActor , gevent . event . Event ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> from pykka . eventlet import EventletActor , EventletEvent <EOL> EventletActorLoggingTest = ConcreteActorLoggingTest ( <EOL> EventletActor , EventletEvent ) <EOL> except ImportError : <EOL> pass </s>
