⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 283272 in webkit


Ignore:
Timestamp:
Sep 29, 2021, 4:02:43 PM (5 years ago)
Author:
commit-queue@webkit.org
Message:

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

Patch by Alex Christensen <achristensen@webkit.org> 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):

Location:
trunk/Tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r283270 r283272  
     12021-09-29  Alex Christensen  <achristensen@webkit.org>
     2
     3        Migrate _WKDownload tests from TCPServer to HTTPServer
     4        https://bugs.webkit.org/show_bug.cgi?id=230980
     5        <rdar://82100878>
     6
     7        Reviewed by Chris Dumez.
     8
     9        The former is very picky when it comes to numbers of TCP connections, and causes tests to time out when
     10        the number of connections changes.  The latter is more forgiving and runs code on the main thread.
     11
     12        * TestWebKitAPI/Tests/WebKitCocoa/Download.mm:
     13        (TestWebKitAPI::respondSlowly):
     14        (TestWebKitAPI::downloadAtRate):
     15        (TEST):
     16        * TestWebKitAPI/cocoa/HTTPServer.mm:
     17        (TestWebKitAPI::Connection::send const):
     18
    1192021-09-29  Matt Lewis  <jlewis3@apple.com>
    220
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Download.mm

    r278253 r283272  
    2727#import <WebKit/WKFoundation.h>
    2828
    29 #if PLATFORM(MAC) || PLATFORM(IOS)
    30 
    3129#import "HTTPServer.h"
    3230#import "PlatformUtilities.h"
    33 #import "TCPServer.h"
    3431#import "Test.h"
    3532#import "TestDownloadDelegate.h"
     
    859856namespace TestWebKitAPI {
    860857
    861 void respondSlowly(int socket, double kbps, bool& terminateServer)
    862 {
    863     EXPECT_FALSE(isMainThread());
    864     char readBuffer[1000];
    865     auto bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    866     EXPECT_GT(bytesRead, 0);
    867     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    868    
    869     const char* responseHeader =
    870     "HTTP/1.1 200 OK\r\n"
    871     "Content-Disposition: attachment; filename=\"filename.dat\"\r\n"
    872     "Content-Length: 100000000\r\n\r\n";
    873     auto bytesWritten = ::write(socket, responseHeader, strlen(responseHeader));
    874     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(responseHeader));
    875    
     858void respondSlowly(const Connection& connection, double kbps)
     859{
     860    EXPECT_TRUE(isMainThread());
     861
    876862    const double writesPerSecond = 100;
    877     Vector<char> writeBuffer(static_cast<size_t>(1024 * kbps / writesPerSecond));
    878     while (!terminateServer) {
    879         auto before = MonotonicTime::now();
    880         ::write(socket, writeBuffer.data(), writeBuffer.size());
     863    Vector<uint8_t> writeBuffer(static_cast<size_t>(1024 * kbps / writesPerSecond));
     864    auto before = MonotonicTime::now();
     865    connection.send(WTFMove(writeBuffer), [=] {
    881866        double writeDuration = (MonotonicTime::now() - before).seconds();
    882867        double desiredSleep = 1.0 / writesPerSecond;
    883868        if (writeDuration < desiredSleep)
    884869            usleep(USEC_PER_SEC * (desiredSleep - writeDuration));
    885     }
     870        respondSlowly(connection, kbps);
     871    });
    886872}
    887873
     
    911897void downloadAtRate(double desiredKbps, unsigned speedMultiplier, AppReturnsToForeground returnToForeground = AppReturnsToForeground::No)
    912898{
    913     bool terminateServer = false;
    914     TCPServer server([&](int socket) {
    915         respondSlowly(socket, desiredKbps, terminateServer);
     899    HTTPServer server([=](const Connection& connection) {
     900        connection.receiveHTTPRequest([=](Vector<char>&&) {
     901            const char* responseHeader =
     902            "HTTP/1.1 200 OK\r\n"
     903            "Content-Disposition: attachment; filename=\"filename.dat\"\r\n"
     904            "Content-Length: 100000000\r\n\r\n";
     905            connection.send(responseHeader, [=] {
     906                respondSlowly(connection, desiredKbps);
     907            });
     908        });
    916909    });
    917910   
     
    925918        [[webView configuration].websiteDataStore _synthesizeAppIsBackground:NO];
    926919    [monitorDelegate() waitForDidFail];
    927     terminateServer = true;
    928920    [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:destination.get() isDirectory:NO] error:nil];
    929921}
     
    11271119    using namespace TestWebKitAPI;
    11281120
    1129     std::atomic<bool> receivedFirstConnection { false };
    1130 
    1131     TCPServer server([&](int socket) {
    1132         if (!receivedFirstConnection.exchange(true)) {
    1133             TCPServer::read(socket);
    1134 
    1135             const char* responseHeader =
    1136             "HTTP/1.1 200 OK\r\n"
    1137             "ETag: test\r\n"
    1138             "Content-Length: 10000\r\n\r\n";
    1139             TCPServer::write(socket, responseHeader, strlen(responseHeader));
    1140 
    1141             char data[5000];
    1142             memset(data, 0, 5000);
    1143             TCPServer::write(socket, data, 5000);
    1144 
    1145             // Wait for the client to cancel the download before closing the connection.
    1146             Util::run(&isDone);
    1147         } else {
    1148             TCPServer::read(socket);
     1121    HTTPServer server([receivedFirstConnection = false] (Connection connection) mutable {
     1122        if (!std::exchange(receivedFirstConnection, true)) {
     1123            connection.receiveHTTPRequest([=](Vector<char>&&) {
     1124                const char* responseHeader =
     1125                "HTTP/1.1 200 OK\r\n"
     1126                "ETag: test\r\n"
     1127                "Content-Length: 10000\r\n\r\n";
     1128                connection.send(responseHeader, [=] {
     1129                    connection.send(Vector<uint8_t>(5000, 0));
     1130                });
     1131            });
     1132            return;
     1133        }
     1134        connection.receiveHTTPRequest([=](Vector<char>&&) {
    11491135            const char* challengeHeader =
    11501136            "HTTP/1.1 401 Unauthorized\r\n"
     
    11521138            "Content-Length: 0\r\n"
    11531139            "WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n";
    1154             TCPServer::write(socket, challengeHeader, strlen(challengeHeader));
    1155 
    1156             TCPServer::read(socket);
    1157 
    1158             const char* responseHeader =
    1159             "HTTP/1.1 206 Partial Content\r\n"
    1160             "ETag: test\r\n"
    1161             "Content-Range: bytes 5000-9999/10000\r\n"
    1162             "Content-Length: 5000\r\n\r\n";
    1163             TCPServer::write(socket, responseHeader, strlen(responseHeader));
    1164 
    1165             char data[5000];
    1166             memset(data, 1, 5000);
    1167             TCPServer::write(socket, data, 5000);
    1168         }
    1169     }, 2);
     1140            connection.send(challengeHeader, [=] {
     1141                connection.receiveHTTPRequest([=](Vector<char>&&) {
     1142                    const char* responseHeader =
     1143                    "HTTP/1.1 206 Partial Content\r\n"
     1144                    "ETag: test\r\n"
     1145                    "Content-Range: bytes 5000-9999/10000\r\n"
     1146                    "Content-Length: 5000\r\n\r\n";
     1147                    connection.send(responseHeader, [=] {
     1148                        connection.send(Vector<uint8_t>(5000, 1));
     1149                    });
     1150                });
     1151            });
     1152        });
     1153    });
    11701154
    11711155    auto processPool = adoptNS([[WKProcessPool alloc] init]);
    1172     auto websiteDataStore = adoptNS([WKWebsiteDataStore defaultDataStore]);
     1156    auto websiteDataStore = [WKWebsiteDataStore defaultDataStore];
    11731157
    11741158    auto delegate1 = adoptNS([[DownloadCancelingDelegate alloc] init]);
     
    11771161    isDone = false;
    11781162    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", server.port()]]];
    1179     [processPool _downloadURLRequest:request websiteDataStore:websiteDataStore.get() originatingWebView:nil];
     1163    [processPool _downloadURLRequest:request websiteDataStore:websiteDataStore originatingWebView:nil];
    11801164
    11811165    Util::run(&isDone);
     
    11841168    auto delegate2 = adoptNS([[AuthenticationChallengeHandlingDelegate alloc] init]);
    11851169    [processPool _setDownloadDelegate:delegate2.get()];
    1186     [processPool _resumeDownloadFromData:[delegate1 resumeData].get() websiteDataStore:websiteDataStore.get() path:[delegate1 path].get() originatingWebView:nil];
     1170    [processPool _resumeDownloadFromData:[delegate1 resumeData].get() websiteDataStore:websiteDataStore path:[delegate1 path].get() originatingWebView:nil];
    11871171
    11881172    Util::run(&isDone);
     
    26362620
    26372621}
    2638 
    2639 #endif // PLATFORM(MAC) || PLATFORM(IOS)
  • trunk/Tools/TestWebKitAPI/cocoa/HTTPServer.mm

    r282755 r283272  
    322322{
    323323    nw_connection_send(m_connection.get(), message.get(), NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT, true, makeBlockPtr([completionHandler = WTFMove(completionHandler)](nw_error_t error) mutable {
    324         ASSERT(!error);
    325324        if (completionHandler)
    326325            completionHandler();
Note: See TracChangeset for help on using the changeset viewer.