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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.directmemory.memory.allocator.Allocator;
31  import org.apache.directmemory.memory.buffer.MemoryBuffer;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  /**
37   * Unit test of {@link RoundRobinAllocationPolicy} class.
38   * 
39   * @author benoit@noisette.ch
40   */
41  public class RoundRobinAllocationPolicyTest
42  {
43  
44      private static final int NUMBER_OF_BUFFERS = 4;
45  
46      List<Allocator> allocators;
47  
48      RoundRobinAllocationPolicy allocationPolicy;
49  
50      @Before
51      public void initAllocationPolicy()
52      {
53  
54          allocators = new ArrayList<Allocator>();
55  
56          for ( int i = 0; i < NUMBER_OF_BUFFERS; i++ )
57          {
58              allocators.add( new DummyByteBufferAllocator() );
59          }
60  
61          allocationPolicy = new RoundRobinAllocationPolicy();
62          allocationPolicy.init( allocators );
63      }
64  
65      @After
66      public void cleanup()
67          throws IOException
68      {
69          for ( Allocator allocator : allocators )
70          {
71              allocator.close();
72          }
73      }
74  
75      @Test
76      public void testSequence()
77      {
78  
79          assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
80          assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
81          assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
82          assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
83          assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
84          assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
85          assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
86          assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
87  
88          assertNotNull( allocationPolicy.getActiveAllocator( null, 1 ) );
89          assertNotNull( allocationPolicy.getActiveAllocator( null, 2 ) );
90          assertNull( allocationPolicy.getActiveAllocator( null, 3 ) );
91  
92          allocationPolicy.reset();
93  
94          assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
95          assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
96          assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
97          assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
98          assertEquals( allocators.get( 0 ), allocationPolicy.getActiveAllocator( null, 1 ) );
99          assertEquals( allocators.get( 1 ), allocationPolicy.getActiveAllocator( null, 1 ) );
100         assertEquals( allocators.get( 2 ), allocationPolicy.getActiveAllocator( null, 1 ) );
101         assertEquals( allocators.get( 3 ), allocationPolicy.getActiveAllocator( null, 1 ) );
102 
103     }
104 
105     @Test
106     public void testMaxAllocation()
107     {
108 
109         allocationPolicy.setMaxAllocations( 1 );
110 
111         assertNotNull( allocationPolicy.getActiveAllocator( null, 1 ) );
112         assertNull( allocationPolicy.getActiveAllocator( null, 2 ) );
113         assertNull( allocationPolicy.getActiveAllocator( null, 3 ) );
114 
115     }
116 
117     /**
118      * Dummy {@link OffHeapMemoryBuffer} that do nothing.
119      */
120     private static class DummyByteBufferAllocator
121         implements Allocator
122     {
123 
124         @Override
125         public void free( MemoryBuffer buffer )
126         {
127         }
128 
129         @Override
130         public MemoryBuffer allocate( int size )
131         {
132             return null;
133         }
134 
135         @Override
136         public void clear()
137         {
138         }
139 
140         @Override
141         public int getCapacity()
142         {
143             return 0;
144         }
145 
146         @Override
147         public int getNumber()
148         {
149             return 0;
150         }
151 
152         @Override
153         public void close()
154             throws IOException
155         {
156 
157         }
158 
159     }
160 }