Add TapPacketReader#startAsyncForTest, poll
Add a startAsyncForTest method to attempt to start the TapPacketReader
on its handler thread. The method will not report failures to start
(especially failures to create the socket), so it is only added in
TapPacketReader for test code, and not FdEventsReader which would be in
production code.
Rename popPacket to poll, keeping the old version as deprecated
for compatibility. "pop" normally refers to removing a packet from the
top of a LIFO stack, so it is not appropriate naming here.
Test: m
Bug: 168868607
Change-Id: I19184aaca018165856d1d5e5d24b976ae75d1664
diff --git a/staticlibs/devicetests/com/android/testutils/TapPacketReader.java b/staticlibs/devicetests/com/android/testutils/TapPacketReader.java
index c36f144..001b709 100644
--- a/staticlibs/devicetests/com/android/testutils/TapPacketReader.java
+++ b/staticlibs/devicetests/com/android/testutils/TapPacketReader.java
@@ -50,6 +50,17 @@
mTapFd = tapFd;
}
+
+ /**
+ * Attempt to start the FdEventsReader on its handler thread.
+ *
+ * As opposed to {@link android.net.util.FdEventsReader#start()}, this method will not report
+ * failure to start, so it is only appropriate in tests that will fail later if that happens.
+ */
+ public void startAsyncForTest() {
+ getHandler().post(this::start);
+ }
+
@Override
protected FileDescriptor createFd() {
return mTapFd;
@@ -64,10 +75,30 @@
}
/**
+ * @deprecated This method does not actually "pop" (which generally means the last packet).
+ * Use {@link #poll(long)}, which has the same behavior, instead.
+ */
+ @Nullable
+ @Deprecated
+ public byte[] popPacket(long timeoutMs) {
+ return poll(timeoutMs);
+ }
+
+ /**
+ * @deprecated This method does not actually "pop" (which generally means the last packet).
+ * Use {@link #poll(long, Predicate)}, which has the same behavior, instead.
+ */
+ @Nullable
+ @Deprecated
+ public byte[] popPacket(long timeoutMs, @NonNull Predicate<byte[]> filter) {
+ return poll(timeoutMs, filter);
+ }
+
+ /**
* Get the next packet that was received on the interface.
*/
@Nullable
- public byte[] popPacket(long timeoutMs) {
+ public byte[] poll(long timeoutMs) {
return mReadHead.getValue().poll(timeoutMs, packet -> true);
}
@@ -75,7 +106,7 @@
* Get the next packet that was received on the interface and matches the specified filter.
*/
@Nullable
- public byte[] popPacket(long timeoutMs, @NonNull Predicate<byte[]> filter) {
+ public byte[] poll(long timeoutMs, @NonNull Predicate<byte[]> filter) {
return mReadHead.getValue().poll(timeoutMs, filter::test);
}