Carmelo Cascone | da15af8 | 2019-12-09 22:36:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License.% |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.pipelines.fabric.impl.behaviour.bng; |
| 18 | |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertNotEquals; |
| 24 | import static org.junit.Assert.fail; |
| 25 | |
| 26 | /** |
| 27 | * Tests for SimpleBngLineIdAllocator. |
| 28 | */ |
| 29 | public class SimpleBngLineIdAllocatorTest { |
| 30 | |
| 31 | private static final int SIZE = 10; |
| 32 | |
| 33 | @Test |
| 34 | public void allocateAndReleaseTest() throws FabricBngLineIdAllocator.IdExhaustedException { |
| 35 | var allocator = new SimpleBngLineIdAllocator(SIZE); |
| 36 | |
| 37 | var id1 = allocator.allocate(new MockAttachment(1)); |
| 38 | var sameAsId1 = allocator.allocate(new MockAttachment(1)); |
| 39 | |
| 40 | var id2 = allocator.allocate(new MockAttachment(2)); |
| 41 | |
| 42 | assertEquals(allocator.allocatedCount(), 2); |
| 43 | assertEquals(allocator.freeCount(), SIZE - allocator.allocatedCount()); |
| 44 | |
| 45 | assertEquals(id1, sameAsId1); |
| 46 | assertNotEquals(id1, id2); |
| 47 | |
| 48 | allocator.release(new MockAttachment(1)); |
| 49 | assertEquals(allocator.allocatedCount(), 1); |
| 50 | assertEquals(allocator.freeCount(), SIZE - allocator.allocatedCount()); |
| 51 | |
| 52 | allocator.release(id2); |
| 53 | assertEquals(allocator.allocatedCount(), 0); |
| 54 | assertEquals(allocator.freeCount(), SIZE); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void exhaustionTest() throws FabricBngLineIdAllocator.IdExhaustedException { |
| 59 | var allocator = new SimpleBngLineIdAllocator(SIZE); |
| 60 | var equalTester = new EqualsTester(); |
| 61 | for (int i = 0; i < SIZE; i++) { |
| 62 | // Add ID to equality group to later make sure that all IDs are |
| 63 | // different. |
| 64 | equalTester.addEqualityGroup( |
| 65 | allocator.allocate(new MockAttachment(i))); |
| 66 | } |
| 67 | |
| 68 | assertEquals(allocator.allocatedCount(), SIZE); |
| 69 | assertEquals(allocator.freeCount(), 0); |
| 70 | equalTester.testEquals(); |
| 71 | |
| 72 | try { |
| 73 | allocator.allocate(new MockAttachment(SIZE + 1)); |
| 74 | fail("IdExhaustedException not thrown"); |
| 75 | } catch (FabricBngLineIdAllocator.IdExhaustedException e) { |
| 76 | // Expected. |
| 77 | } |
| 78 | |
| 79 | for (int i = 0; i < SIZE; i++) { |
| 80 | allocator.release(new MockAttachment(i)); |
| 81 | } |
| 82 | |
| 83 | assertEquals(allocator.allocatedCount(), 0); |
| 84 | assertEquals(allocator.freeCount(), SIZE); |
| 85 | } |
| 86 | |
| 87 | } |