Follow-up CL to the change at aosp/1498277 am: 7d05e6fddd am: 0276690274

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1547695

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ibef2b8d76779536b28c42d421c24516d94cee826
This commit is contained in:
Hungming Chen
2021-01-12 14:59:23 +00:00
committed by Automerger Merge Worker
2 changed files with 17 additions and 22 deletions

View File

@@ -134,7 +134,7 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
*
* TODO: consider allowing null passed-in key.
*/
public K getNextKey(@Nullable K key) throws ErrnoException {
public K getNextKey(@NonNull K key) throws ErrnoException {
Objects.requireNonNull(key);
return getNextKeyInternal(key);
}
@@ -185,14 +185,11 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
* Otherwise, iteration will result in undefined behaviour.
*/
public void forEach(BiConsumer<K, V> action) throws ErrnoException {
@Nullable
K nextKey = getFirstKey();
@Nullable K nextKey = getFirstKey();
while (nextKey != null) {
@NonNull
final K curKey = nextKey;
@NonNull
final V value = getValue(curKey);
@NonNull final K curKey = nextKey;
@NonNull final V value = getValue(curKey);
nextKey = getNextKey(curKey);
action.accept(curKey, value);

View File

@@ -43,6 +43,7 @@ import org.junit.runner.RunWith;
import java.net.InetAddress;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
@RunWith(AndroidJUnit4.class)
@@ -96,7 +97,9 @@ public final class BpfMapTest {
bpfMap.forEach((key, value) -> {
try {
assertTrue(bpfMap.deleteEntry(key));
} catch (ErrnoException ignored) { }
} catch (ErrnoException e) {
fail("Fail to delete the key " + key + ": " + e);
}
});
assertNull(bpfMap.getFirstKey());
}
@@ -284,15 +287,11 @@ public final class BpfMapTest {
@Test
public void testIterateEmptyMap() throws Exception {
try (BpfMap<TetherIngressKey, TetherIngressValue> bpfMap = getTestMap()) {
// Use an one element array to be a trick for a counter because local variables
// referenced from a lambda expression must be final.
final int[] count = {0};
bpfMap.forEach((key, value) -> {
count[0]++;
});
// Expect that consumer has never be called.
assertEquals(0, count[0]);
// Can't use an int because variables used in a lambda must be final.
final AtomicInteger count = new AtomicInteger();
bpfMap.forEach((key, value) -> count.incrementAndGet());
// Expect that the consumer was never called.
assertEquals(0, count.get());
}
}
@@ -306,9 +305,8 @@ public final class BpfMapTest {
bpfMap.insertEntry(resultMap.keyAt(i), resultMap.valueAt(i));
}
// Use an one element array to be a trick for a counter because local variables
// referenced from a lambda expression must be final.
final int[] count = {0};
// Can't use an int because variables used in a lambda must be final.
final AtomicInteger count = new AtomicInteger();
bpfMap.forEach((key, value) -> {
try {
assertTrue(bpfMap.deleteEntry(key));
@@ -318,9 +316,9 @@ public final class BpfMapTest {
if (!value.equals(resultMap.remove(key))) {
fail("Unexpected result: " + key + ", value: " + value);
}
count[0]++;
count.incrementAndGet();
});
assertEquals(3, count[0]);
assertEquals(3, count.get());
assertTrue(resultMap.isEmpty());
assertNull(bpfMap.getFirstKey());
}