Truly independent web browser
Tests/LibWebView: Stop the test HTTP servers without a wake connection
Shannon Booth3fb5840a1c0f
diff --git a/Tests/LibWebView/TestFileDownloader.cpp b/Tests/LibWebView/TestFileDownloader.cppindex e6ce1ea45c5..6da9a5cd0e8 100644--- a/Tests/LibWebView/TestFileDownloader.cpp+++ b/Tests/LibWebView/TestFileDownloader.cpp@@ -72,6 +72,8 @@ public:MUST(Core::System::bind(m_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)));MUST(Core::System::listen(m_socket, 16));+ // accept() must not block when a connection that poll() reported is gone by then.+ MUST(Core::System::fcntl(m_socket, F_SETFL, O_NONBLOCK));socklen_t address_size = sizeof(address);VERIFY(::getsockname(m_socket, reinterpret_cast<sockaddr*>(&address), &address_size) == 0);@@ -87,7 +89,6 @@ public:~TestHttpServer(){m_shutting_down.store(true);- wake_server();(void)m_thread->join();for (auto& thread : m_stalled_connection_threads)(void)thread->join();@@ -115,33 +116,31 @@ public:}private:+ // A connection made to wake accept() can be lost to a signal, so poll and recheck for shutdown instead.void run(){while (!m_shutting_down.load()) {+ pollfd poll_fd { .fd = m_socket, .events = POLLIN, .revents = 0 };+ auto ready = Core::System::poll({ &poll_fd, 1 }, 100);+ if (ready.is_error() && ready.error().code() != EINTR)+ return;+ if (ready.is_error() || ready.value() == 0)+ continue;+auto client = ::accept(m_socket, nullptr, nullptr);- if (client < 0)+ if (client < 0) {+ if (errno == EAGAIN || errno == ECONNABORTED)+ continue;return;+ }+ // Accepted sockets inherit O_NONBLOCK on some platforms.+ MUST(Core::System::fcntl(client, F_SETFL, 0));if (handle_connection(client))(void)Core::System::close(client);}}- void wake_server()- {- auto socket = Core::System::socket(AF_INET, SOCK_STREAM, 0);- if (socket.is_error())- return;-- sockaddr_in address {};- address.sin_family = AF_INET;- address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);- address.sin_port = htons(m_port);-- (void)::connect(socket.value(), reinterpret_cast<sockaddr*>(&address), sizeof(address));- (void)Core::System::close(socket.value());- }-// Returns whether the caller still owns the client socket and should close it.bool handle_connection(int client){diff --git a/Tests/LibWebView/TestNavigationDownloads.cpp b/Tests/LibWebView/TestNavigationDownloads.cppindex 3468e3f50cc..890518f7c64 100644--- a/Tests/LibWebView/TestNavigationDownloads.cpp+++ b/Tests/LibWebView/TestNavigationDownloads.cpp@@ -46,6 +46,8 @@ public:address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);MUST(Core::System::bind(m_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)));MUST(Core::System::listen(m_socket, 16));+ // accept() must not block when a connection that poll() reported is gone by then.+ MUST(Core::System::fcntl(m_socket, F_SETFL, O_NONBLOCK));socklen_t address_size = sizeof(address);VERIFY(::getsockname(m_socket, reinterpret_cast<sockaddr*>(&address), &address_size) == 0);@@ -61,7 +63,6 @@ public:~TestHttpServer(){m_shutting_down.store(true);- wake_server();(void)m_thread->join();(void)Core::System::close(m_socket);}@@ -80,31 +81,30 @@ public:size_t request_count() const { return m_request_count.load(); }private:+ // A connection made to wake accept() can be lost to a signal, so poll and recheck for shutdown instead.void run(){while (!m_shutting_down.load()) {+ pollfd poll_fd { .fd = m_socket, .events = POLLIN, .revents = 0 };+ auto ready = Core::System::poll({ &poll_fd, 1 }, 100);+ if (ready.is_error() && ready.error().code() != EINTR)+ return;+ if (ready.is_error() || ready.value() == 0)+ continue;+auto client = ::accept(m_socket, nullptr, nullptr);- if (client < 0)+ if (client < 0) {+ if (errno == EAGAIN || errno == ECONNABORTED)+ continue;return;+ }+ // Accepted sockets inherit O_NONBLOCK on some platforms.+ MUST(Core::System::fcntl(client, F_SETFL, 0));respond(client);(void)Core::System::close(client);}}- void wake_server()- {- auto socket = Core::System::socket(AF_INET, SOCK_STREAM, 0);- if (socket.is_error())- return;-- sockaddr_in address {};- address.sin_family = AF_INET;- address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);- address.sin_port = htons(m_port);- (void)::connect(socket.value(), reinterpret_cast<sockaddr*>(&address), sizeof(address));- (void)Core::System::close(socket.value());- }-void respond(int client){StringBuilder request;diff --git a/Tests/LibWebView/TestRendererViolationReports.cpp b/Tests/LibWebView/TestRendererViolationReports.cppindex 31a530a3564..bad3ef92896 100644--- a/Tests/LibWebView/TestRendererViolationReports.cpp+++ b/Tests/LibWebView/TestRendererViolationReports.cpp@@ -54,6 +54,8 @@ public:address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);MUST(Core::System::bind(m_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)));MUST(Core::System::listen(m_socket, 16));+ // accept() must not block when a connection that poll() reported is gone by then.+ MUST(Core::System::fcntl(m_socket, F_SETFL, O_NONBLOCK));socklen_t address_size = sizeof(address);VERIFY(::getsockname(m_socket, reinterpret_cast<sockaddr*>(&address), &address_size) == 0);@@ -69,7 +71,6 @@ public:~TestHttpServer(){m_shutting_down.store(true);- wake_server();(void)m_thread->join();for (auto client : m_held_clients)(void)Core::System::close(client);@@ -85,12 +86,25 @@ public:size_t report_barriers() const { return m_report_barriers.load(); }private:+ // A connection made to wake accept() can be lost to a signal, so poll and recheck for shutdown instead.void run(){while (!m_shutting_down.load()) {+ pollfd poll_fd { .fd = m_socket, .events = POLLIN, .revents = 0 };+ auto ready = Core::System::poll({ &poll_fd, 1 }, 100);+ if (ready.is_error() && ready.error().code() != EINTR)+ return;+ if (ready.is_error() || ready.value() == 0)+ continue;+auto client = ::accept(m_socket, nullptr, nullptr);- if (client < 0)+ if (client < 0) {+ if (errno == EAGAIN || errno == ECONNABORTED)+ continue;return;+ }+ // Accepted sockets inherit O_NONBLOCK on some platforms.+ MUST(Core::System::fcntl(client, F_SETFL, 0));if (respond(client))m_held_clients.append(client);else@@ -98,20 +112,6 @@ private:}}- void wake_server()- {- auto socket = Core::System::socket(AF_INET, SOCK_STREAM, 0);- if (socket.is_error())- return;-- sockaddr_in address {};- address.sin_family = AF_INET;- address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);- address.sin_port = htons(m_port);- (void)::connect(socket.value(), reinterpret_cast<sockaddr*>(&address), sizeof(address));- (void)Core::System::close(socket.value());- }-// Returns whether the request is held, unanswered.bool respond(int client){