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
This commit is contained in:
Hungming Chen
2021-01-12 18:55:14 +08:00
parent 12067258b2
commit 7d05e6fddd
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. * 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); Objects.requireNonNull(key);
return getNextKeyInternal(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. * Otherwise, iteration will result in undefined behaviour.
*/ */
public void forEach(BiConsumer<K, V> action) throws ErrnoException { public void forEach(BiConsumer<K, V> action) throws ErrnoException {
@Nullable @Nullable K nextKey = getFirstKey();
K nextKey = getFirstKey();
while (nextKey != null) { while (nextKey != null) {
@NonNull @NonNull final K curKey = nextKey;
final K curKey = nextKey; @NonNull final V value = getValue(curKey);
@NonNull
final V value = getValue(curKey);
nextKey = getNextKey(curKey); nextKey = getNextKey(curKey);
action.accept(curKey, value); action.accept(curKey, value);

View File

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