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:
@@ -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);
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user