File: test_highwatermark_command.py

package info (click to toggle)
python-memray 1.17.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,396 kB
  • sloc: python: 28,451; ansic: 16,507; sh: 10,586; cpp: 8,494; javascript: 1,474; makefile: 822; awk: 12
file content (247 lines) | stat: -rw-r--r-- 8,633 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import os
import sys
from pathlib import Path
from unittest.mock import ANY
from unittest.mock import MagicMock
from unittest.mock import Mock
from unittest.mock import call
from unittest.mock import patch

import pytest

from memray._errors import MemrayCommandError
from memray._memray import FileFormat
from memray.commands.common import HighWatermarkCommand


class TestFilenameValidation:
    def test_fails_when_results_does_not_exist(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"

        # WHEN / THEN
        with pytest.raises(MemrayCommandError, match="No such file"):
            command.validate_filenames(
                output=None,
                results=os.fspath(results),
            )

    def test_generates_output_name_when_none(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()

        # WHEN
        results_file, output_file = command.validate_filenames(
            output=None,
            results=os.fspath(results),
        )

        # THEN
        assert results_file == results
        assert output_file == tmp_path / "memray-reporter-results.html"

    def test_uses_determine_output_filename_when_output_is_none(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()
        command.determine_output_filename = MagicMock(return_value="patched.html")

        # WHEN
        results_file, output_file = command.validate_filenames(
            output=None,
            results=os.fspath(results),
        )

        # THEN
        assert results_file == results
        assert output_file == Path("patched.html")
        command.determine_output_filename.assert_called_once_with(results_file)

    def test_uses_output_name_as_given(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        output = tmp_path / "output.html"
        results = tmp_path / "results.bin"
        results.touch()

        # WHEN
        results_file, output_file = command.validate_filenames(
            output=os.fspath(output),
            results=os.fspath(results),
        )

        # THEN
        assert results_file == results
        assert output_file == output

    def test_fails_when_fallback_output_exists(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()
        (tmp_path / "memray-reporter-results.html").touch()

        # WHEN / THEN
        with pytest.raises(MemrayCommandError, match="File already exists"):
            command.validate_filenames(
                output=None,
                results=os.fspath(results),
            )

    def test_succeeds_when_fallback_output_exists_but_can_overwrite(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()
        (tmp_path / "memray-reporter-results.html").touch()

        # WHEN / THEN
        results_file, output_file = command.validate_filenames(
            output=None,
            results=os.fspath(results),
            overwrite=True,
        )

        # THEN
        assert results_file == results
        assert output_file is not None

    def test_fails_when_given_output_exists(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()
        output = tmp_path / "output.html"
        output.touch()

        # WHEN / THEN
        with pytest.raises(MemrayCommandError, match="File already exists"):
            command.validate_filenames(
                output=output,
                results=os.fspath(results),
            )

    def test_succeeds_when_given_output_exists_but_can_overwrite(self, tmp_path):
        # GIVEN
        command = HighWatermarkCommand(Mock(), reporter_name="reporter")
        results = tmp_path / "results.bin"
        results.touch()
        output = tmp_path / "output.html"
        output.touch()

        # WHEN / THEN
        results_file, output_file = command.validate_filenames(
            output=output,
            results=os.fspath(results),
            overwrite=True,
        )

        # THEN
        assert results_file == results
        assert output_file == output


class TestReportGeneration:
    @pytest.mark.parametrize("merge_threads", [True, False])
    def test_tracker_and_reporter_interactions_for_peak(self, tmp_path, merge_threads):
        # GIVEN
        reporter_factory_mock = Mock()
        command = HighWatermarkCommand(reporter_factory_mock, reporter_name="reporter")
        result_path = tmp_path / "results.bin"
        output_file = tmp_path / "output.txt"

        # WHEN
        with patch("memray.commands.common.FileReader") as reader_mock:
            command.write_report(
                result_path=result_path,
                output_file=output_file,
                show_memory_leaks=False,
                temporary_allocation_threshold=-1,
                merge_threads=merge_threads,
            )

        # THEN
        calls = [
            call(os.fspath(result_path), report_progress=True),
            call().metadata.has_native_traces.__bool__(),
            call().metadata.file_format.__eq__(FileFormat.ALL_ALLOCATIONS)
            if sys.version_info >= (3, 8, 0)
            else ANY,
            call().get_high_watermark_allocation_records(merge_threads=merge_threads),
            call().get_memory_snapshots(),
        ]
        reader_mock.assert_has_calls(calls)

        reporter_factory_mock.assert_called_once()
        reporter_factory_mock().render.assert_called_once()

    @pytest.mark.parametrize("merge_threads", [True, False])
    def test_tracker_and_reporter_interactions_for_leak(self, tmp_path, merge_threads):
        # GIVEN
        reporter_factory_mock = Mock()
        command = HighWatermarkCommand(reporter_factory_mock, reporter_name="reporter")
        result_path = tmp_path / "results.bin"
        output_file = tmp_path / "output.txt"

        # WHEN
        with patch("memray.commands.common.FileReader") as reader_mock:
            command.write_report(
                result_path=result_path,
                output_file=output_file,
                show_memory_leaks=True,
                temporary_allocation_threshold=-1,
                merge_threads=merge_threads,
            )

        # THEN
        calls = [
            call(os.fspath(result_path), report_progress=True),
            call().metadata.has_native_traces.__bool__(),
            call().metadata.file_format.__eq__(FileFormat.ALL_ALLOCATIONS)
            if sys.version_info >= (3, 8, 0)
            else ANY,
            call().get_leaked_allocation_records(merge_threads=merge_threads),
            call().get_memory_snapshots(),
        ]
        reader_mock.assert_has_calls(calls)

        reporter_factory_mock.assert_called_once()
        reporter_factory_mock().render.assert_called_once()

    @pytest.mark.parametrize("merge_threads", [True, False])
    def test_tracker_and_reporter_interactions_for_temporary_allocations(
        self, tmp_path, merge_threads
    ):
        # GIVEN
        reporter_factory_mock = Mock()
        command = HighWatermarkCommand(reporter_factory_mock, reporter_name="reporter")
        result_path = tmp_path / "results.bin"
        output_file = tmp_path / "output.txt"

        # WHEN
        with patch("memray.commands.common.FileReader") as reader_mock:
            command.write_report(
                result_path=result_path,
                output_file=output_file,
                show_memory_leaks=False,
                temporary_allocation_threshold=3,
                merge_threads=merge_threads,
            )

        # THEN
        calls = [
            call(os.fspath(result_path), report_progress=True),
            call().metadata.has_native_traces.__bool__(),
            call().get_temporary_allocation_records(
                threshold=3, merge_threads=merge_threads
            ),
            call().get_memory_snapshots(),
        ]
        reader_mock.assert_has_calls(calls)

        reporter_factory_mock.assert_called_once()
        reporter_factory_mock().render.assert_called_once()