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

Side by Side Diff: net/test/net_test_suite.cc

Issue 2839663002: Instantiate ScopedTaskEnvironment in net unittests. (Closed)
Patch Set: self-review 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "net/test/net_test_suite.h" 5 #include "net/test/net_test_suite.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/test/scoped_task_environment.h"
8 #include "net/base/network_change_notifier.h" 10 #include "net/base/network_change_notifier.h"
9 #include "net/http/http_stream_factory.h" 11 #include "net/http/http_stream_factory.h"
10 #include "net/spdy/chromium/spdy_session.h" 12 #include "net/spdy/chromium/spdy_session.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
12 14
13 #if defined(USE_NSS_CERTS) 15 #if defined(USE_NSS_CERTS)
14 #include "net/cert_net/nss_ocsp.h" 16 #include "net/cert_net/nss_ocsp.h"
15 #endif 17 #endif
16 18
19 namespace {
20 NetTestSuite* g_current_net_test_suite = nullptr;
21 } // namespace
22
17 NetTestSuite::NetTestSuite(int argc, char** argv) 23 NetTestSuite::NetTestSuite(int argc, char** argv)
18 : TestSuite(argc, argv) { 24 : TestSuite(argc, argv) {
25 DCHECK(!g_current_net_test_suite);
26 g_current_net_test_suite = this;
19 } 27 }
20 28
21 NetTestSuite::~NetTestSuite() {} 29 NetTestSuite::~NetTestSuite() {
30 DCHECK_EQ(g_current_net_test_suite, this);
31 g_current_net_test_suite = nullptr;
32 }
22 33
23 void NetTestSuite::Initialize() { 34 void NetTestSuite::Initialize() {
24 TestSuite::Initialize(); 35 TestSuite::Initialize();
25 InitializeTestThread(); 36 InitializeTestThread();
26 } 37 }
27 38
28 void NetTestSuite::Shutdown() { 39 void NetTestSuite::Shutdown() {
29 #if defined(USE_NSS_CERTS) 40 #if defined(USE_NSS_CERTS)
30 net::ShutdownNSSHttpIO(); 41 net::ShutdownNSSHttpIO();
31 #endif 42 #endif
32 43
33 // We want to destroy this here before the TestSuite continues to tear down 44 // We want to destroy this here before the TestSuite continues to tear down
34 // the environment. 45 // the environment.
35 message_loop_.reset(); 46 scoped_task_environment_.reset();
36 47
37 TestSuite::Shutdown(); 48 TestSuite::Shutdown();
38 } 49 }
39 50
51 base::test::ScopedTaskEnvironment* NetTestSuite::GetScopedTaskEnvironment() {
52 DCHECK(g_current_net_test_suite);
53 return g_current_net_test_suite->scoped_task_environment_.get();
54 }
55
40 void NetTestSuite::InitializeTestThread() { 56 void NetTestSuite::InitializeTestThread() {
41 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); 57 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock());
42 58
43 InitializeTestThreadNoNetworkChangeNotifier(); 59 InitializeTestThreadNoNetworkChangeNotifier();
44 } 60 }
45 61
46 void NetTestSuite::InitializeTestThreadNoNetworkChangeNotifier() { 62 void NetTestSuite::InitializeTestThreadNoNetworkChangeNotifier() {
47 host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL); 63 host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL);
48 scoped_host_resolver_proc_.Init(host_resolver_proc_.get()); 64 scoped_host_resolver_proc_.Init(host_resolver_proc_.get());
49 // In case any attempts are made to resolve host names, force them all to 65 // In case any attempts are made to resolve host names, force them all to
50 // be mapped to localhost. This prevents DNS queries from being sent in 66 // be mapped to localhost. This prevents DNS queries from being sent in
51 // the process of running these unit tests. 67 // the process of running these unit tests.
52 host_resolver_proc_->AddRule("*", "127.0.0.1"); 68 host_resolver_proc_->AddRule("*", "127.0.0.1");
53 69
54 message_loop_.reset(new base::MessageLoopForIO()); 70 scoped_task_environment_ =
71 base::MakeUnique<base::test::ScopedTaskEnvironment>(
72 base::test::ScopedTaskEnvironment::MainThreadType::IO);
55 } 73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698