@@ -96,10 +96,11 @@ public void testEmpty() throws Exception
9696 public void testTwoSegments () throws Exception
9797 {
9898 ByteBuffer buffer = allocateBuffer (2048 );
99+ int bufSize = 1024 ;
99100 try (ChannelProxy channel = new ChannelProxy (writeFile ("testTwoSegments" , buffer ));
100101 MmappedRegions regions = MmappedRegions .empty (channel ))
101102 {
102- regions .extend (1024 );
103+ regions .extend (1024 , bufSize );
103104 for (int i = 0 ; i < 1024 ; i ++)
104105 {
105106 MmappedRegions .Region region = regions .floor (i );
@@ -108,7 +109,7 @@ public void testTwoSegments() throws Exception
108109 assertEquals (1024 , region .end ());
109110 }
110111
111- regions .extend (2048 );
112+ regions .extend (2048 , bufSize );
112113 for (int i = 0 ; i < 2048 ; i ++)
113114 {
114115 MmappedRegions .Region region = regions .floor (i );
@@ -131,15 +132,16 @@ public void testTwoSegments() throws Exception
131132 public void testSmallSegmentSize () throws Exception
132133 {
133134 int OLD_MAX_SEGMENT_SIZE = MmappedRegions .MAX_SEGMENT_SIZE ;
135+ int bufSize = 1024 ;
134136 MmappedRegions .MAX_SEGMENT_SIZE = 1024 ;
135137
136138 ByteBuffer buffer = allocateBuffer (4096 );
137139 try (ChannelProxy channel = new ChannelProxy (writeFile ("testSmallSegmentSize" , buffer ));
138140 MmappedRegions regions = MmappedRegions .empty (channel ))
139141 {
140- regions .extend (1024 );
141- regions .extend (2048 );
142- regions .extend (4096 );
142+ regions .extend (1024 , bufSize );
143+ regions .extend (2048 , bufSize );
144+ regions .extend (4096 , bufSize );
143145
144146 final int SIZE = MmappedRegions .MAX_SEGMENT_SIZE ;
145147 for (int i = 0 ; i < buffer .capacity (); i ++)
@@ -156,18 +158,46 @@ public void testSmallSegmentSize() throws Exception
156158 }
157159 }
158160
161+ @ Test
162+ public void testSizeIsChunkMultiple () throws Exception
163+ {
164+ final int oldMaxSegmentSize = MmappedRegions .MAX_SEGMENT_SIZE ;
165+ final int bufSize = 1024 ;
166+ MmappedRegions .MAX_SEGMENT_SIZE = 2047 ;
167+ ByteBuffer buffer = allocateBuffer (4096 );
168+ try (ChannelProxy channel = new ChannelProxy (writeFile ("testSmallSegmentSize" , buffer ));
169+ MmappedRegions regions = MmappedRegions .empty (channel ))
170+ {
171+ regions .extend (1024 , bufSize );
172+ regions .extend (2048 , bufSize );
173+ regions .extend (4096 , bufSize );
174+ for (int i = 0 ; i < buffer .capacity (); i ++)
175+ {
176+ MmappedRegions .Region region = regions .floor (i );
177+ assertNotNull (region );
178+ assertEquals (bufSize * (i / bufSize ), region .offset ());
179+ assertEquals (bufSize + (bufSize * (i / bufSize )), region .end ());
180+ }
181+ }
182+ finally
183+ {
184+ MmappedRegions .MAX_SEGMENT_SIZE = oldMaxSegmentSize ;
185+ }
186+ }
187+
159188 @ Test
160189 public void testAllocRegions () throws Exception
161190 {
162191 int OLD_MAX_SEGMENT_SIZE = MmappedRegions .MAX_SEGMENT_SIZE ;
163192 MmappedRegions .MAX_SEGMENT_SIZE = 1024 ;
193+ int bufSize = 1024 ;
164194
165195 ByteBuffer buffer = allocateBuffer (MmappedRegions .MAX_SEGMENT_SIZE * MmappedRegions .REGION_ALLOC_SIZE * 3 );
166196
167197 try (ChannelProxy channel = new ChannelProxy (writeFile ("testAllocRegions" , buffer ));
168198 MmappedRegions regions = MmappedRegions .empty (channel ))
169199 {
170- regions .extend (buffer .capacity ());
200+ regions .extend (buffer .capacity (), bufSize );
171201
172202 final int SIZE = MmappedRegions .MAX_SEGMENT_SIZE ;
173203 for (int i = 0 ; i < buffer .capacity (); i ++)
@@ -188,17 +218,18 @@ public void testAllocRegions() throws Exception
188218 public void testCopy () throws Exception
189219 {
190220 ByteBuffer buffer = allocateBuffer (128 * 1024 );
221+ int bufSize = 4096 ;
191222
192223 MmappedRegions snapshot ;
193224 ChannelProxy channelCopy ;
194225
195226 try (ChannelProxy channel = new ChannelProxy (writeFile ("testSnapshot" , buffer ));
196- MmappedRegions regions = MmappedRegions .map (channel , buffer .capacity () / 4 , 0 , false ))
227+ MmappedRegions regions = MmappedRegions .map (channel , buffer .capacity () / 4 , bufSize , 0 , false ))
197228 {
198229 // create 3 more segments, one per quater capacity
199- regions .extend (buffer .capacity () / 2 );
200- regions .extend (3 * buffer .capacity () / 4 );
201- regions .extend (buffer .capacity ());
230+ regions .extend (buffer .capacity () / 2 , bufSize );
231+ regions .extend (3 * buffer .capacity () / 4 , bufSize );
232+ regions .extend (buffer .capacity (), bufSize );
202233
203234 // make a snapshot
204235 snapshot = regions .sharedCopy ();
@@ -230,14 +261,15 @@ public void testCopy() throws Exception
230261 public void testCopyCannotExtend () throws Exception
231262 {
232263 ByteBuffer buffer = allocateBuffer (128 * 1024 );
264+ int bufSize = 1024 ;
233265
234266 MmappedRegions snapshot ;
235267 ChannelProxy channelCopy ;
236268
237269 try (ChannelProxy channel = new ChannelProxy (writeFile ("testSnapshotCannotExtend" , buffer ));
238270 MmappedRegions regions = MmappedRegions .empty (channel ))
239271 {
240- regions .extend (buffer .capacity () / 2 );
272+ regions .extend (buffer .capacity () / 2 , bufSize );
241273
242274 // make a snapshot
243275 snapshot = regions .sharedCopy ();
@@ -248,7 +280,7 @@ public void testCopyCannotExtend() throws Exception
248280
249281 try
250282 {
251- snapshot .extend (buffer .capacity ());
283+ snapshot .extend (buffer .capacity (), bufSize );
252284 }
253285 finally
254286 {
@@ -261,12 +293,13 @@ public void testCopyCannotExtend() throws Exception
261293 public void testExtendOutOfOrder () throws Exception
262294 {
263295 ByteBuffer buffer = allocateBuffer (4096 );
296+ int bufSize = 1024 ;
264297 try (ChannelProxy channel = new ChannelProxy (writeFile ("testExtendOutOfOrder" , buffer ));
265298 MmappedRegions regions = MmappedRegions .empty (channel ))
266299 {
267- regions .extend (4096 );
268- regions .extend (1024 );
269- regions .extend (2048 );
300+ regions .extend (4096 , bufSize );
301+ regions .extend (1024 , bufSize );
302+ regions .extend (2048 , bufSize );
270303
271304 for (int i = 0 ; i < buffer .capacity (); i ++)
272305 {
@@ -282,10 +315,11 @@ public void testExtendOutOfOrder() throws Exception
282315 public void testNegativeExtend () throws Exception
283316 {
284317 ByteBuffer buffer = allocateBuffer (1024 );
318+ int bufSize = 1024 ;
285319 try (ChannelProxy channel = new ChannelProxy (writeFile ("testNegativeExtend" , buffer ));
286320 MmappedRegions regions = MmappedRegions .empty (channel ))
287321 {
288- regions .extend (-1 );
322+ regions .extend (-1 , bufSize );
289323 }
290324 }
291325
@@ -346,8 +380,9 @@ public void testMapForCompressionMetadata() throws Exception
346380 public void testIllegalArgForMap1 () throws Exception
347381 {
348382 ByteBuffer buffer = allocateBuffer (1024 );
383+ int bufSize = 1024 ;
349384 try (ChannelProxy channel = new ChannelProxy (writeFile ("testIllegalArgForMap1" , buffer ));
350- MmappedRegions regions = MmappedRegions .map (channel , 0 , 0 , false ))
385+ MmappedRegions regions = MmappedRegions .map (channel , 0 , bufSize , 0 , false ))
351386 {
352387 assertTrue (regions .isEmpty ());
353388 }
@@ -357,8 +392,9 @@ public void testIllegalArgForMap1() throws Exception
357392 public void testIllegalArgForMap2 () throws Exception
358393 {
359394 ByteBuffer buffer = allocateBuffer (1024 );
395+ int bufSize = 1024 ;
360396 try (ChannelProxy channel = new ChannelProxy (writeFile ("testIllegalArgForMap2" , buffer ));
361- MmappedRegions regions = MmappedRegions .map (channel , -1L , 0 , false ))
397+ MmappedRegions regions = MmappedRegions .map (channel , -1L , bufSize , 0 , false ))
362398 {
363399 assertTrue (regions .isEmpty ());
364400 }
@@ -374,5 +410,4 @@ public void testIllegalArgForMap3() throws Exception
374410 assertTrue (regions .isEmpty ());
375411 }
376412 }
377-
378413}
0 commit comments