OLD | NEW |
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 Loading... |
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 Loading... |
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 Loading... |
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 ASSERT_EQ(0, dispatch_semaphore_wait( |
159 dispatch_time(DISPATCH_TIME_NOW, deadline_ns)); | 169 [delegate_ semaphore], |
| 170 dispatch_time(DISPATCH_TIME_NOW, deadline_ns))); |
160 } | 171 } |
161 | 172 |
162 base::scoped_nsobject<NSURLSession> session_; | 173 base::scoped_nsobject<NSURLSession> session_; |
163 base::scoped_nsobject<TestDelegate> delegate_; | 174 base::scoped_nsobject<TestDelegate> delegate_; |
164 }; | 175 }; |
165 | 176 |
166 TEST_F(HttpTest, NSURLSessionReceivesData) { | 177 TEST_F(HttpTest, NSURLSessionReceivesData) { |
167 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); | 178 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl)); |
168 __block BOOL block_used = NO; | 179 __block BOOL block_used = NO; |
169 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 180 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
170 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { | 181 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { |
171 block_used = YES; | 182 block_used = YES; |
172 EXPECT_EQ([request URL], url); | 183 EXPECT_EQ([request URL], url); |
173 return YES; | 184 return YES; |
174 }]; | 185 }]; |
175 StartDataTaskAndWaitForCompletion(task); | 186 StartDataTaskAndWaitForCompletion(task); |
176 EXPECT_TRUE(block_used); | 187 EXPECT_TRUE(block_used); |
177 EXPECT_EQ(nil, [delegate_ error]); | 188 EXPECT_EQ(nil, [delegate_ error]); |
178 EXPECT_STREQ(grpc_support::kHelloBodyValue, | 189 EXPECT_STREQ(grpc_support::kSimpleBodyValue, |
179 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); | 190 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); |
180 } | 191 } |
181 | 192 |
| 193 TEST_F(HttpTest, NSURLSessionReceivesBigHttpDataLoop) { |
| 194 int iterations = 50; |
| 195 long size = 10 * 1024 * 1024; |
| 196 LOG(INFO) << "Downloading " << size << " bytes " << iterations << " times."; |
| 197 NSTimeInterval elapsed_avg = 0; |
| 198 NSTimeInterval elapsed_max = 0; |
| 199 NSURL* url = net::NSURLWithGURL(GURL(TestServer::PrepareBigDataURL(size))); |
| 200 for (int i = 0; i < iterations; ++i) { |
| 201 [delegate_ reset]; |
| 202 __block BOOL block_used = NO; |
| 203 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
| 204 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { |
| 205 block_used = YES; |
| 206 EXPECT_EQ([request URL], url); |
| 207 return YES; |
| 208 }]; |
| 209 NSDate* start = [NSDate date]; |
| 210 StartDataTaskAndWaitForCompletion(task); |
| 211 NSTimeInterval elapsed = -[start timeIntervalSinceNow]; |
| 212 elapsed_avg += elapsed; |
| 213 if (elapsed > elapsed_max) |
| 214 elapsed_max = elapsed; |
| 215 EXPECT_TRUE(block_used); |
| 216 EXPECT_EQ(nil, [delegate_ error]); |
| 217 } |
| 218 // Release the response buffer. |
| 219 TestServer::ReleaseBigDataURL(); |
| 220 LOG(INFO) << "Elapsed Average:" << elapsed_avg * 1000 / iterations |
| 221 << "ms Max:" << elapsed_max * 1000 << "ms"; |
| 222 } |
| 223 |
182 TEST_F(HttpTest, GetGlobalMetricsDeltas) { | 224 TEST_F(HttpTest, GetGlobalMetricsDeltas) { |
183 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; | 225 NSData* delta1 = [Cronet getGlobalMetricsDeltas]; |
184 | 226 |
185 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl)); | 227 NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl)); |
186 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 228 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
187 StartDataTaskAndWaitForCompletion(task); | 229 StartDataTaskAndWaitForCompletion(task); |
188 EXPECT_EQ(nil, [delegate_ error]); | 230 EXPECT_EQ(nil, [delegate_ error]); |
189 EXPECT_STREQ(grpc_support::kHelloBodyValue, | 231 EXPECT_STREQ(grpc_support::kSimpleBodyValue, |
190 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); | 232 base::SysNSStringToUTF8([delegate_ responseBody]).c_str()); |
191 | 233 |
192 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; | 234 NSData* delta2 = [Cronet getGlobalMetricsDeltas]; |
193 EXPECT_FALSE([delta2 isEqualToData:delta1]); | 235 EXPECT_FALSE([delta2 isEqualToData:delta1]); |
194 } | 236 } |
195 | 237 |
196 TEST_F(HttpTest, SdchDisabledByDefault) { | 238 TEST_F(HttpTest, SdchDisabledByDefault) { |
197 NSURL* url = | 239 NSURL* url = |
198 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); | 240 net::NSURLWithGURL(GURL(TestServer::GetEchoHeaderURL("Accept-Encoding"))); |
199 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; | 241 NSURLSessionDataTask* task = [session_ dataTaskWithURL:url]; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { | 451 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { |
410 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; | 452 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; |
411 return YES; | 453 return YES; |
412 }]; | 454 }]; |
413 StartDataTaskAndWaitForCompletion(task); | 455 StartDataTaskAndWaitForCompletion(task); |
414 EXPECT_EQ(nil, [delegate_ error]); | 456 EXPECT_EQ(nil, [delegate_ error]); |
415 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); | 457 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); |
416 } | 458 } |
417 | 459 |
418 } // namespace cronet | 460 } // namespace cronet |
OLD | NEW |