test_macintelpower.py (2661B)
1 #!/usr/bin/env python 2 3 import time 4 from unittest import mock 5 6 import mozunit 7 8 9 def test_macintelpower_init(macintelpower_obj): 10 """Tests that the MacIntelPower object is correctly initialized.""" 11 assert macintelpower_obj.ipg_path 12 assert macintelpower_obj.ipg 13 assert macintelpower_obj._os == "darwin" 14 assert macintelpower_obj._cpu == "intel" 15 16 17 def test_macintelpower_measuring(macintelpower_obj): 18 """Tests that measurement initialization and finalization works 19 for the MacIntelPower object. 20 """ 21 assert not macintelpower_obj.start_time 22 assert not macintelpower_obj.ipg._running 23 assert not macintelpower_obj.ipg._output_files 24 macintelpower_obj.initialize_power_measurements() 25 26 # Check that initialization set start_time, and started the 27 # IPG measurer thread. 28 29 # Wait a bit for thread to start, then check it 30 timeout = 10 31 start = time.time() 32 while time.time() - start < timeout and not macintelpower_obj.ipg._running: 33 time.sleep(1) 34 35 assert macintelpower_obj.start_time 36 assert macintelpower_obj.ipg._running 37 38 test_data = {"power-usage": "data"} 39 40 def formatter_side_effect(*args, **kwargs): 41 return test_data 42 43 with mock.patch( 44 "mozpower.intel_power_gadget.IPGResultsHandler.clean_ipg_data" 45 ) as _: 46 with mock.patch( 47 "mozpower.intel_power_gadget.IPGResultsHandler." 48 "format_ipg_data_to_partial_perfherder" 49 ) as formatter: 50 formatter.side_effect = formatter_side_effect 51 52 macintelpower_obj.finalize_power_measurements(wait_interval=2, timeout=30) 53 54 # Check that finalization set the end_time, stopped the IPG measurement 55 # thread, added atleast one output file name, and initialized 56 # an IPGResultsHandler object 57 assert macintelpower_obj.end_time 58 assert not macintelpower_obj.ipg._running 59 assert macintelpower_obj.ipg._output_files 60 assert macintelpower_obj.ipg_results_handler 61 62 # Check that the IPGResultHandler's methods were 63 # called 64 macintelpower_obj.ipg_results_handler.clean_ipg_data.assert_called() 65 macintelpower_obj.ipg_results_handler.format_ipg_data_to_partial_perfherder.assert_called_once_with( # NOQA: E501 66 macintelpower_obj.end_time - macintelpower_obj.start_time, 67 "power-testing", 68 ) 69 70 # Make sure we can get the expected perfherder data 71 # after formatting 72 assert macintelpower_obj.get_perfherder_data() == test_data 73 74 75 if __name__ == "__main__": 76 mozunit.main()