1 package org.apache.directmemory.memory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
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
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 }