Add NetIdManagerTest

Add a simple test to verify that IDs loop correctly, that they skip used
IDs correctly, and throw when there is no remaining ID.

Test: atest com.android.server.NetIdManagerTest
Change-Id: I4c9518c725156d743286e062fd2eec1423a0459e
This commit is contained in:
Remi NGUYEN VAN 2019-08-01 16:25:50 +09:00
commit df6248fe83

View file

@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.util.SparseBooleanArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
/**
* Class used to reserve and release net IDs.
@ -38,14 +39,25 @@ public class NetIdManager {
@GuardedBy("mNetIdInUse")
private int mLastNetId = MIN_NET_ID - 1;
private final int mMaxNetId;
public NetIdManager() {
this(MAX_NET_ID);
}
@VisibleForTesting
NetIdManager(int maxNetId) {
mMaxNetId = maxNetId;
}
/**
* Get the first netId that follows the provided lastId and is available.
*/
private static int getNextAvailableNetIdLocked(
private int getNextAvailableNetIdLocked(
int lastId, @NonNull SparseBooleanArray netIdInUse) {
int netId = lastId;
for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) {
netId = netId < MAX_NET_ID ? netId + 1 : MIN_NET_ID;
for (int i = MIN_NET_ID; i <= mMaxNetId; i++) {
netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID;
if (!netIdInUse.get(netId)) {
return netId;
}