View Javadoc

1   package org.apache.directmemory.memory;
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 java.io.IOException;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Random;
26  
27  import junit.framework.Assert;
28  
29  import org.junit.Test;
30  import org.junit.runner.RunWith;
31  import org.junit.runners.Parameterized;
32  import org.junit.runners.Parameterized.Parameters;
33  
34  @RunWith( Parameterized.class )
35  public class MemoryManagerServiceImplTest
36  {
37  
38      protected static final Random R = new Random();
39  
40      protected static final byte[] SMALL_PAYLOAD = "ABCD".getBytes();
41  
42      @Parameters
43      public static Collection<Object[]> data()
44      {
45          return Arrays.asList( new Object[][] { { MemoryManagerServiceImpl.class },
46              { UnsafeMemoryManagerServiceImpl.class } } );
47      }
48  
49      private final Class<? extends MemoryManagerService<Object>> memoryManagerServiceClass;
50  
51      public MemoryManagerServiceImplTest( Class<? extends MemoryManagerService<Object>> memoryManagerServiceClass )
52      {
53          this.memoryManagerServiceClass = memoryManagerServiceClass;
54      }
55  
56      protected MemoryManagerService<Object> getMemoryManagerService()
57      {
58          try
59          {
60              return memoryManagerServiceClass.newInstance();
61          }
62          catch ( Exception e )
63          {
64              throw new RuntimeException( e );
65          }
66      }
67  
68      @Test
69      public void testFirstMatchBorderCase()
70          throws IOException
71      {
72  
73          // Storing a first payload of 4 bytes, 1 byte remaining in the buffer.
74          // When storing a second 4 bytes payload, an BufferOverflowException is
75          // thrown instead of an easy check.
76  
77          final int BUFFER_SIZE = 5;
78  
79          final MemoryManagerService<Object> memoryManagerService = getMemoryManagerService();
80  
81          memoryManagerService.init( 1, BUFFER_SIZE );
82  
83          Pointer<Object> pointer1 = memoryManagerService.store( SMALL_PAYLOAD );
84          Assert.assertNotNull( pointer1 );
85  
86          Pointer<Object> pointer2 = memoryManagerService.store( SMALL_PAYLOAD );
87          Assert.assertNull( pointer2 );
88  
89          memoryManagerService.close();
90      }
91  
92      @Test
93      public void testAllocateMultipleBuffers()
94          throws IOException
95      {
96  
97          // Initializing 4 buffers of 4 bytes, MemoryManagerService should search
98          // for available space in another buffer.
99  
100         final int NUMBER_OF_OBJECTS = 4;
101 
102         final MemoryManagerService<Object> memoryManagerService = getMemoryManagerService();
103 
104         memoryManagerService.init( NUMBER_OF_OBJECTS, SMALL_PAYLOAD.length );
105 
106         for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
107         {
108             Pointer<Object> pointer = memoryManagerService.store( SMALL_PAYLOAD );
109             Assert.assertNotNull( pointer );
110         }
111 
112         Pointer<Object> pointerNull = memoryManagerService.store( SMALL_PAYLOAD );
113         Assert.assertNull( pointerNull );
114 
115         memoryManagerService.close();
116     }
117 
118     @Test
119     public void testByteLeaking()
120         throws IOException
121     {
122 
123         // Initializing 1 buffer of 10*4 bytes, should be able to allocate 10
124         // objects of 4 bytes.
125 
126         final int NUMBER_OF_OBJECTS = 10;
127 
128         final MemoryManagerService<Object> memoryManagerService = getMemoryManagerService();
129         memoryManagerService.init( 1, NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length );
130 
131         for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
132         {
133             Pointer<Object> pointer = memoryManagerService.store( SMALL_PAYLOAD );
134             Assert.assertNotNull( pointer );
135         }
136 
137         Pointer<Object> pointerNull = memoryManagerService.store( SMALL_PAYLOAD );
138         Assert.assertNull( pointerNull );
139 
140         memoryManagerService.close();
141     }
142 
143     @Test
144     public void testReportCorrectUsedMemory()
145         throws IOException
146     {
147 
148         // Initializing 1 buffer of 4*4 bytes, storing and freeing and storing
149         // again should report correct numbers.
150 
151         final int NUMBER_OF_OBJECTS = 4;
152         final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length;
153 
154         final MemoryManagerService<Object> memoryManagerService = getMemoryManagerService();
155 
156         memoryManagerService.init( 1, BUFFER_SIZE );
157 
158         Pointer<Object> lastPointer = null;
159         for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
160         {
161             Pointer<Object> pointer = memoryManagerService.store( SMALL_PAYLOAD );
162             Assert.assertNotNull( pointer );
163             lastPointer = pointer;
164         }
165 
166         // Buffer is fully used.
167         Assert.assertEquals( BUFFER_SIZE, memoryManagerService.used() );
168 
169         Assert.assertNotNull( lastPointer );
170         memoryManagerService.free( lastPointer );
171 
172         Pointer<Object> pointerNotNull = memoryManagerService.store( SMALL_PAYLOAD );
173         Assert.assertNotNull( pointerNotNull );
174 
175         // Buffer again fully used.
176         Assert.assertEquals( BUFFER_SIZE, memoryManagerService.used() );
177 
178         memoryManagerService.close();
179     }
180 
181     @Test
182     public void testRandomPayload()
183         throws IOException
184     {
185 
186         final int NUMBER_OF_OBJECTS = 10;
187         final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length;
188 
189         final MemoryManagerService<Object> memoryManagerService = getMemoryManagerService();
190 
191         memoryManagerService.init( 1, BUFFER_SIZE );
192 
193         for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
194         {
195             byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length );
196             Pointer<Object> pointer = memoryManagerService.store( payload );
197             Assert.assertNotNull( pointer );
198             byte[] fetchedPayload = memoryManagerService.retrieve( pointer );
199             Assert.assertEquals( new String( payload ), new String( fetchedPayload ) );
200             if ( R.nextBoolean() )
201             {
202                 memoryManagerService.free( pointer );
203             }
204         }
205 
206         memoryManagerService.clear();
207 
208         for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
209         {
210             byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length );
211             Pointer<Object> pointer = memoryManagerService.store( payload );
212             Assert.assertNotNull( pointer );
213             byte[] fetchedPayload = memoryManagerService.retrieve( pointer );
214             Assert.assertEquals( new String( payload ), new String( fetchedPayload ) );
215             if ( R.nextBoolean() )
216             {
217                 memoryManagerService.free( pointer );
218                 i--;
219             }
220         }
221 
222         memoryManagerService.clear();
223 
224         Pointer<Object> pointer = null;
225         do
226         {
227             byte[] payload = MemoryTestUtils.generateRandomPayload( R.nextInt( BUFFER_SIZE / 4 + 1 ) );
228             pointer = memoryManagerService.store( payload );
229             if ( pointer != null && R.nextBoolean() )
230             {
231                 memoryManagerService.free( pointer );
232             }
233         }
234         while ( pointer != null );
235 
236         memoryManagerService.close();
237     }
238 
239 }