File: test_mmap.py

package info (click to toggle)
python3.14 3.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,680 kB
  • sloc: python: 751,968; ansic: 717,163; xml: 31,250; sh: 5,989; cpp: 4,063; makefile: 1,995; objc: 787; lisp: 502; javascript: 136; asm: 75; csh: 12
file content (270 lines) | stat: -rw-r--r-- 9,535 bytes parent folder | download | duplicates (2)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import unittest

from test.support import import_helper, threading_helper
from test.support.threading_helper import run_concurrently

import os
import string
import tempfile
import threading

from collections import Counter

mmap = import_helper.import_module("mmap")

NTHREADS = 10
ANONYMOUS_MEM = -1


@threading_helper.requires_working_threading()
class MmapTests(unittest.TestCase):
    def test_read_and_read_byte(self):
        ascii_uppercase = string.ascii_uppercase.encode()
        # Choose a total mmap size that evenly divides across threads and the
        # read pattern (3 bytes per loop).
        mmap_size = 3 * NTHREADS * len(ascii_uppercase)
        num_bytes_to_read_per_thread = mmap_size // NTHREADS
        bytes_read_from_mmap = []

        def read(mm_obj):
            nread = 0
            while nread < num_bytes_to_read_per_thread:
                b = mm_obj.read_byte()
                bytes_read_from_mmap.append(b)
                b = mm_obj.read(2)
                bytes_read_from_mmap.extend(b)
                nread += 3

        with mmap.mmap(ANONYMOUS_MEM, mmap_size) as mm_obj:
            for i in range(mmap_size // len(ascii_uppercase)):
                mm_obj.write(ascii_uppercase)

            mm_obj.seek(0)
            run_concurrently(
                worker_func=read,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

        self.assertEqual(len(bytes_read_from_mmap), mmap_size)
        # Count each letter/byte to verify read correctness
        counter = Counter(bytes_read_from_mmap)
        self.assertEqual(len(counter), len(ascii_uppercase))
        # Each letter/byte should be read (3 * NTHREADS) times
        for letter in ascii_uppercase:
            self.assertEqual(counter[letter], 3 * NTHREADS)

    def test_readline(self):
        num_lines = 1000
        lines_read_from_mmap = []
        expected_lines = []

        def readline(mm_obj):
            for i in range(num_lines // NTHREADS):
                line = mm_obj.readline()
                lines_read_from_mmap.append(line)

        # Allocate mmap enough for num_lines (max line 5 bytes including NL)
        with mmap.mmap(ANONYMOUS_MEM, num_lines * 5) as mm_obj:
            for i in range(num_lines):
                line = b"%d\n" % i
                mm_obj.write(line)
                expected_lines.append(line)

            mm_obj.seek(0)
            run_concurrently(
                worker_func=readline,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

        self.assertEqual(len(lines_read_from_mmap), num_lines)
        # Every line should be read once by threads; order is non-deterministic
        # Sort numerically by integer value
        lines_read_from_mmap.sort(key=lambda x: int(x))
        self.assertEqual(lines_read_from_mmap, expected_lines)

    def test_write_and_write_byte(self):
        thread_letters = list(string.ascii_uppercase)
        self.assertLessEqual(NTHREADS, len(thread_letters))
        per_thread_write_loop = 100

        def write(mm_obj):
            # Each thread picks a unique letter to write
            thread_letter = thread_letters.pop(0)
            thread_bytes = (thread_letter * 2).encode()
            for _ in range(per_thread_write_loop):
                mm_obj.write_byte(thread_bytes[0])
                mm_obj.write(thread_bytes)

        with mmap.mmap(
            ANONYMOUS_MEM, per_thread_write_loop * 3 * NTHREADS
        ) as mm_obj:
            run_concurrently(
                worker_func=write,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )
            mm_obj.seek(0)
            data = mm_obj.read()
            self.assertEqual(len(data), NTHREADS * per_thread_write_loop * 3)
            counter = Counter(data)
            self.assertEqual(len(counter), NTHREADS)
            # Each thread letter should be written `per_thread_write_loop` * 3
            for letter in counter:
                self.assertEqual(counter[letter], per_thread_write_loop * 3)

    def test_move(self):
        ascii_uppercase = string.ascii_uppercase.encode()
        num_letters = len(ascii_uppercase)

        def move(mm_obj):
            for i in range(num_letters):
                # Move 1 byte from the first half to the second half
                mm_obj.move(0 + i, num_letters + i, 1)

        with mmap.mmap(ANONYMOUS_MEM, 2 * num_letters) as mm_obj:
            mm_obj.write(ascii_uppercase)
            run_concurrently(
                worker_func=move,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

    def test_seek_and_tell(self):
        seek_per_thread = 10

        def seek(mm_obj):
            self.assertTrue(mm_obj.seekable())
            for _ in range(seek_per_thread):
                before_seek = mm_obj.tell()
                mm_obj.seek(1, os.SEEK_CUR)
                self.assertLess(before_seek, mm_obj.tell())

        with mmap.mmap(ANONYMOUS_MEM, 1024) as mm_obj:
            run_concurrently(
                worker_func=seek,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )
            # Each thread seeks from current position, the end position should
            # be the sum of all seeks from all threads.
            self.assertEqual(mm_obj.tell(), NTHREADS * seek_per_thread)

    def test_slice_update_and_slice_read(self):
        thread_letters = list(string.ascii_uppercase)
        self.assertLessEqual(NTHREADS, len(thread_letters))

        def slice_update_and_slice_read(mm_obj):
            # Each thread picks a unique letter to write
            thread_letter = thread_letters.pop(0)
            thread_bytes = (thread_letter * 1024).encode()
            for _ in range(100):
                mm_obj[:] = thread_bytes
                read_bytes = mm_obj[:]
                # Read bytes should be all the same letter, showing no
                # interleaving
                self.assertTrue(all_same(read_bytes))

        with mmap.mmap(ANONYMOUS_MEM, 1024) as mm_obj:
            run_concurrently(
                worker_func=slice_update_and_slice_read,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

    def test_item_update_and_item_read(self):
        thread_indexes = [i for i in range(NTHREADS)]

        def item_update_and_item_read(mm_obj):
            # Each thread picks a unique index to write
            thread_index = thread_indexes.pop()
            for i in range(100):
                mm_obj[thread_index] = i
                self.assertEqual(mm_obj[thread_index], i)

            # Read values set by other threads, all values
            # should be less than '100'
            for val in mm_obj:
                self.assertLess(int.from_bytes(val), 100)

        with mmap.mmap(ANONYMOUS_MEM, NTHREADS + 1) as mm_obj:
            run_concurrently(
                worker_func=item_update_and_item_read,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

    def test_close_and_closed(self):
        def close_mmap(mm_obj):
            mm_obj.close()
            self.assertTrue(mm_obj.closed)

        with mmap.mmap(ANONYMOUS_MEM, 1) as mm_obj:
            run_concurrently(
                worker_func=close_mmap,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

    def test_find_and_rfind(self):
        per_thread_loop = 10

        def find_and_rfind(mm_obj):
            pattern = b'Thread-Ident:"%d"' % threading.get_ident()
            mm_obj.write(pattern)
            for _ in range(per_thread_loop):
                found_at = mm_obj.find(pattern, 0)
                self.assertNotEqual(found_at, -1)
                # Should not find it after the `found_at`
                self.assertEqual(mm_obj.find(pattern, found_at + 1), -1)
                found_at_rev = mm_obj.rfind(pattern, 0)
                self.assertEqual(found_at, found_at_rev)
                # Should not find it after the `found_at`
                self.assertEqual(mm_obj.rfind(pattern, found_at + 1), -1)

        with mmap.mmap(ANONYMOUS_MEM, 1024) as mm_obj:
            run_concurrently(
                worker_func=find_and_rfind,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )

    def test_mmap_export_as_memoryview(self):
        """
        Each thread creates a memoryview and updates the internal state of the
        mmap object.
        """
        buffer_size = 42

        def create_memoryview_from_mmap(mm_obj):
            memoryviews = []
            for _ in range(100):
                mv = memoryview(mm_obj)
                memoryviews.append(mv)
                self.assertEqual(len(mv), buffer_size)
                self.assertEqual(mv[:7], b"CPython")

            # Cannot close the mmap while it is exported as buffers
            with self.assertRaisesRegex(
                BufferError, "cannot close exported pointers exist"
            ):
                mm_obj.close()

        with mmap.mmap(ANONYMOUS_MEM, 42) as mm_obj:
            mm_obj.write(b"CPython")
            run_concurrently(
                worker_func=create_memoryview_from_mmap,
                args=(mm_obj,),
                nthreads=NTHREADS,
            )
            # Implicit mm_obj.close() verifies all exports (memoryviews) are
            # properly freed.


def all_same(lst):
    return all(item == lst[0] for item in lst)


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