blob: afc5ec2774e9571ffb743171e5ce51494c840860 [file] [log] [blame]
Matteo Scandoloe033c262020-10-14 11:37:39 -07001/*
2 * Copyright 2017-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 */
17package org.opencord.aaa.impl;
18
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070022import org.junit.runner.RunWith;
23import org.junit.runners.Parameterized;
Matteo Scandoloe033c262020-10-14 11:37:39 -070024import org.slf4j.Logger;
25
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070026import java.util.concurrent.Callable;
27import java.util.concurrent.ExecutionException;
28import java.util.concurrent.ExecutorService;
29import java.util.concurrent.Executors;
30import java.util.concurrent.Future;
31import java.util.concurrent.TimeUnit;
32
33import static org.junit.Assert.*;
Matteo Scandoloe033c262020-10-14 11:37:39 -070034import static org.slf4j.LoggerFactory.getLogger;
35
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070036@RunWith(Parameterized.class)
Matteo Scandoloe033c262020-10-14 11:37:39 -070037public class IdentifierManagerTest {
38
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070039 // 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 Scandoloe033c262020-10-14 11:37:39 -070045 IdentifierManager idManager = null;
46 private final Logger log = getLogger(getClass());
47
48 @Before
49 public void setUp() {
50 System.out.print("Set up");
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070051 idManager.timeout = 1500;
52 idManager.pruningInterval = 1;
53 idManager.pollTimeout = 1;
Matteo Scandoloe033c262020-10-14 11:37:39 -070054 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));
70 log.trace("Id: {}", id.identifier() & 0xff);
71 assertNotEquals(id.identifier(), 0);
72 assertNotEquals(id.identifier(), 1);
73 idManager.releaseIdentifier(id);
74 }
75 }
Matteo Scandolobbc1ffb2020-10-16 15:56:20 -070076
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 Scandoloe033c262020-10-14 11:37:39 -0700156}