Skip to content

Commit

Permalink
Merge "[DatagramChannelMulticastTest] poll before receive"
Browse files Browse the repository at this point in the history
am: 0e6a2b6

Change-Id: Id7895f3f774eeeb8d241c05f59eb37664b1070d0
  • Loading branch information
kongy authored and android-build-merger committed Dec 2, 2016
2 parents 89674d6 + 0e6a2b6 commit 31c04b0
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import android.system.ErrnoException;
import android.system.StructPollfd;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand All @@ -36,6 +40,8 @@
import java.util.ArrayList;
import java.util.Enumeration;

import static android.system.OsConstants.POLLIN;

/**
* Tests associated with multicast behavior of DatagramChannel.
* These tests require IPv6 multicasting enabled network.
Expand Down Expand Up @@ -774,6 +780,7 @@ private void test_block_filtersAsExpected(
// Send a message. It should be received.
String msg1 = "Hello1";
sendMessage(sendingChannel, msg1, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
InetSocketAddress sourceAddress1 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertEquals(sourceAddress1, sendingAddress);
assertEquals(msg1, new String(receiveBuffer.array(), 0, receiveBuffer.position()));
Expand All @@ -784,6 +791,7 @@ private void test_block_filtersAsExpected(
// Send a message. It should be filtered.
String msg2 = "Hello2";
sendMessage(sendingChannel, msg2, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
receiveBuffer.position(0);
InetSocketAddress sourceAddress2 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertNull(sourceAddress2);
Expand All @@ -794,6 +802,7 @@ private void test_block_filtersAsExpected(
// Send a message. It should be received.
String msg3 = "Hello3";
sendMessage(sendingChannel, msg3, groupSocketAddress);
blockUntilAvailableOrTimeout(receivingChannel.socket(), 1000);
receiveBuffer.position(0);
InetSocketAddress sourceAddress3 = (InetSocketAddress) receivingChannel.receive(receiveBuffer);
assertEquals(sourceAddress3, sendingAddress);
Expand Down Expand Up @@ -1237,5 +1246,13 @@ private static InetAddress getLocalIpv6Address(NetworkInterface networkInterface
throw new AssertionFailedError("Unable to find local IPv6 address for " + networkInterface);
}

private static void blockUntilAvailableOrTimeout(DatagramSocket s, int timeout) {
try {
StructPollfd[] pollFds = new StructPollfd[]{ new StructPollfd() };
pollFds[0].fd = s.getFileDescriptor$();
pollFds[0].events = (short) POLLIN;
android.system.Os.poll(pollFds, timeout);
} catch (ErrnoException ignored) { }
}
}

0 comments on commit 31c04b0

Please sign in to comment.