Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 4141a91 | 2022-12-29 16:51:38 -0500 | [diff] [blame^] | 2 | * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 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.opencord.aaa.impl; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 22 | import org.junit.runner.RunWith; |
| 23 | import org.junit.runners.Parameterized; |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 26 | import java.util.concurrent.Callable; |
| 27 | import java.util.concurrent.ExecutionException; |
| 28 | import java.util.concurrent.ExecutorService; |
| 29 | import java.util.concurrent.Executors; |
| 30 | import java.util.concurrent.Future; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | |
| 33 | import static org.junit.Assert.*; |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 36 | @RunWith(Parameterized.class) |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 37 | public class IdentifierManagerTest { |
| 38 | |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 39 | // Change this to have more run with mvn |
| 40 | @Parameterized.Parameters |
| 41 | public static Object[][] data() { |
| 42 | return new Object[1][0]; |
| 43 | } |
| 44 | |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 45 | IdentifierManager idManager = null; |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() { |
| 50 | System.out.print("Set up"); |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 51 | idManager.timeout = 1500; |
| 52 | idManager.pruningInterval = 1; |
| 53 | idManager.pollTimeout = 1; |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 54 | idManager = new IdentifierManager(); |
| 55 | } |
| 56 | |
| 57 | @After |
| 58 | public void tearDown() { |
| 59 | System.out.print("Tear down"); |
| 60 | idManager = null; |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | /** |
| 65 | * Make sure that we never get ID 1 or 0 as they are reserved for RadiusOperationalStatusManager |
| 66 | */ |
| 67 | public void testIdSequence() { |
| 68 | for (int i = 1; i <= 300; i++) { |
| 69 | RequestIdentifier id = idManager.getNewIdentifier(Integer.toString(i)); |
Matteo Scandolo | 105210b | 2020-12-10 10:32:39 -0800 | [diff] [blame] | 70 | log.trace("Id: {}", id.getReadableIdentifier()); |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 71 | assertNotEquals(id.identifier(), 0); |
| 72 | assertNotEquals(id.identifier(), 1); |
| 73 | idManager.releaseIdentifier(id); |
| 74 | } |
| 75 | } |
Matteo Scandolo | bbc1ffb | 2020-10-16 15:56:20 -0700 | [diff] [blame] | 76 | |
| 77 | @Test(timeout = 3800) |
| 78 | public void testIdRelease() { |
| 79 | assertEquals(254, idManager.getAvailableIdentifiers()); |
| 80 | for (int i = 0; i <= 253; i++) { |
| 81 | idManager.getNewIdentifier(Integer.toString(i)); |
| 82 | } |
| 83 | |
| 84 | assertEquals(0, idManager.getAvailableIdentifiers()); |
| 85 | |
| 86 | try { |
| 87 | TimeUnit.MILLISECONDS.sleep(3500); |
| 88 | } catch (InterruptedException e) { |
| 89 | log.error("Can't sleep"); |
| 90 | } |
| 91 | |
| 92 | // check that the queue has been emptied after the timeout occurred |
| 93 | assertEquals(254, idManager.getAvailableIdentifiers()); |
| 94 | |
| 95 | // check that I can get a new ID immediately (note the timeout in the test declaration) |
| 96 | idManager.getNewIdentifier(Integer.toString(254)); |
| 97 | } |
| 98 | |
| 99 | @Test(timeout = 5000) |
| 100 | public void unavailableIds() { |
| 101 | |
| 102 | ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 103 | |
| 104 | Callable<Object> acquireId = () -> idManager.getNewIdentifier(Integer.toString(2)); |
| 105 | |
| 106 | // fill the queue |
| 107 | for (int i = 2; i <= 255; i++) { |
| 108 | idManager.getNewIdentifier(Integer.toString(i)); |
| 109 | } |
| 110 | |
| 111 | // try to acquire an id |
| 112 | Future<Object> futureAcquire = executor.submit(acquireId); |
| 113 | |
| 114 | // wait for the threads to complete |
| 115 | RequestIdentifier id = null; |
| 116 | try { |
| 117 | id = (RequestIdentifier) futureAcquire.get(); |
| 118 | |
| 119 | // if we can't get the ID within 2 |
| 120 | // seconds we'll drop the packet and we'll retry |
| 121 | assertNull(id); |
| 122 | } catch (InterruptedException | ExecutionException ex) { |
| 123 | log.error("Something failed"); |
| 124 | assertNull(id); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Test(timeout = 5000) |
| 129 | public void availableIds() { |
| 130 | |
| 131 | ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 132 | |
| 133 | Callable<Object> acquireId = () -> idManager.getNewIdentifier(Integer.toString(2)); |
| 134 | |
| 135 | // fill the queue |
| 136 | for (int i = 2; i <= 255; i++) { |
| 137 | idManager.getNewIdentifier(Integer.toString(i)); |
| 138 | } |
| 139 | |
| 140 | // try to release an id |
| 141 | final RequestIdentifier id = new RequestIdentifier((byte) 2); |
| 142 | executor.submit(() -> idManager.releaseIdentifier(id)); |
| 143 | // try to acquire an id |
| 144 | Future<Object> futureAcquire = executor.submit(acquireId); |
| 145 | |
| 146 | // wait for the threads to complete |
| 147 | RequestIdentifier idGet = null; |
| 148 | try { |
| 149 | idGet = (RequestIdentifier) futureAcquire.get(); |
| 150 | assertNotNull(idGet); |
| 151 | } catch (InterruptedException | ExecutionException ex) { |
| 152 | log.error("Something failed"); |
| 153 | assertNull(idGet); |
| 154 | } |
| 155 | } |
Matteo Scandolo | e033c26 | 2020-10-14 11:37:39 -0700 | [diff] [blame] | 156 | } |