<s> import collections <EOL> import logging <EOL> import threading <EOL> class TestLogHandler ( logging . Handler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . lock = threading . RLock ( ) <EOL> with self . lock : <EOL> self . events = collections . defaultdict ( threading . Event ) <EOL> self . messages = { } <EOL> self . reset ( ) <EOL> logging . Handler . __init__ ( self , * args , ** kwargs ) <EOL> def emit ( self , record ) : <EOL> with self . lock : <EOL> level = record . levelname . lower ( ) <EOL> self . messages [ level ] . append ( record ) <EOL> self . events [ level ] . set ( ) <EOL> def reset ( self ) : <EOL> with self . lock : <EOL> for level in ( '<STR_LIT>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT:error>' , '<STR_LIT>' ) : <EOL> self . events [ level ] . clear ( ) <EOL> self . messages [ level ] = [ ] <EOL> def wait_for_message ( self , level , num_messages = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> with self . lock : <EOL> if len ( self . messages [ level ] ) >= num_messages : <EOL> return <EOL> self . events [ level ] . clear ( ) <EOL> self . events [ level ] . wait ( <NUM_LIT:5> ) </s>
