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
This commit is contained in:
Remi NGUYEN VAN
2020-09-23 15:13:40 +09:00
parent a60293a5e9
commit 3b1e9265eb

View File

@@ -50,6 +50,17 @@ public class TapPacketReader extends PacketReader {
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;
@@ -63,11 +74,31 @@ public class TapPacketReader extends PacketReader {
}
}
/**
* @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 @@ public class TapPacketReader extends PacketReader {
* 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);
}