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

Commit b585d8b0 authored by Mike Yu's avatar Mike Yu
Browse files

Remove the waiting time when restart test DoH servers

We used to wait for 100 milliseconds before restart test DoH
servers in order to avoid binding a socket address that hasn't
be released. With this change, test DoH servers can try to bind
the socket address again, so the 100-milliseconds waiting time
can be removed.

Bug: 215818810
Test: wrote and ran a test that calls doh.stopServer() and then
      doh.startServer() 5000 times
Change-Id: I89acfd0997beeca0eb53139919a56931b2e79986
parent adc98bd9
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -233,7 +233,7 @@ impl DohFrontend {
        backend_socket.connect(self.backend_socket_addr)?;
        backend_socket.set_nonblocking(true)?;

        let frontend_socket = std::net::UdpSocket::bind(self.listen_socket_addr)?;
        let frontend_socket = bind_udp_socket_retry(self.listen_socket_addr)?;
        frontend_socket.set_nonblocking(true)?;

        let clients = ClientMap::new(create_quiche_config(
@@ -473,3 +473,18 @@ fn build_pipe() -> Result<(File, File)> {
    }
    Err(anyhow::Error::new(std::io::Error::last_os_error()).context("build_pipe failed"))
}

// Can retry to bind the socket address if it is in use.
fn bind_udp_socket_retry(addr: std::net::SocketAddr) -> Result<std::net::UdpSocket> {
    for _ in 0..3 {
        match std::net::UdpSocket::bind(addr) {
            Ok(socket) => return Ok(socket),
            Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {
                warn!("Binding socket address {} that is in use. Try again", addr);
                std::thread::sleep(Duration::from_millis(50));
            }
            Err(e) => return Err(anyhow::anyhow!(e)),
        }
    }
    Err(anyhow::anyhow!(std::io::Error::last_os_error()))
}
+0 −14
Original line number Diff line number Diff line
@@ -791,9 +791,6 @@ TEST_F(PrivateDnsDohTest, ExcessDnsRequests) {
    const int initial_max_idle_timeout_ms = 2000;
    ASSERT_TRUE(doh.stopServer());
    EXPECT_TRUE(doh.setMaxIdleTimeout(initial_max_idle_timeout_ms));
    // Sleep a while to avoid binding socket failed.
    // TODO: Make DohFrontend retry binding sockets.
    sleep_for(milliseconds(100));
    ASSERT_TRUE(doh.startServer());

    auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
@@ -886,9 +883,6 @@ TEST_F(PrivateDnsDohTest, RunOutOfDataLimit) {

    ASSERT_TRUE(doh.stopServer());
    EXPECT_TRUE(doh.setMaxBufferSize(initial_max_data));
    // Sleep a while to avoid binding socket failed.
    // TODO: Make DohFrontend retry binding sockets.
    sleep_for(milliseconds(100));
    ASSERT_TRUE(doh.startServer());

    const auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
@@ -938,8 +932,6 @@ TEST_F(PrivateDnsDohTest, RunOutOfStreams) {

    ASSERT_TRUE(doh.stopServer());
    EXPECT_TRUE(doh.setMaxStreamsBidi(initial_max_streams_bidi));
    // Sleep a while to avoid binding socket failed.
    sleep_for(milliseconds(100));
    ASSERT_TRUE(doh.startServer());

    const auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
@@ -978,8 +970,6 @@ TEST_F(PrivateDnsDohTest, ReconnectAfterIdleTimeout) {

    ASSERT_TRUE(doh.stopServer());
    EXPECT_TRUE(doh.setMaxIdleTimeout(initial_max_idle_timeout_ms));
    // Sleep a while to avoid binding socket failed.
    sleep_for(milliseconds(100));
    ASSERT_TRUE(doh.startServer());

    const auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
@@ -1062,8 +1052,6 @@ TEST_F(PrivateDnsDohTest, SessionResumption) {

        ASSERT_TRUE(doh.stopServer());
        EXPECT_TRUE(doh.setMaxIdleTimeout(initial_max_idle_timeout_ms));
        // Sleep a while to avoid binding socket failed.
        sleep_for(milliseconds(100));
        ASSERT_TRUE(doh.startServer());

        const auto parcel = DnsResponderClient::GetDefaultResolverParamsParcel();
@@ -1112,8 +1100,6 @@ TEST_F(PrivateDnsDohTest, RemoteConnectionClosed) {
    // Make the server close the connection. This will also reset the stats, so the doh query
    // count below is still 2 rather than 4.
    ASSERT_TRUE(doh.stopServer());
    // Sleep a while to avoid binding socket failed.
    sleep_for(milliseconds(100));
    ASSERT_TRUE(doh.startServer());

    EXPECT_NO_FAILURE(sendQueryAndCheckResult());