Open Kilda Java Documentation
ResourcePoolTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.messaging.payload;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import org.junit.Test;
21 
22 public class ResourcePoolTest {
23  private static final ResourcePool pool = new ResourcePool(1, 5);
24 
25  @Test
26  public void resourcePoolTest() {
27  int first = pool.allocate();
28  assertEquals(1, first);
29 
30  int second = pool.allocate();
31  assertEquals(2, second);
32 
33  int third = pool.allocate();
34  assertEquals(3, third);
35 
36  pool.deallocate(second);
37 
38  int fourth = pool.allocate();
39  assertEquals(4, fourth);
40 
41  int fifth = pool.allocate();
42  assertEquals(5, fifth);
43 
44  int sixth = pool.allocate();
45  assertEquals(2, sixth);
46 
47  assertEquals(5, pool.dumpPool().size());
48  }
49 
50 
51  @Test
52  public void testRollover() {
53  ResourcePool mypool = new ResourcePool(1, 5);
54 
55  int first = mypool.allocate(5);
56  assertEquals(5, first);
57 
58  int second = mypool.allocate();
59  assertEquals(1, second);
60 
61  assertEquals(2, mypool.dumpPool().size());
62  }
63 
64 
65  @Test(expected = ArrayIndexOutOfBoundsException.class)
66  public void resourcePoolFullTest() {
67  ResourcePool pool = new ResourcePool(1, 1);
68  pool.allocate();
69  pool.allocate();
70  }
71 }
Integer deallocate(final Integer resourceId)