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

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

Issue 2836063005: [cronet] Add mechanism for restarting CronetEnvironment (Closed)
Patch Set: per #21 Created 3 years, 7 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"
11 #include "base/mac/scoped_nsobject.h" 11 #include "base/mac/scoped_nsobject.h"
12 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
13 #include "components/cronet/ios/test/start_cronet.h" 13 #include "components/cronet/ios/test/start_cronet.h"
14 #include "components/cronet/ios/test/test_server.h" 14 #include "components/cronet/ios/test/test_server.h"
15 #include "components/grpc_support/test/quic_test_server.h" 15 #include "components/grpc_support/test/quic_test_server.h"
16 #include "net/base/mac/url_conversions.h" 16 #include "net/base/mac/url_conversions.h"
17 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
18 #include "net/cert/mock_cert_verifier.h" 18 #include "net/cert/mock_cert_verifier.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/gtest_mac.h" 20 #include "testing/gtest_mac.h"
21 21
22 #include "url/gurl.h" 22 #include "url/gurl.h"
23 23
24 @interface Cronet (ExposedForTesting)
mef 2017/04/27 14:58:20 I wonder whether it makes sense to create a header
lilyhoughton 2017/05/01 15:31:35 I like this idea a lot. It probably makes sense a
mef 2017/05/01 19:12:58 sgtm
25 + (void)shutdownForTesting;
26 @end
27
24 @interface TestDelegate : NSObject<NSURLSessionDataDelegate, 28 @interface TestDelegate : NSObject<NSURLSessionDataDelegate,
25 NSURLSessionDelegate, 29 NSURLSessionDelegate,
26 NSURLSessionTaskDelegate> 30 NSURLSessionTaskDelegate>
27 31
28 // Completion semaphore for this TestDelegate. When the request this delegate is 32 // Completion semaphore for this TestDelegate. When the request this delegate is
29 // attached to finishes (either successfully or with an error), this delegate 33 // attached to finishes (either successfully or with an error), this delegate
30 // signals this semaphore. 34 // signals this semaphore.
31 @property(assign, atomic) dispatch_semaphore_t semaphore; 35 @property(assign, atomic) dispatch_semaphore_t semaphore;
32 36
33 // Error the request this delegate is attached to failed with, if any. 37 // Error the request this delegate is attached to failed with, if any.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 HttpTest() {} 137 HttpTest() {}
134 ~HttpTest() override {} 138 ~HttpTest() override {}
135 139
136 void SetUp() override { 140 void SetUp() override {
137 grpc_support::StartQuicTestServer(); 141 grpc_support::StartQuicTestServer();
138 TestServer::Start(); 142 TestServer::Start();
139 143
140 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 144 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
141 return YES; 145 return YES;
142 }]; 146 }];
143 StartCronetIfNecessary(grpc_support::GetQuicTestServerPort()); 147 StartCronet(grpc_support::GetQuicTestServerPort());
144 [Cronet registerHttpProtocolHandler]; 148 [Cronet registerHttpProtocolHandler];
145 NSURLSessionConfiguration* config = 149 NSURLSessionConfiguration* config =
146 [NSURLSessionConfiguration ephemeralSessionConfiguration]; 150 [NSURLSessionConfiguration ephemeralSessionConfiguration];
147 [Cronet installIntoSessionConfiguration:config]; 151 [Cronet installIntoSessionConfiguration:config];
148 delegate_.reset([[TestDelegate alloc] init]); 152 delegate_.reset([[TestDelegate alloc] init]);
149 NSURLSession* session = [NSURLSession sessionWithConfiguration:config 153 NSURLSession* session = [NSURLSession sessionWithConfiguration:config
150 delegate:delegate_ 154 delegate:delegate_
151 delegateQueue:nil]; 155 delegateQueue:nil];
152 // Take a reference to the session and store it so it doesn't get 156 // Take a reference to the session and store it so it doesn't get
153 // deallocated until this object does. 157 // deallocated until this object does.
154 session_.reset([session retain]); 158 session_.reset([session retain]);
155 } 159 }
156 160
157 void TearDown() override { 161 void TearDown() override {
158 grpc_support::ShutdownQuicTestServer(); 162 grpc_support::ShutdownQuicTestServer();
159 TestServer::Shutdown(); 163 TestServer::Shutdown();
164
165 [Cronet stopNetLog];
166 [Cronet shutdownForTesting];
160 } 167 }
161 168
162 // Launches the supplied |task| and blocks until it completes, with a timeout 169 // Launches the supplied |task| and blocks until it completes, with a timeout
163 // of 1 second. 170 // of 1 second.
164 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) { 171 void StartDataTaskAndWaitForCompletion(NSURLSessionDataTask* task) {
165 [delegate_ reset]; 172 [delegate_ reset];
166 [task resume]; 173 [task resume];
167 int64_t deadline_ns = 20 * ns_in_second; 174 int64_t deadline_ns = 20 * ns_in_second;
168 ASSERT_EQ(0, dispatch_semaphore_wait( 175 ASSERT_EQ(0, dispatch_semaphore_wait(
169 [delegate_ semaphore], 176 [delegate_ semaphore],
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 [Cronet setRequestFilterBlock:^(NSURLRequest* request) { 458 [Cronet setRequestFilterBlock:^(NSURLRequest* request) {
452 EXPECT_TRUE(false) << "Block should not be called for unsupported requests"; 459 EXPECT_TRUE(false) << "Block should not be called for unsupported requests";
453 return YES; 460 return YES;
454 }]; 461 }];
455 StartDataTaskAndWaitForCompletion(task); 462 StartDataTaskAndWaitForCompletion(task);
456 EXPECT_EQ(nil, [delegate_ error]); 463 EXPECT_EQ(nil, [delegate_ error]);
457 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]); 464 EXPECT_TRUE([[delegate_ responseBody] containsString:testString]);
458 } 465 }
459 466
460 } // namespace cronet 467 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698