Follow-up CL to the change at aosp/1498277
1. Correct the API annotation.
2. The entry deletion failure causes the exception in cleanTestMap().
3. Use AtomicInteger to be the counter in the lambda.
Test: atest BpfMapTest
Change-Id: I4a56038881a38bda993ef5303b71f0e2a99f03d1
diff --git a/Tethering/src/com/android/networkstack/tethering/BpfMap.java b/Tethering/src/com/android/networkstack/tethering/BpfMap.java
index 9505709..69ad1b6 100644
--- a/Tethering/src/com/android/networkstack/tethering/BpfMap.java
+++ b/Tethering/src/com/android/networkstack/tethering/BpfMap.java
@@ -134,7 +134,7 @@
*
* 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 @@
* 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);
diff --git a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java
index 77c0961..1ddbaa9 100644
--- a/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java
+++ b/Tethering/tests/privileged/src/com/android/networkstack/tethering/BpfMapTest.java
@@ -43,6 +43,7 @@
import java.net.InetAddress;
import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicInteger;
@RunWith(AndroidJUnit4.class)
@@ -96,7 +97,9 @@
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 @@
@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 @@
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 @@
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());
}