diff options
author | Benedict Wong <benedictwong@google.com> | 2019-03-20 09:44:09 -0700 |
---|---|---|
committer | Benedict Wong <benedictwong@google.com> | 2019-04-03 17:52:03 -0700 |
commit | abcc6c082193053ea403921d3129f426ad3f5c3e (patch) | |
tree | db0ee241e47521f47b496ec0ab45ca8e6af1da33 /tests/net/java/com/android/server/IpSecServiceTest.java | |
parent | 88e2a9aaee134178d6b7edbc479e87a3f6b1b665 (diff) |
Fix flaky UdpEncapsulationSocket test
This commit reduces the flakiness of the
testOpenAndCloseUdpEncapsulationSocket by retrying up to three times.
Unfortunately, testing port-selected socket creation is racy against
other applications. This helps to handle the same race condition as done
in IpSecService#bindToRandomPort
Bug: 128024100
Test: 200x runs of testOpenAndCloseUdpEncapsulationSocket
Change-Id: I7e036ce821019dbac6c50899bd0894e89d2fe82a
Diffstat (limited to 'tests/net/java/com/android/server/IpSecServiceTest.java')
-rw-r--r-- | tests/net/java/com/android/server/IpSecServiceTest.java | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/tests/net/java/com/android/server/IpSecServiceTest.java b/tests/net/java/com/android/server/IpSecServiceTest.java index b5c3e9287467..4a35015044ff 100644 --- a/tests/net/java/com/android/server/IpSecServiceTest.java +++ b/tests/net/java/com/android/server/IpSecServiceTest.java @@ -156,10 +156,21 @@ public class IpSecServiceTest { @Test public void testOpenAndCloseUdpEncapsulationSocket() throws Exception { - int localport = findUnusedPort(); + int localport = -1; + IpSecUdpEncapResponse udpEncapResp = null; + + for (int i = 0; i < IpSecService.MAX_PORT_BIND_ATTEMPTS; i++) { + localport = findUnusedPort(); + + udpEncapResp = mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); + assertNotNull(udpEncapResp); + if (udpEncapResp.status == IpSecManager.Status.OK) { + break; + } + + // Else retry to reduce possibility for port-bind failures. + } - IpSecUdpEncapResponse udpEncapResp = - mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); assertNotNull(udpEncapResp); assertEquals(IpSecManager.Status.OK, udpEncapResp.status); assertEquals(localport, udpEncapResp.port); @@ -204,12 +215,11 @@ public class IpSecServiceTest { @Test public void testOpenUdpEncapsulationSocketAfterClose() throws Exception { - int localport = findUnusedPort(); IpSecUdpEncapResponse udpEncapResp = - mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); + mIpSecService.openUdpEncapsulationSocket(0, new Binder()); assertNotNull(udpEncapResp); assertEquals(IpSecManager.Status.OK, udpEncapResp.status); - assertEquals(localport, udpEncapResp.port); + int localport = udpEncapResp.port; mIpSecService.closeUdpEncapsulationSocket(udpEncapResp.resourceId); udpEncapResp.fileDescriptor.close(); @@ -226,12 +236,11 @@ public class IpSecServiceTest { */ @Test public void testUdpEncapPortNotReleased() throws Exception { - int localport = findUnusedPort(); IpSecUdpEncapResponse udpEncapResp = - mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); + mIpSecService.openUdpEncapsulationSocket(0, new Binder()); assertNotNull(udpEncapResp); assertEquals(IpSecManager.Status.OK, udpEncapResp.status); - assertEquals(localport, udpEncapResp.port); + int localport = udpEncapResp.port; udpEncapResp.fileDescriptor.close(); @@ -273,14 +282,11 @@ public class IpSecServiceTest { @Test public void testOpenUdpEncapsulationSocketTwice() throws Exception { - int localport = findUnusedPort(); - IpSecUdpEncapResponse udpEncapResp = - mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); + mIpSecService.openUdpEncapsulationSocket(0, new Binder()); assertNotNull(udpEncapResp); assertEquals(IpSecManager.Status.OK, udpEncapResp.status); - assertEquals(localport, udpEncapResp.port); - mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); + int localport = udpEncapResp.port; IpSecUdpEncapResponse testUdpEncapResp = mIpSecService.openUdpEncapsulationSocket(localport, new Binder()); |