Title: [283272] trunk/Tools
Revision
283272
Author
[email protected]
Date
2021-09-29 16:02:43 -0700 (Wed, 29 Sep 2021)

Log Message

Migrate _WKDownload tests from TCPServer to HTTPServer
https://bugs.webkit.org/show_bug.cgi?id=230980
<rdar://82100878>

Patch by Alex Christensen <[email protected]> on 2021-09-29
Reviewed by Chris Dumez.

The former is very picky when it comes to numbers of TCP connections, and causes tests to time out when
the number of connections changes.  The latter is more forgiving and runs code on the main thread.

* TestWebKitAPI/Tests/WebKitCocoa/Download.mm:
(TestWebKitAPI::respondSlowly):
(TestWebKitAPI::downloadAtRate):
(TEST):
* TestWebKitAPI/cocoa/HTTPServer.mm:
(TestWebKitAPI::Connection::send const):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (283271 => 283272)


--- trunk/Tools/ChangeLog	2021-09-29 22:45:23 UTC (rev 283271)
+++ trunk/Tools/ChangeLog	2021-09-29 23:02:43 UTC (rev 283272)
@@ -1,3 +1,21 @@
+2021-09-29  Alex Christensen  <[email protected]>
+
+        Migrate _WKDownload tests from TCPServer to HTTPServer
+        https://bugs.webkit.org/show_bug.cgi?id=230980
+        <rdar://82100878>
+
+        Reviewed by Chris Dumez.
+
+        The former is very picky when it comes to numbers of TCP connections, and causes tests to time out when
+        the number of connections changes.  The latter is more forgiving and runs code on the main thread.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/Download.mm:
+        (TestWebKitAPI::respondSlowly):
+        (TestWebKitAPI::downloadAtRate):
+        (TEST):
+        * TestWebKitAPI/cocoa/HTTPServer.mm:
+        (TestWebKitAPI::Connection::send const):
+
 2021-09-29  Matt Lewis  <[email protected]>
 
         Unreviewed built trigger fix.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Download.mm (283271 => 283272)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Download.mm	2021-09-29 22:45:23 UTC (rev 283271)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Download.mm	2021-09-29 23:02:43 UTC (rev 283272)
@@ -26,11 +26,8 @@
 #import "config.h"
 #import <WebKit/WKFoundation.h>
 
-#if PLATFORM(MAC) || PLATFORM(IOS)
-
 #import "HTTPServer.h"
 #import "PlatformUtilities.h"
-#import "TCPServer.h"
 #import "Test.h"
 #import "TestDownloadDelegate.h"
 #import "TestLegacyDownloadDelegate.h"
@@ -858,31 +855,20 @@
 
 namespace TestWebKitAPI {
 
-void respondSlowly(int socket, double kbps, bool& terminateServer)
+void respondSlowly(const Connection& connection, double kbps)
 {
-    EXPECT_FALSE(isMainThread());
-    char readBuffer[1000];
-    auto bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
-    EXPECT_GT(bytesRead, 0);
-    EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
-    
-    const char* responseHeader =
-    "HTTP/1.1 200 OK\r\n"
-    "Content-Disposition: attachment; filename=\"filename.dat\"\r\n"
-    "Content-Length: 100000000\r\n\r\n";
-    auto bytesWritten = ::write(socket, responseHeader, strlen(responseHeader));
-    EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(responseHeader));
-    
+    EXPECT_TRUE(isMainThread());
+
     const double writesPerSecond = 100;
-    Vector<char> writeBuffer(static_cast<size_t>(1024 * kbps / writesPerSecond));
-    while (!terminateServer) {
-        auto before = MonotonicTime::now();
-        ::write(socket, writeBuffer.data(), writeBuffer.size());
+    Vector<uint8_t> writeBuffer(static_cast<size_t>(1024 * kbps / writesPerSecond));
+    auto before = MonotonicTime::now();
+    connection.send(WTFMove(writeBuffer), [=] {
         double writeDuration = (MonotonicTime::now() - before).seconds();
         double desiredSleep = 1.0 / writesPerSecond;
         if (writeDuration < desiredSleep)
             usleep(USEC_PER_SEC * (desiredSleep - writeDuration));
-    }
+        respondSlowly(connection, kbps);
+    });
 }
 
 static RetainPtr<DownloadMonitorTestDelegate> monitorDelegate()
@@ -910,9 +896,16 @@
     
 void downloadAtRate(double desiredKbps, unsigned speedMultiplier, AppReturnsToForeground returnToForeground = AppReturnsToForeground::No)
 {
-    bool terminateServer = false;
-    TCPServer server([&](int socket) {
-        respondSlowly(socket, desiredKbps, terminateServer);
+    HTTPServer server([=](const Connection& connection) {
+        connection.receiveHTTPRequest([=](Vector<char>&&) {
+            const char* responseHeader =
+            "HTTP/1.1 200 OK\r\n"
+            "Content-Disposition: attachment; filename=\"filename.dat\"\r\n"
+            "Content-Length: 100000000\r\n\r\n";
+            connection.send(responseHeader, [=] {
+                respondSlowly(connection, desiredKbps);
+            });
+        });
     });
     
     auto webView = webViewWithDownloadMonitorSpeedMultiplier(speedMultiplier);
@@ -924,7 +917,6 @@
     if (returnToForeground == AppReturnsToForeground::Yes)
         [[webView configuration].websiteDataStore _synthesizeAppIsBackground:NO];
     [monitorDelegate() waitForDidFail];
-    terminateServer = true;
     [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:destination.get() isDirectory:NO] error:nil];
 }
 
@@ -1126,50 +1118,42 @@
 {
     using namespace TestWebKitAPI;
 
-    std::atomic<bool> receivedFirstConnection { false };
-
-    TCPServer server([&](int socket) {
-        if (!receivedFirstConnection.exchange(true)) {
-            TCPServer::read(socket);
-
-            const char* responseHeader =
-            "HTTP/1.1 200 OK\r\n"
-            "ETag: test\r\n"
-            "Content-Length: 10000\r\n\r\n";
-            TCPServer::write(socket, responseHeader, strlen(responseHeader));
-
-            char data[5000];
-            memset(data, 0, 5000);
-            TCPServer::write(socket, data, 5000);
-
-            // Wait for the client to cancel the download before closing the connection.
-            Util::run(&isDone);
-        } else {
-            TCPServer::read(socket);
+    HTTPServer server([receivedFirstConnection = false] (Connection connection) mutable {
+        if (!std::exchange(receivedFirstConnection, true)) {
+            connection.receiveHTTPRequest([=](Vector<char>&&) {
+                const char* responseHeader =
+                "HTTP/1.1 200 OK\r\n"
+                "ETag: test\r\n"
+                "Content-Length: 10000\r\n\r\n";
+                connection.send(responseHeader, [=] {
+                    connection.send(Vector<uint8_t>(5000, 0));
+                });
+            });
+            return;
+        }
+        connection.receiveHTTPRequest([=](Vector<char>&&) {
             const char* challengeHeader =
             "HTTP/1.1 401 Unauthorized\r\n"
             "Date: Sat, 23 Mar 2019 06:29:01 GMT\r\n"
             "Content-Length: 0\r\n"
             "WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n";
-            TCPServer::write(socket, challengeHeader, strlen(challengeHeader));
+            connection.send(challengeHeader, [=] {
+                connection.receiveHTTPRequest([=](Vector<char>&&) {
+                    const char* responseHeader =
+                    "HTTP/1.1 206 Partial Content\r\n"
+                    "ETag: test\r\n"
+                    "Content-Range: bytes 5000-9999/10000\r\n"
+                    "Content-Length: 5000\r\n\r\n";
+                    connection.send(responseHeader, [=] {
+                        connection.send(Vector<uint8_t>(5000, 1));
+                    });
+                });
+            });
+        });
+    });
 
-            TCPServer::read(socket);
-
-            const char* responseHeader =
-            "HTTP/1.1 206 Partial Content\r\n"
-            "ETag: test\r\n"
-            "Content-Range: bytes 5000-9999/10000\r\n"
-            "Content-Length: 5000\r\n\r\n";
-            TCPServer::write(socket, responseHeader, strlen(responseHeader));
-
-            char data[5000];
-            memset(data, 1, 5000);
-            TCPServer::write(socket, data, 5000);
-        }
-    }, 2);
-
     auto processPool = adoptNS([[WKProcessPool alloc] init]);
-    auto websiteDataStore = adoptNS([WKWebsiteDataStore defaultDataStore]);
+    auto websiteDataStore = [WKWebsiteDataStore defaultDataStore];
 
     auto delegate1 = adoptNS([[DownloadCancelingDelegate alloc] init]);
     [processPool _setDownloadDelegate:delegate1.get()];
@@ -1176,7 +1160,7 @@
 
     isDone = false;
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", server.port()]]];
-    [processPool _downloadURLRequest:request websiteDataStore:websiteDataStore.get() originatingWebView:nil];
+    [processPool _downloadURLRequest:request websiteDataStore:websiteDataStore originatingWebView:nil];
 
     Util::run(&isDone);
 
@@ -1183,7 +1167,7 @@
     isDone = false;
     auto delegate2 = adoptNS([[AuthenticationChallengeHandlingDelegate alloc] init]);
     [processPool _setDownloadDelegate:delegate2.get()];
-    [processPool _resumeDownloadFromData:[delegate1 resumeData].get() websiteDataStore:websiteDataStore.get() path:[delegate1 path].get() originatingWebView:nil];
+    [processPool _resumeDownloadFromData:[delegate1 resumeData].get() websiteDataStore:websiteDataStore path:[delegate1 path].get() originatingWebView:nil];
 
     Util::run(&isDone);
 }
@@ -2635,5 +2619,3 @@
 }
 
 }
-
-#endif // PLATFORM(MAC) || PLATFORM(IOS)

Modified: trunk/Tools/TestWebKitAPI/cocoa/HTTPServer.mm (283271 => 283272)


--- trunk/Tools/TestWebKitAPI/cocoa/HTTPServer.mm	2021-09-29 22:45:23 UTC (rev 283271)
+++ trunk/Tools/TestWebKitAPI/cocoa/HTTPServer.mm	2021-09-29 23:02:43 UTC (rev 283272)
@@ -321,7 +321,6 @@
 void Connection::send(RetainPtr<dispatch_data_t>&& message, CompletionHandler<void()>&& completionHandler) const
 {
     nw_connection_send(m_connection.get(), message.get(), NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, true, makeBlockPtr([completionHandler = WTFMove(completionHandler)](nw_error_t error) mutable {
-        ASSERT(!error);
         if (completionHandler)
             completionHandler();
     }).get());
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to