Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: components/cronet/ios/test/cronet_http_test.mm

Issue 2776583004: Use 64kb read buffer in CRNHttpProtocolHandler on iOS. (Closed)
Patch Set: Address Andrei's comments, use RawHttpResponse. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cronet/Cronet.h> 5 #import <Cronet/Cronet.h>
6 #import <Foundation/Foundation.h> 6 #import <Foundation/Foundation.h>
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 10 matching lines...) Expand all
21 21
22 #include "url/gurl.h" 22 #include "url/gurl.h"
23 23
24 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, 24 @interface TestDelegate : NSObject<NSURLSessionDataDelegate,
25 NSURLSessionDelegate, 25 NSURLSessionDelegate,
26 NSURLSessionTaskDelegate> 26 NSURLSessionTaskDelegate>
27 27
28 // Completion semaphore for this TestDelegate. When the request this delegate is 28 // Completion semaphore for this TestDelegate. When the request this delegate is
29 // attached to finishes (either successfully or with an error), this delegate 29 // attached to finishes (either successfully or with an error), this delegate
30 // signals this semaphore. 30 // signals this semaphore.
31 @property(assign, nonatomic) dispatch_semaphore_t semaphore; 31 @property(assign, atomic) dispatch_semaphore_t semaphore;
32
33 // Body of response received by the request this delegate is attached to.
34 @property(retain, nonatomic) NSString* responseBody;
35 32
36 // Error the request this delegate is attached to failed with, if any. 33 // Error the request this delegate is attached to failed with, if any.
37 @property(retain, nonatomic) NSError* error; 34 @property(retain, atomic) NSError* error;
38 35
39 @end 36 @end
40 37
41 @implementation TestDelegate 38 @implementation TestDelegate
42 @synthesize semaphore = _semaphore; 39 @synthesize semaphore = _semaphore;
43 @synthesize responseBody = _responseBody;
44 @synthesize error = _error; 40 @synthesize error = _error;
45 41
42 NSMutableArray<NSData*>* _responseData;
43
46 - (id)init { 44 - (id)init {
47 if (self = [super init]) { 45 if (self = [super init]) {
48 _semaphore = dispatch_semaphore_create(0); 46 _semaphore = dispatch_semaphore_create(0);
49 } 47 }
50 return self; 48 return self;
51 } 49 }
52 50
53 - (void)dealloc { 51 - (void)dealloc {
54 dispatch_release(_semaphore); 52 dispatch_release(_semaphore);
55 [_error release]; 53 [_error release];
56 _error = nil; 54 _error = nil;
57 [super dealloc]; 55 [super dealloc];
58 } 56 }
59 57
60 - (void)reset { 58 - (void)reset {
61 _responseBody = nil; 59 [_responseData dealloc];
60 _responseData = nil;
62 _error = nil; 61 _error = nil;
63 } 62 }
64 63
64 - (NSString*)responseBody {
65 if (_responseData == nil) {
66 return nil;
67 }
68 NSMutableString* body = [NSMutableString string];
69 for (NSData* data in _responseData) {
70 [body appendString:[[NSString alloc] initWithData:data
71 encoding:NSUTF8StringEncoding]];
72 }
73 VLOG(3) << "responseBody size:" << [body length]
74 << " chunks:" << [_responseData count];
75 return body;
76 }
77
65 - (void)URLSession:(NSURLSession*)session 78 - (void)URLSession:(NSURLSession*)session
66 didBecomeInvalidWithError:(NSError*)error { 79 didBecomeInvalidWithError:(NSError*)error {
67 } 80 }
68 81
69 - (void)URLSession:(NSURLSession*)session 82 - (void)URLSession:(NSURLSession*)session
70 task:(NSURLSessionTask*)task 83 task:(NSURLSessionTask*)task
71 didCompleteWithError:(NSError*)error { 84 didCompleteWithError:(NSError*)error {
72 [self setError:error]; 85 [self setError:error];
73 dispatch_semaphore_signal(_semaphore); 86 dispatch_semaphore_signal(_semaphore);
74 } 87 }
(...skipping 11 matching lines...) Expand all
86 dataTask:(NSURLSessionDataTask*)dataTask 99 dataTask:(NSURLSessionDataTask*)dataTask
87 didReceiveResponse:(NSURLResponse*)response 100 didReceiveResponse:(NSURLResponse*)response
88 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition)) 101 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))
89 completionHandler { 102 completionHandler {
90 completionHandler(NSURLSessionResponseAllow); 103 completionHandler(NSURLSessionResponseAllow);
91 } 104 }
92 105
93 - (void)URLSession:(NSURLSession*)session 106 - (void)URLSession:(NSURLSession*)session
94 dataTask:(NSURLSessionDataTask*)dataTask 107 dataTask:(NSURLSessionDataTask*)dataTask
95 didReceiveData:(NSData*)data { 108 didReceiveData:(NSData*)data {
96 NSString* stringData = 109 if (_responseData == nil) {
97 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 110 _responseData = [[NSMutableArray alloc] init];
98 if (_responseBody == nil) {
99 _responseBody = stringData;
100 } else {
101 _responseBody = [_responseBody stringByAppendingString:stringData];
102 } 111 }
112 [_responseData addObject:data];
103 } 113 }
104 114
105 - (void)URLSession:(NSURLSession*)session 115 - (void)URLSession:(NSURLSession*)session
106 dataTask:(NSURLSessionDataTask*)dataTask 116 dataTask:(NSURLSessionDataTask*)dataTask
107 willCacheResponse:(NSCachedURLResponse*)proposedResponse 117 willCacheResponse:(NSCachedURLResponse*)proposedResponse
108 completionHandler: 118 completionHandler:
109 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler { 119 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler {
110 completionHandler(proposedResponse); 120 completionHandler(proposedResponse);
111 } 121 }
112 122
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 void TearDown() override { 157 void TearDown() override {
148 grpc_support::ShutdownQuicTestServer(); 158 grpc_support::ShutdownQuicTestServer();
149 TestServer::Shutdown(); 159 TestServer::Shutdown();
150 } 160 }
151 161
152 // Launches the supplied |task| and blocks until it completes, with a timeout 162 // Launches the supplied |task| and blocks until it completes, with a timeout
153 // of 1 second. 163 // of 1 second.
154 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { 164 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
155 [delegate_ reset]; 165 [delegate_ reset];
156 [task resume]; 166 [task resume];
157 int64_t deadline_ns = 1 * ns_in_second; 167 int64_t deadline_ns = 20 * ns_in_second;
158 dispatch_semaphore_wait([delegate_ semaphore], 168 dispatch_semaphore_wait([delegate_ semaphore],
kapishnikov 2017/04/19 18:19:01 Are we interested in the result of dispatch_semaph
mef 2017/04/19 19:04:48 Done.
159 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); 169 dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
160 } 170 }
161 171
162 base::scoped_nsobject<NSURLSession> session_; 172 base::scoped_nsobject<NSURLSession> session_;
163 base::scoped_nsobject<TestDelegate> delegate_; 173 base::scoped_nsobject<TestDelegate> delegate_;
164 }; 174 };
165 175
166 TEST_F(HttpTest, NSURLSessionReceivesData) { 176 TEST_F(HttpTest, NSURLSessionReceivesData) {
167 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); 177 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl));
168 __block BOOL block_used = NO; 178 __block BOOL block_used = NO;
169 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 179 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
170 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 180 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
171 block_used = YES; 181 block_used = YES;
172 EXPECT_EQ([request URL], url); 182 EXPECT_EQ([request URL], url);
173 return YES; 183 return YES;
174 }]; 184 }];
175 StartDataTaskAndWaitForCompletion(task); 185 StartDataTaskAndWaitForCompletion(task);
176 EXPECT_TRUE(block_used); 186 EXPECT_TRUE(block_used);
177 EXPECT_EQ(nil, [delegate_ error]); 187 EXPECT_EQ(nil, [delegate_ error]);
178 EXPECT_STREQ(grpc_support::kHelloBodyValue, 188 EXPECT_STREQ(grpc_support::kSimpleBodyValue,
179 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); 189 base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
180 } 190 }
181 191
192 TEST_F(HttpTest, NSURLSessionReceivesBigHttpDataLoop) {
193 int iterations = 50;
194 long size = 10 * 1024 * 1024;
195 LOG(INFO) << "Downloading " << size << " bytes " << iterations << " times.";
196 NSTimeInterval elapsed_avg = 0;
197 NSTimeInterval elapsed_max = 0;
198 NSURL* url = net::NSURLWithGURL(GURL(TestServer::PrepareBigDataURL(size)));
199 for (int i = 0; i < iterations; ++i) {
200 [delegate_ reset];
201 __block BOOL block_used = NO;
202 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
203 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
204 block_used = YES;
205 EXPECT_EQ([request URL], url);
206 return YES;
207 }];
208 NSDate* date = [NSDate date];
209 StartDataTaskAndWaitForCompletion(task);
210 NSTimeInterval elapsed = [date timeIntervalSinceNow] * -1000.0;
211 elapsed_avg += elapsed;
212 if (elapsed > elapsed_max)
213 elapsed_max = elapsed;
214 EXPECT_TRUE(block_used);
215 EXPECT_EQ(nil, [delegate_ error]);
216 }
217 // Release the response buffer.
218 TestServer::PrepareBigDataURL(0);
219 LOG(INFO) << "Elapsed Average:" << elapsed_avg / iterations
220 << "ms Max:" << elapsed_max << "ms";
221 }
222
182 TEST_F(HttpTest, GetGlobalMetricsDeltas) { 223 TEST_F(HttpTest, GetGlobalMetricsDeltas) {
183 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; 224 NSData* delta1 = [Cronet getGlobalMetricsDeltas];
184 225
185 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); 226 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl));
186 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 227 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
187 StartDataTaskAndWaitForCompletion(task); 228 StartDataTaskAndWaitForCompletion(task);
188 EXPECT_EQ(nil, [delegate_ error]); 229 EXPECT_EQ(nil, [delegate_ error]);
189 EXPECT_STREQ(grpc_support::kHelloBodyValue, 230 EXPECT_STREQ(grpc_support::kSimpleBodyValue,
190 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); 231 base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
191 232
192 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; 233 NSData* delta2 = [Cronet getGlobalMetricsDeltas];
193 EXPECT_FALSE([delta2 isEqualToData:delta1]); 234 EXPECT_FALSE([delta2 isEqualToData:delta1]);
194 } 235 }
195 236
196 TEST_F(HttpTest, SdchDisabledByDefault) { 237 TEST_F(HttpTest, SdchDisabledByDefault) {
197 NSURL* url = 238 NSURL* url =
198 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); 239 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding")));
199 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; 240 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 450 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
410 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; 451 EXPECT_TRUE(false) << "Block should not be called for unsupported requests";
411 return YES; 452 return YES;
412 }]; 453 }];
413 StartDataTaskAndWaitForCompletion(task); 454 StartDataTaskAndWaitForCompletion(task);
414 EXPECT_EQ(nil, [delegate_ error]); 455 EXPECT_EQ(nil, [delegate_ error]);
415 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); 456 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]);
416 } 457 }
417 458
418 } // namespace cronet 459 } // namespace cronet
OLDNEW
« no previous file with comments | « no previous file | components/cronet/ios/test/start_cronet.mm » ('j') | components/cronet/ios/test/test_server.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698