1+ from __future__ import with_statement , print_function
2+
13from .lib import TestBase , FileCreator
24
35from smmap .mman import *
1214from copy import copy
1315
1416class TestMMan (TestBase ):
15-
17+
1618 def test_cursor (self ):
1719 fc = FileCreator (self .k_window_test_size , "cursor_test" )
18-
20+
1921 man = SlidingWindowMapManager ()
2022 ci = WindowCursor (man ) # invalid cursor
2123 assert not ci .is_valid ()
2224 assert not ci .is_associated ()
2325 assert ci .size () == 0 # this is cached, so we can query it in invalid state
24-
26+
2527 cv = man .make_cursor (fc .path )
2628 assert not cv .is_valid () # no region mapped yet
2729 assert cv .is_associated ()# but it know where to map it from
2830 assert cv .file_size () == fc .size
2931 assert cv .path () == fc .path
30-
32+
3133 # copy module
3234 cio = copy (cv )
3335 assert not cio .is_valid () and cio .is_associated ()
34-
36+
3537 # assign method
3638 assert not ci .is_associated ()
3739 ci .assign (cv )
3840 assert not ci .is_valid () and ci .is_associated ()
39-
41+
4042 # unuse non-existing region is fine
4143 cv .unuse_region ()
4244 cv .unuse_region ()
43-
45+
4446 # destruction is fine (even multiple times)
4547 cv ._destroy ()
4648 WindowCursor (man )._destroy ()
47-
49+
4850 def test_memory_manager (self ):
4951 slide_man = SlidingWindowMapManager ()
5052 static_man = StaticWindowMapManager ()
51-
53+
5254 for man in (static_man , slide_man ):
5355 assert man .num_file_handles () == 0
5456 assert man .num_open_files () == 0
@@ -59,15 +61,15 @@ def test_memory_manager(self):
5961 assert man .window_size () > winsize_cmp_val
6062 assert man .mapped_memory_size () == 0
6163 assert man .max_mapped_memory_size () > 0
62-
64+
6365 # collection doesn't raise in 'any' mode
6466 man ._collect_lru_region (0 )
6567 # doesn't raise if we are within the limit
6668 man ._collect_lru_region (10 )
67-
68- # doesn't fail if we overallocate
69+
70+ # doesn't fail if we overallocate
6971 assert man ._collect_lru_region (sys .maxsize ) == 0
70-
72+
7173 # use a region, verify most basic functionality
7274 fc = FileCreator (self .k_window_test_size , "manager_test" )
7375 fd = os .open (fc .path , os .O_RDONLY )
@@ -77,8 +79,9 @@ def test_memory_manager(self):
7779 assert c .use_region (10 , 10 ).is_valid ()
7880 assert c .ofs_begin () == 10
7981 assert c .size () == 10
80- assert c .buffer ()[:] == open (fc .path , 'rb' ).read (20 )[10 :]
81-
82+ with open (fc .path , 'rb' ) as fp :
83+ assert c .buffer ()[:] == fp .read (20 )[10 :]
84+
8285 if isinstance (item , int ):
8386 self .assertRaises (ValueError , c .path )
8487 else :
@@ -87,38 +90,39 @@ def test_memory_manager(self):
8790 #END for each input
8891 os .close (fd )
8992 # END for each manager type
90-
93+
9194 def test_memman_operation (self ):
9295 # test more access, force it to actually unmap regions
9396 fc = FileCreator (self .k_window_test_size , "manager_operation_test" )
94- data = open (fc .path , 'rb' ).read ()
97+ with open (fc .path , 'rb' ) as fp :
98+ data = fp .read ()
9599 fd = os .open (fc .path , os .O_RDONLY )
96100 max_num_handles = 15
97- #small_size =
101+ #small_size =
98102 for mtype , args in ( (StaticWindowMapManager , (0 , fc .size // 3 , max_num_handles )),
99103 (SlidingWindowMapManager , (fc .size // 100 , fc .size // 3 , max_num_handles )),):
100104 for item in (fc .path , fd ):
101105 assert len (data ) == fc .size
102-
106+
103107 # small windows, a reasonable max memory. Not too many regions at once
104108 man = mtype (window_size = args [0 ], max_memory_size = args [1 ], max_open_handles = args [2 ])
105109 c = man .make_cursor (item )
106-
110+
107111 # still empty (more about that is tested in test_memory_manager()
108112 assert man .num_open_files () == 0
109113 assert man .mapped_memory_size () == 0
110-
114+
111115 base_offset = 5000
112116 # window size is 0 for static managers, hence size will be 0. We take that into consideration
113117 size = man .window_size () // 2
114118 assert c .use_region (base_offset , size ).is_valid ()
115119 rr = c .region_ref ()
116120 assert rr ().client_count () == 2 # the manager and the cursor and us
117-
121+
118122 assert man .num_open_files () == 1
119123 assert man .num_file_handles () == 1
120124 assert man .mapped_memory_size () == rr ().size ()
121-
125+
122126 #assert c.size() == size # the cursor may overallocate in its static version
123127 assert c .ofs_begin () == base_offset
124128 assert rr ().ofs_begin () == 0 # it was aligned and expanded
@@ -127,9 +131,9 @@ def test_memman_operation(self):
127131 else :
128132 assert rr ().size () == fc .size
129133 #END ignore static managers which dont use windows and are aligned to file boundaries
130-
131- assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
132-
134+
135+ assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
136+
133137 # obtain second window, which spans the first part of the file - it is a still the same window
134138 nsize = (size or fc .size ) - 10
135139 assert c .use_region (0 , nsize ).is_valid ()
@@ -138,7 +142,7 @@ def test_memman_operation(self):
138142 assert c .size () == nsize
139143 assert c .ofs_begin () == 0
140144 assert c .buffer ()[:] == data [:nsize ]
141-
145+
142146 # map some part at the end, our requested size cannot be kept
143147 overshoot = 4000
144148 base_offset = fc .size - (size or c .size ()) + overshoot
@@ -156,23 +160,23 @@ def test_memman_operation(self):
156160 assert rr ().ofs_begin () < c .ofs_begin () # it should have extended itself to the left
157161 assert rr ().ofs_end () <= fc .size # it cannot be larger than the file
158162 assert c .buffer ()[:] == data [base_offset :base_offset + (size or c .size ())]
159-
163+
160164 # unising a region makes the cursor invalid
161165 c .unuse_region ()
162166 assert not c .is_valid ()
163167 if man .window_size ():
164- # but doesn't change anything regarding the handle count - we cache it and only
168+ # but doesn't change anything regarding the handle count - we cache it and only
165169 # remove mapped regions if we have to
166170 assert man .num_file_handles () == 2
167171 #END ignore this for static managers
168-
172+
169173 # iterate through the windows, verify data contents
170174 # this will trigger map collection after a while
171175 max_random_accesses = 5000
172176 num_random_accesses = max_random_accesses
173177 memory_read = 0
174178 st = time ()
175-
179+
176180 # cache everything to get some more performance
177181 includes_ofs = c .includes_ofs
178182 max_mapped_memory_size = man .max_mapped_memory_size ()
@@ -182,7 +186,7 @@ def test_memman_operation(self):
182186 while num_random_accesses :
183187 num_random_accesses -= 1
184188 base_offset = randint (0 , fc .size - 1 )
185-
189+
186190 # precondition
187191 if man .window_size ():
188192 assert max_mapped_memory_size >= mapped_memory_size ()
@@ -192,19 +196,20 @@ def test_memman_operation(self):
192196 csize = c .size ()
193197 assert c .buffer ()[:] == data [base_offset :base_offset + csize ]
194198 memory_read += csize
195-
199+
196200 assert includes_ofs (base_offset )
197201 assert includes_ofs (base_offset + csize - 1 )
198202 assert not includes_ofs (base_offset + csize )
199203 # END while we should do an access
200204 elapsed = max (time () - st , 0.001 ) # prevent zero divison errors on windows
201205 mb = float (1000 * 1000 )
202- sys .stderr .write ("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n "
203- % (mtype , memory_read / mb , max_random_accesses , type (item ), elapsed , (memory_read / mb )/ elapsed ))
204-
206+ print ("%s: Read %i mb of memory with %i random on cursor initialized with %s accesses in %fs (%f mb/s)\n "
207+ % (mtype , memory_read / mb , max_random_accesses , type (item ), elapsed , (memory_read / mb )/ elapsed ),
208+ file = sys .stderr )
209+
205210 # an offset as large as the size doesn't work !
206211 assert not c .use_region (fc .size , size ).is_valid ()
207-
212+
208213 # collection - it should be able to collect all
209214 assert man .num_file_handles ()
210215 assert man .collect ()
0 commit comments