View Javadoc

1   package org.apache.directmemory.memory.allocator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertNotNull;
23  
24  import java.io.IOException;
25  import java.nio.BufferOverflowException;
26  
27  import junit.framework.Assert;
28  
29  import org.apache.directmemory.DirectMemory;
30  import org.apache.directmemory.cache.CacheService;
31  import org.apache.directmemory.memory.MemoryManagerServiceImpl;
32  import org.apache.directmemory.memory.buffer.MemoryBuffer;
33  import org.junit.Test;
34  
35  public class MergingByteBufferAllocatorTest
36  {
37      @Test
38      public void allocationTest()
39          throws IOException
40      {
41  
42          Allocator allocator = new MergingByteBufferAllocator( 0, 5000 );
43  
44          MemoryBuffer bf1 = allocator.allocate( 250 );
45          Assert.assertEquals( 250, bf1.maxCapacity() );
46          Assert.assertEquals( 250, bf1.capacity() );
47  
48          MemoryBuffer bf2 = allocator.allocate( 251 );
49          Assert.assertEquals( 251, bf2.maxCapacity() );
50          Assert.assertEquals( 251, bf2.capacity() );
51  
52          MemoryBuffer bf3 = allocator.allocate( 200 );
53          Assert.assertEquals( 200, bf3.maxCapacity() );
54          Assert.assertEquals( 200, bf3.capacity() );
55  
56          MemoryBuffer bf4 = allocator.allocate( 2000 );
57          Assert.assertEquals( 2000, bf4.maxCapacity() );
58          Assert.assertEquals( 2000, bf4.capacity() );
59  
60          MemoryBuffer bf5 = allocator.allocate( 2001 );
61          Assert.assertEquals( 2001, bf5.maxCapacity() );
62          Assert.assertEquals( 2001, bf5.capacity() );
63  
64          MemoryBuffer bf6 = allocator.allocate( 298 );
65          Assert.assertEquals( 298, bf6.maxCapacity() );
66          Assert.assertEquals( 298, bf6.capacity() );
67  
68          MemoryBuffer bf7 = allocator.allocate( 128 );
69          Assert.assertNull( bf7 );
70  
71          allocator.close();
72      }
73  
74      @Test
75      public void releaseTest()
76          throws IOException
77      {
78  
79          Allocator allocator = new MergingByteBufferAllocator( 0, 1000 );
80  
81          MemoryBuffer bf1 = allocator.allocate( 250 );
82          Assert.assertEquals( 250, bf1.maxCapacity() );
83          Assert.assertEquals( 250, bf1.capacity() );
84  
85          MemoryBuffer bf2 = allocator.allocate( 251 );
86          Assert.assertEquals( 251, bf2.maxCapacity() );
87          Assert.assertEquals( 251, bf2.capacity() );
88  
89          MemoryBuffer bf3 = allocator.allocate( 252 );
90          Assert.assertEquals( 252, bf3.maxCapacity() );
91          Assert.assertEquals( 252, bf3.capacity() );
92  
93          MemoryBuffer bf4 = allocator.allocate( 500 );
94          Assert.assertNull( bf4 );
95  
96          allocator.free( bf1 );
97          allocator.free( bf2 );
98  
99          MemoryBuffer bf5 = allocator.allocate( 500 );
100         Assert.assertEquals( 501, bf5.maxCapacity() );
101         Assert.assertEquals( 500, bf5.capacity() );
102 
103         allocator.close();
104     }
105 
106     @Test
107     public void allocateAndFreeTest()
108         throws IOException
109     {
110 
111         Allocator allocator = new MergingByteBufferAllocator( 0, 1000 );
112 
113         for ( int i = 0; i < 1000; i++ )
114         {
115             MemoryBuffer bf1 = allocator.allocate( 250 );
116             Assert.assertEquals( 250, bf1.maxCapacity() );
117             Assert.assertEquals( 250, bf1.capacity() );
118 
119             allocator.free( bf1 );
120         }
121 
122         MemoryBuffer bf2 = allocator.allocate( 1000 );
123         Assert.assertEquals( 1000, bf2.maxCapacity() );
124         Assert.assertEquals( 1000, bf2.capacity() );
125 
126         allocator.close();
127     }
128 
129     @Test
130     public void allocationWithoutSplittingPointerTest()
131         throws IOException
132     {
133 
134         Allocator allocator = new MergingByteBufferAllocator( 0, 200 );
135 
136         MemoryBuffer bf1 = allocator.allocate( 180 );
137         Assert.assertEquals( 200, bf1.maxCapacity() );
138         Assert.assertEquals( 180, bf1.capacity() );
139 
140         MemoryBuffer bf2 = allocator.allocate( 5 );
141         Assert.assertNull( bf2 );
142 
143         allocator.free( bf1 );
144 
145         MemoryBuffer bf3 = allocator.allocate( 10 );
146         Assert.assertEquals( 10, bf3.maxCapacity() );
147         Assert.assertEquals( 10, bf3.capacity() );
148 
149         MemoryBuffer bf4 = allocator.allocate( 20 );
150         Assert.assertEquals( 20, bf4.maxCapacity() );
151         Assert.assertEquals( 20, bf4.capacity() );
152 
153         MemoryBuffer bf5 = allocator.allocate( 30 );
154         Assert.assertEquals( 30, bf5.maxCapacity() );
155         Assert.assertEquals( 30, bf5.capacity() );
156 
157         allocator.free( bf4 );
158         allocator.free( bf3 );
159 
160         MemoryBuffer bf6 = allocator.allocate( 25 );
161         Assert.assertEquals( 30, bf6.maxCapacity() );
162         Assert.assertEquals( 25, bf6.capacity() );
163 
164         allocator.close();
165     }
166 
167     @Test
168     public void allocationWithDifferentRatioTest()
169         throws IOException
170     {
171 
172         MergingByteBufferAllocator allocator = new MergingByteBufferAllocator( 0, 200 );
173         allocator.setSizeRatioThreshold( 0.95 );
174 
175         allocator.setSizeRatioThreshold( 10 );
176 
177         MemoryBuffer bf1 = allocator.allocate( 180 );
178         Assert.assertEquals( 180, bf1.maxCapacity() );
179         Assert.assertEquals( 180, bf1.capacity() );
180 
181         MemoryBuffer bf2 = allocator.allocate( 10 );
182         Assert.assertEquals( 20, bf2.maxCapacity() );
183         Assert.assertEquals( 10, bf2.capacity() );
184 
185         allocator.close();
186     }
187 
188     @Test( expected = BufferOverflowException.class )
189     public void allocationThrowingBOExceptionTest()
190         throws IOException
191     {
192 
193         MergingByteBufferAllocator allocator = new MergingByteBufferAllocator( 0, 200 );
194         allocator.setReturnNullWhenBufferIsFull( false );
195 
196         try
197         {
198             allocator.allocate( 210 );
199             Assert.fail();
200         }
201         finally
202         {
203             allocator.close();
204         }
205     }
206 
207     @Test
208     public void testJiraIssue118()
209         throws Exception
210     {
211         CacheService<String, Object> cacheService = new DirectMemory<String, Object>() 
212                         .setNumberOfBuffers(1) 
213                         .setInitialCapacity(1) 
214                         .setMemoryManager(new MemoryManagerServiceImpl<Object>(true)) 
215                         .setSize(350 *(1024*1024)) 
216                         .setConcurrencyLevel(1) 
217                         .newCacheService();
218         assertNotNull(cacheService);
219     }
220 
221 }