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..30274b4af71d456cfb9c4ef15bfae4490afd7eb6 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(retain, 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); |
@@ -51,17 +49,33 @@ |
} |
- (void)dealloc { |
- dispatch_release(_semaphore); |
- [_error release]; |
+// dispatch_release(_semaphore); |
+// [_error release]; |
+ _semaphore = nil; |
_error = nil; |
- [super dealloc]; |
+// [super dealloc]; |
} |
- (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 +107,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 |
@@ -113,9 +124,6 @@ |
@end |
namespace cronet { |
-// base::TimeDelta would normally be ideal for this but it does not support |
-// nanosecond resolution. |
-static const int64_t ns_in_second = 1000000000LL; |
const char kUserAgent[] = "CronetTest/1.0.0.0"; |
class HttpTest : public ::testing::Test { |
@@ -141,7 +149,8 @@ class HttpTest : public ::testing::Test { |
delegateQueue:nil]; |
// Take a reference to the session and store it so it doesn't get |
// deallocated until this object does. |
- session_.reset([session retain]); |
+ //session_.reset([session retain]); |
+ session_.reset(session); |
} |
void TearDown() override { |
@@ -154,9 +163,7 @@ 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)); |
+ dispatch_semaphore_wait([delegate_ semaphore], DISPATCH_TIME_FOREVER); |
} |
base::scoped_nsobject<NSURLSession> session_; |
@@ -164,7 +171,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 +182,47 @@ 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::GetBigDataURL(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* date = [NSDate date]; |
+ StartDataTaskAndWaitForCompletion(task); |
+ NSTimeInterval elapsed = [date timeIntervalSinceNow] * -1000.0; |
+ elapsed_avg += elapsed; |
+ if (elapsed > elapsed_max) |
+ elapsed_max = elapsed; |
+ EXPECT_TRUE(block_used); |
+ EXPECT_EQ(nil, [delegate_ error]); |
+ } |
+ LOG(INFO) << "Elapsed Average:" << elapsed_avg / iterations |
+ << "ms Max:" << elapsed_max << "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]; |