<s> from unittest import TestCase <EOL> from nose . tools import assert_equal , raises , assert_true , assert_false <EOL> from wlauto import Instrument <EOL> from wlauto . core import signal , instrumentation <EOL> from wlauto . instrumentation import instrument_is_installed , instrument_is_enabled , clear_instrumentation <EOL> class MockInstrument ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> self . before = <NUM_LIT:0> <EOL> self . after = <NUM_LIT:0> <EOL> def before_workload_execution ( self , context ) : <EOL> self . before += <NUM_LIT:1> <EOL> def after_workload_execution ( self , context ) : <EOL> self . after += <NUM_LIT:1> <EOL> class MockInstrument2 ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> self . before = <NUM_LIT:0> <EOL> self . after = <NUM_LIT:0> <EOL> self . result = <NUM_LIT:0> <EOL> def before_workload_execution ( self , context ) : <EOL> self . before += <NUM_LIT:1> <EOL> def after_workload_execution ( self , context ) : <EOL> self . after += <NUM_LIT:1> <EOL> def after_workload_result_update ( self , context ) : <EOL> self . result += <NUM_LIT:1> <EOL> class MockInstrument3 ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> def slow_before_workload_execution ( self , context ) : <EOL> global counter <EOL> counter += <NUM_LIT:1> <EOL> class MockInstrument4 ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> def slow_before_first_iteration_boot ( self , context ) : <EOL> global counter <EOL> counter = <NUM_LIT:4> <EOL> class MockInstrument5 ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> def fast_before_first_iteration_boot ( self , context ) : <EOL> global counter <EOL> counter += <NUM_LIT:2> <EOL> class MockInstrument6 ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> Instrument . __init__ ( self , None ) <EOL> def before_first_iteration_boot ( self , context ) : <EOL> global counter <EOL> counter *= <NUM_LIT:10> <EOL> class BadInstrument ( Instrument ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> pass <EOL> def teardown ( self ) : <EOL> pass <EOL> counter = <NUM_LIT:0> <EOL> class InstrumentationTest ( TestCase ) : <EOL> def tearDown ( self ) : <EOL> clear_instrumentation ( ) <EOL> def test_install ( self ) : <EOL> instrument = _instantiate ( MockInstrument ) <EOL> instrument2 = _instantiate ( MockInstrument2 ) <EOL> instrumentation . install ( instrument ) <EOL> instrumentation . install ( instrument2 ) <EOL> signal . send ( signal . BEFORE_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_RESULT_UPDATE , self , context = None ) <EOL> assert_equal ( instrument . before , <NUM_LIT:1> ) <EOL> assert_equal ( instrument . after , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . before , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . after , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . result , <NUM_LIT:1> ) <EOL> def test_enable_disable ( self ) : <EOL> instrument = _instantiate ( MockInstrument ) <EOL> instrument2 = _instantiate ( MockInstrument2 ) <EOL> instrumentation . install ( instrument ) <EOL> instrumentation . install ( instrument2 ) <EOL> instrumentation . disable_all ( ) <EOL> signal . send ( signal . BEFORE_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_RESULT_UPDATE , self , context = None ) <EOL> assert_equal ( instrument . before , <NUM_LIT:0> ) <EOL> assert_equal ( instrument . after , <NUM_LIT:0> ) <EOL> assert_equal ( instrument2 . before , <NUM_LIT:0> ) <EOL> assert_equal ( instrument2 . after , <NUM_LIT:0> ) <EOL> assert_equal ( instrument2 . result , <NUM_LIT:0> ) <EOL> instrumentation . enable ( instrument ) <EOL> signal . send ( signal . BEFORE_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_RESULT_UPDATE , self , context = None ) <EOL> assert_equal ( instrument . before , <NUM_LIT:1> ) <EOL> assert_equal ( instrument . after , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . before , <NUM_LIT:0> ) <EOL> assert_equal ( instrument2 . after , <NUM_LIT:0> ) <EOL> assert_equal ( instrument2 . result , <NUM_LIT:0> ) <EOL> instrumentation . enable_all ( ) <EOL> signal . send ( signal . BEFORE_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_EXECUTION , self , context = None ) <EOL> signal . send ( signal . AFTER_WORKLOAD_RESULT_UPDATE , self , context = None ) <EOL> assert_equal ( instrument . before , <NUM_LIT:2> ) <EOL> assert_equal ( instrument . after , <NUM_LIT:2> ) <EOL> assert_equal ( instrument2 . before , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . after , <NUM_LIT:1> ) <EOL> assert_equal ( instrument2 . result , <NUM_LIT:1> ) <EOL> def test_check_enabled ( self ) : <EOL> instrument = _instantiate ( MockInstrument ) <EOL> instrumentation . install ( instrument ) <EOL> instrumentation . enable ( instrument ) <EOL> assert_true ( instrument_is_enabled ( instrument ) ) <EOL> assert_true ( instrument_is_enabled ( instrument . name ) ) <EOL> instrumentation . disable ( instrument ) <EOL> assert_false ( instrument_is_enabled ( instrument ) ) <EOL> assert_false ( instrument_is_enabled ( instrument . name ) ) <EOL> def test_local_instrument ( self ) : <EOL> global counter <EOL> counter = <NUM_LIT:0> <EOL> self . install_local_instrument ( ) <EOL> signal . send ( signal . BEFORE_WORKLOAD_EXECUTION , self , context = None ) <EOL> assert_equal ( counter , <NUM_LIT:1> ) <EOL> def test_priority_prefix_instrument ( self ) : <EOL> global counter <EOL> counter = <NUM_LIT:0> <EOL> instrument1 = _instantiate ( MockInstrument4 ) <EOL> instrument2 = _instantiate ( MockInstrument5 ) <EOL> instrument3 = _instantiate ( MockInstrument6 ) <EOL> instrumentation . install ( instrument1 ) <EOL> instrumentation . install ( instrument2 ) <EOL> instrumentation . install ( instrument3 ) <EOL> signal . send ( signal . BEFORE_FIRST_ITERATION_BOOT , self , context = None ) <EOL> assert_equal ( counter , <NUM_LIT> ) <EOL> @ raises ( ValueError ) <EOL> def test_bad_argspec ( self ) : <EOL> instrument = _instantiate ( BadInstrument ) <EOL> instrumentation . install ( instrument ) <EOL> def test_check_installed ( self ) : <EOL> instrumentation . install ( _instantiate ( MockInstrument ) ) <EOL> assert_true ( instrument_is_installed ( '<STR_LIT>' ) ) <EOL> assert_true ( instrument_is_installed ( MockInstrument ) ) <EOL> assert_false ( instrument_is_installed ( MockInstrument2 ) ) <EOL> def install_local_instrument ( self ) : <EOL> instrument = _instantiate ( MockInstrument3 ) <EOL> instrumentation . install ( instrument ) <EOL> @ raises ( ValueError ) <EOL> def test_duplicate_install ( self ) : <EOL> instrument = _instantiate ( MockInstrument ) <EOL> instrument2 = _instantiate ( MockInstrument ) <EOL> instrumentation . install ( instrument ) <EOL> instrumentation . install ( instrument2 ) <EOL> def _instantiate ( cls ) : <EOL> return cls ( ) </s>
