Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c299df2b authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Automerger Merge Worker
Browse files

Merge "Test that IpClient does not leak fds on shutdown." am: e0ddb33c am: 735cfe80

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1403767

Change-Id: I31720af8e94ed564274c2ae97ec367720775ded1
parents 48682d4d 735cfe80
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -169,6 +169,7 @@ import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.IOException;
@@ -2654,4 +2655,38 @@ public abstract class IpClientIntegrationTestCommon {
        // due to the null V6ONLY_WAIT.
        assertIpMemoryStoreNetworkAttributes(TEST_LEASE_DURATION_S, currentTime, TEST_DEFAULT_MTU);
    }

    private static int getNumOpenFds() {
        return new File("/proc/" + Os.getpid() + "/fd").listFiles().length;
    }

    private void shutdownAndRecreateIpClient() throws Exception {
        mIpc.shutdown();
        awaitIpClientShutdown();
        mIpc = makeIpClient();
    }

    @Test
    public void testNoFdLeaks() throws Exception {
        // Shut down and restart IpClient once to ensure that any fds that are opened the first
        // time it runs do not cause the test to fail.
        doDualStackProvisioning();
        shutdownAndRecreateIpClient();

        // Unfortunately we cannot use a large number of iterations as it would make the test run
        // too slowly. On crosshatch-eng each iteration takes ~250ms.
        final int iterations = 10;
        final int before = getNumOpenFds();
        for (int i = 0; i < iterations; i++) {
            doDualStackProvisioning();
            shutdownAndRecreateIpClient();
            // The last time this loop runs, mIpc will be shut down in tearDown.
        }
        final int after = getNumOpenFds();

        // Check that the number of open fds is the same as before.
        // If this exact match becomes flaky, we could add some tolerance here (e.g., allow 2-3
        // extra fds), since it's likely that any leak would at least leak one FD per loop.
        assertEquals("Fd leak after " + iterations + " iterations: ", before, after);
    }
}