| Index: components/cronet/ios/test/cronet_http_test.mm
|
| diff --git a/components/cronet/ios/test/cronet_http_test.mm b/components/cronet/ios/test/cronet_http_test.mm
|
| index e1642feeaf9cbb5a6ad0cff8fdde7d7e7a03d6b1..58185081938d23a4a681f663069287eff30f6fb7 100644
|
| --- a/components/cronet/ios/test/cronet_http_test.mm
|
| +++ b/components/cronet/ios/test/cronet_http_test.mm
|
| @@ -28,21 +28,19 @@
|
| // Completion semaphore for this TestDelegate. When the request this delegate is
|
| // attached to finishes (either successfully or with an error), this delegate
|
| // signals this semaphore.
|
| -@property(assign, nonatomic) dispatch_semaphore_t semaphore;
|
| -
|
| -// Body of response received by the request this delegate is attached to.
|
| -@property(retain, nonatomic) NSString* responseBody;
|
| +@property(assign, atomic) dispatch_semaphore_t semaphore;
|
|
|
| // Error the request this delegate is attached to failed with, if any.
|
| -@property(retain, nonatomic) NSError* error;
|
| +@property(retain, atomic) NSError* error;
|
|
|
| @end
|
|
|
| @implementation TestDelegate
|
| @synthesize semaphore = _semaphore;
|
| -@synthesize responseBody = _responseBody;
|
| @synthesize error = _error;
|
|
|
| +NSMutableArray<NSData*>* _responseData;
|
| +
|
| - (id)init {
|
| if (self = [super init]) {
|
| _semaphore = dispatch_semaphore_create(0);
|
| @@ -58,10 +56,25 @@
|
| }
|
|
|
| - (void)reset {
|
| - _responseBody = nil;
|
| + [_responseData dealloc];
|
| + _responseData = nil;
|
| _error = nil;
|
| }
|
|
|
| +- (NSString*)responseBody {
|
| + if (_responseData == nil) {
|
| + return nil;
|
| + }
|
| + NSMutableString* body = [NSMutableString string];
|
| + for (NSData* data in _responseData) {
|
| + [body appendString:[[NSString alloc] initWithData:data
|
| + encoding:NSUTF8StringEncoding]];
|
| + }
|
| + VLOG(3) << "responseBody size:" << [body length]
|
| + << " chunks:" << [_responseData count];
|
| + return body;
|
| +}
|
| +
|
| - (void)URLSession:(NSURLSession*)session
|
| didBecomeInvalidWithError:(NSError*)error {
|
| }
|
| @@ -93,13 +106,10 @@
|
| - (void)URLSession:(NSURLSession*)session
|
| dataTask:(NSURLSessionDataTask*)dataTask
|
| didReceiveData:(NSData*)data {
|
| - NSString* stringData =
|
| - [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
| - if (_responseBody == nil) {
|
| - _responseBody = stringData;
|
| - } else {
|
| - _responseBody = [_responseBody stringByAppendingString:stringData];
|
| + if (_responseData == nil) {
|
| + _responseData = [[NSMutableArray alloc] init];
|
| }
|
| + [_responseData addObject:data];
|
| }
|
|
|
| - (void)URLSession:(NSURLSession*)session
|
| @@ -154,9 +164,10 @@ class HttpTest : public ::testing::Test {
|
| void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
|
| [delegate_ reset];
|
| [task resume];
|
| - int64_t deadline_ns = 1 * ns_in_second;
|
| - dispatch_semaphore_wait([delegate_ semaphore],
|
| - dispatch_time(DISPATCH_TIME_NOW, deadline_ns));
|
| + int64_t deadline_ns = 20 * ns_in_second;
|
| + ASSERT_EQ(0, dispatch_semaphore_wait(
|
| + [delegate_ semaphore],
|
| + dispatch_time(DISPATCH_TIME_NOW, deadline_ns)));
|
| }
|
|
|
| base::scoped_nsobject<NSURLSession> session_;
|
| @@ -164,7 +175,7 @@ class HttpTest : public ::testing::Test {
|
| };
|
|
|
| TEST_F(HttpTest, NSURLSessionReceivesData) {
|
| - NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl));
|
| + NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl));
|
| __block BOOL block_used = NO;
|
| NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
|
| [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
|
| @@ -175,18 +186,49 @@ TEST_F(HttpTest, NSURLSessionReceivesData) {
|
| StartDataTaskAndWaitForCompletion(task);
|
| EXPECT_TRUE(block_used);
|
| EXPECT_EQ(nil, [delegate_ error]);
|
| - EXPECT_STREQ(grpc_support::kHelloBodyValue,
|
| + EXPECT_STREQ(grpc_support::kSimpleBodyValue,
|
| base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
|
| }
|
|
|
| +TEST_F(HttpTest, NSURLSessionReceivesBigHttpDataLoop) {
|
| + int iterations = 50;
|
| + long size = 10 * 1024 * 1024;
|
| + LOG(INFO) << "Downloading " << size << " bytes " << iterations << " times.";
|
| + NSTimeInterval elapsed_avg = 0;
|
| + NSTimeInterval elapsed_max = 0;
|
| + NSURL* url = net::NSURLWithGURL(GURL(TestServer::PrepareBigDataURL(size)));
|
| + for (int i = 0; i < iterations; ++i) {
|
| + [delegate_ reset];
|
| + __block BOOL block_used = NO;
|
| + NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
|
| + [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
|
| + block_used = YES;
|
| + EXPECT_EQ([request URL], url);
|
| + return YES;
|
| + }];
|
| + NSDate* start = [NSDate date];
|
| + StartDataTaskAndWaitForCompletion(task);
|
| + NSTimeInterval elapsed = -[start timeIntervalSinceNow];
|
| + elapsed_avg += elapsed;
|
| + if (elapsed > elapsed_max)
|
| + elapsed_max = elapsed;
|
| + EXPECT_TRUE(block_used);
|
| + EXPECT_EQ(nil, [delegate_ error]);
|
| + }
|
| + // Release the response buffer.
|
| + TestServer::ReleaseBigDataURL();
|
| + LOG(INFO) << "Elapsed Average:" << elapsed_avg * 1000 / iterations
|
| + << "ms Max:" << elapsed_max * 1000 << "ms";
|
| +}
|
| +
|
| TEST_F(HttpTest, GetGlobalMetricsDeltas) {
|
| NSData* delta1 = [Cronet getGlobalMetricsDeltas];
|
|
|
| - NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerUrl));
|
| + NSURL* url = net::NSURLWithGURL(GURL(grpc_support::kTestServerSimpleUrl));
|
| NSURLSessionDataTask* task = [session_ dataTaskWithURL:url];
|
| StartDataTaskAndWaitForCompletion(task);
|
| EXPECT_EQ(nil, [delegate_ error]);
|
| - EXPECT_STREQ(grpc_support::kHelloBodyValue,
|
| + EXPECT_STREQ(grpc_support::kSimpleBodyValue,
|
| base::SysNSStringToUTF8([delegate_ responseBody]).c_str());
|
|
|
| NSData* delta2 = [Cronet getGlobalMetricsDeltas];
|
|
|