Reduce length of test data in UdpDataSourceContractTest

PiperOrigin-RevId: 348011243
This commit is contained in:
christosts 2020-12-17 14:33:56 +00:00 committed by Oliver Woodman
parent 9d2855c31f
commit 6b43f1566f

View File

@ -43,11 +43,7 @@ public class UdpDataSourceContractTest extends DataSourceContractTest {
@Before @Before
public void setUp() { public void setUp() {
udpDataSource = new UdpDataSource(); udpDataSource = new UdpDataSource();
// UDP is unreliable: it may lose, duplicate or re-order packets. We want to transmit more than data = TestUtil.buildTestData(/* length= */ 256);
// one UDP packets to thoroughly test the UDP data source. We assume that UDP delivery within
// the same host is reliable.
int dataLength = (10 * 1024) + 512; // 10.5 KiB, not a round number by intention
data = TestUtil.buildTestData(dataLength);
PacketTrasmitterTransferListener transferListener = new PacketTrasmitterTransferListener(data); PacketTrasmitterTransferListener transferListener = new PacketTrasmitterTransferListener(data);
udpDataSource.addTransferListener(transferListener); udpDataSource.addTransferListener(transferListener);
} }
@ -116,9 +112,11 @@ public class UdpDataSourceContractTest extends DataSourceContractTest {
String host = dataSpec.uri.getHost(); String host = dataSpec.uri.getHost();
int port = dataSpec.uri.getPort(); int port = dataSpec.uri.getPort();
try (DatagramSocket socket = new DatagramSocket()) { try (DatagramSocket socket = new DatagramSocket()) {
// Split data in packets of up to 1024 bytes. // Split data in packets of up to 64 bytes: UDP is unreliable, it may lose, duplicate or
for (int offset = 0; offset < data.length; offset += 1024) { // re-order packets. However, we want to transmit more than one UDP packets to thoroughly
int packetLength = min(1024, data.length - offset); // test the UDP data source. We assume that UDP delivery within the same host is reliable.
for (int offset = 0; offset < data.length; offset += 64) {
int packetLength = min(64, data.length - offset);
DatagramPacket packet = DatagramPacket packet =
new DatagramPacket(data, offset, packetLength, InetAddress.getByName(host), port); new DatagramPacket(data, offset, packetLength, InetAddress.getByName(host), port);
socket.send(packet); socket.send(packet);