summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozpower/tests/test_macintelpower.py
blob: 2332c94c3e27ca691c11ff664d1fbece3f86a33a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python

import time
from unittest import mock

import mozunit


def test_macintelpower_init(macintelpower_obj):
    """Tests that the MacIntelPower object is correctly initialized."""
    assert macintelpower_obj.ipg_path
    assert macintelpower_obj.ipg
    assert macintelpower_obj._os == "darwin"
    assert macintelpower_obj._cpu == "intel"


def test_macintelpower_measuring(macintelpower_obj):
    """Tests that measurement initialization and finalization works
    for the MacIntelPower object.
    """
    assert not macintelpower_obj.start_time
    assert not macintelpower_obj.ipg._running
    assert not macintelpower_obj.ipg._output_files
    macintelpower_obj.initialize_power_measurements()

    # Check that initialization set start_time, and started the
    # IPG measurer thread.

    # Wait a bit for thread to start, then check it
    timeout = 10
    start = time.time()
    while time.time() - start < timeout and not macintelpower_obj.ipg._running:
        time.sleep(1)

    assert macintelpower_obj.start_time
    assert macintelpower_obj.ipg._running

    test_data = {"power-usage": "data"}

    def formatter_side_effect(*args, **kwargs):
        return test_data

    with mock.patch(
        "mozpower.intel_power_gadget.IPGResultsHandler.clean_ipg_data"
    ) as _:
        with mock.patch(
            "mozpower.intel_power_gadget.IPGResultsHandler."
            "format_ipg_data_to_partial_perfherder"
        ) as formatter:
            formatter.side_effect = formatter_side_effect

            macintelpower_obj.finalize_power_measurements(wait_interval=2, timeout=30)

            # Check that finalization set the end_time, stopped the IPG measurement
            # thread, added atleast one output file name, and initialized
            # an IPGResultsHandler object
            assert macintelpower_obj.end_time
            assert not macintelpower_obj.ipg._running
            assert macintelpower_obj.ipg._output_files
            assert macintelpower_obj.ipg_results_handler

            # Check that the IPGResultHandler's methods were
            # called
            macintelpower_obj.ipg_results_handler.clean_ipg_data.assert_called()
            macintelpower_obj.ipg_results_handler.format_ipg_data_to_partial_perfherder.assert_called_once_with(  # NOQA: E501
                macintelpower_obj.end_time - macintelpower_obj.start_time,
                "power-testing",
            )

            # Make sure we can get the expected perfherder data
            # after formatting
            assert macintelpower_obj.get_perfherder_data() == test_data


if __name__ == "__main__":
    mozunit.main()