• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 com.android.server
18 
19 import java.util.concurrent.atomic.AtomicInteger
20 
21 /**
22  * A [NetIdManager] that generates ID starting from [NetIdManager.MAX_NET_ID] and decreasing, rather
23  * than starting from [NetIdManager.MIN_NET_ID] and increasing.
24  *
25  * Useful for testing ConnectivityService, to minimize the risk of test ConnectivityService netIDs
26  * overlapping with netIDs used by the real ConnectivityService on the device.
27  *
28  * IDs may still overlap if many networks have been used on the device (so the "real" netIDs
29  * are close to MAX_NET_ID), but this is typically not the case when running unit tests. Also, there
30  * is no way to fully solve the overlap issue without altering ID allocation in non-test code, as
31  * the real ConnectivityService could start using netIds that have been used by the test in the
32  * past.
33  */
34 class TestNetIdManager : NetIdManager() {
35     private val nextId = AtomicInteger(MAX_NET_ID)
reserveNetIdnull36     override fun reserveNetId() = nextId.decrementAndGet()
37     override fun releaseNetId(id: Int) = Unit
38     fun peekNextNetId() = nextId.get() - 1
39 }
40