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

Side by Side Diff: net/test/spawned_test_server/remote_test_server.cc

Issue 2505023004: Add assertions to ensure that only one instance of RemoteTestServer is (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/spawned_test_server/remote_test_server.h" 5 #include "net/test/spawned_test_server/remote_test_server.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/base_paths.h" 12 #include "base/base_paths.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/json/json_writer.h" 15 #include "base/json/json_writer.h"
16 #include "base/lazy_instance.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/path_service.h" 18 #include "base/path_service.h"
18 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
20 #include "base/values.h" 21 #include "base/values.h"
21 #include "net/base/host_port_pair.h" 22 #include "net/base/host_port_pair.h"
22 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
23 #include "net/test/spawned_test_server/spawner_communicator.h" 24 #include "net/test/spawned_test_server/spawner_communicator.h"
24 #include "url/gurl.h" 25 #include "url/gurl.h"
25 26
26 namespace net { 27 namespace net {
27 28
28 namespace { 29 namespace {
29 30
31 // Based on how the Android runner sets things up, it is only valid for one
32 // RemoteTestServer to be active on the device at a time.
33 class RemoteTestServerTracker {
34 public:
35 void StartingServer() {
36 base::AutoLock l(lock_);
37 CHECK_EQ(count_, 0);
38 count_++;
39 }
40
41 void StoppingServer() {
42 base::AutoLock l(lock_);
43 CHECK_EQ(count_, 1);
44 count_--;
45 }
46
47 private:
48 // |lock_| protects access to |count_|.
49 base::Lock lock_;
50 int count_ = 0;
51 };
52
53 base::LazyInstance<RemoteTestServerTracker>::Leaky tracker =
54 LAZY_INSTANCE_INITIALIZER;
55
30 // To reduce the running time of tests, tests may be sharded across several 56 // To reduce the running time of tests, tests may be sharded across several
31 // devices. This means that it may be necessary to support multiple instances 57 // devices. This means that it may be necessary to support multiple instances
32 // of the test server spawner and the Python test server simultaneously on the 58 // of the test server spawner and the Python test server simultaneously on the
33 // same host. Each pair of (test server spawner, Python test server) correspond 59 // same host. Each pair of (test server spawner, Python test server) correspond
34 // to a single testing device. 60 // to a single testing device.
35 // The mapping between the test server spawner and the individual Python test 61 // The mapping between the test server spawner and the individual Python test
36 // servers is written to a file on the device prior to executing any tests. 62 // servers is written to a file on the device prior to executing any tests.
37 base::FilePath GetTestServerPortInfoFile() { 63 base::FilePath GetTestServerPortInfoFile() {
38 #if !defined(OS_ANDROID) 64 #if !defined(OS_ANDROID)
39 return base::FilePath("/tmp/net-test-server-ports"); 65 return base::FilePath("/tmp/net-test-server-ports");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 NOTREACHED(); 111 NOTREACHED();
86 } 112 }
87 113
88 RemoteTestServer::~RemoteTestServer() { 114 RemoteTestServer::~RemoteTestServer() {
89 Stop(); 115 Stop();
90 } 116 }
91 117
92 bool RemoteTestServer::Start() { 118 bool RemoteTestServer::Start() {
93 if (spawner_communicator_.get()) 119 if (spawner_communicator_.get())
94 return true; 120 return true;
121
122 tracker.Get().StartingServer();
123
95 spawner_communicator_.reset(new SpawnerCommunicator(spawner_server_port_)); 124 spawner_communicator_.reset(new SpawnerCommunicator(spawner_server_port_));
96 125
97 base::DictionaryValue arguments_dict; 126 base::DictionaryValue arguments_dict;
98 if (!GenerateArguments(&arguments_dict)) 127 if (!GenerateArguments(&arguments_dict))
99 return false; 128 return false;
100 129
101 arguments_dict.Set("on-remote-server", base::Value::CreateNullValue()); 130 arguments_dict.Set("on-remote-server", base::Value::CreateNullValue());
102 131
103 // Append the 'server-type' argument which is used by spawner server to 132 // Append the 'server-type' argument which is used by spawner server to
104 // pass right server type to Python test server. 133 // pass right server type to Python test server.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 170 }
142 171
143 bool RemoteTestServer::BlockUntilStarted() { 172 bool RemoteTestServer::BlockUntilStarted() {
144 NOTIMPLEMENTED(); 173 NOTIMPLEMENTED();
145 return false; 174 return false;
146 } 175 }
147 176
148 bool RemoteTestServer::Stop() { 177 bool RemoteTestServer::Stop() {
149 if (!spawner_communicator_.get()) 178 if (!spawner_communicator_.get())
150 return true; 179 return true;
180
181 tracker.Get().StoppingServer();
182
151 CleanUpWhenStoppingServer(); 183 CleanUpWhenStoppingServer();
152 bool stopped = spawner_communicator_->StopServer(); 184 bool stopped = spawner_communicator_->StopServer();
185
186 if (!stopped)
187 LOG(ERROR) << "Failed stopping RemoteTestServer";
188
153 // Explicitly reset |spawner_communicator_| to avoid reusing the stopped one. 189 // Explicitly reset |spawner_communicator_| to avoid reusing the stopped one.
154 spawner_communicator_.reset(NULL); 190 spawner_communicator_.reset(NULL);
155 return stopped; 191 return stopped;
156 } 192 }
157 193
158 // On Android, the document root in the device is not the same as the document 194 // On Android, the document root in the device is not the same as the document
159 // root in the host machine where the test server is launched. So prepend 195 // root in the host machine where the test server is launched. So prepend
160 // DIR_SOURCE_ROOT here to get the actual path of document root on the Android 196 // DIR_SOURCE_ROOT here to get the actual path of document root on the Android
161 // device. 197 // device.
162 base::FilePath RemoteTestServer::GetDocumentRoot() const { 198 base::FilePath RemoteTestServer::GetDocumentRoot() const {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 fixed_root = base::FilePath(base::FilePath::kCurrentDirectory); 242 fixed_root = base::FilePath(base::FilePath::kCurrentDirectory);
207 SetResourcePath(fixed_root, base::FilePath().AppendASCII("net") 243 SetResourcePath(fixed_root, base::FilePath().AppendASCII("net")
208 .AppendASCII("data") 244 .AppendASCII("data")
209 .AppendASCII("ssl") 245 .AppendASCII("ssl")
210 .AppendASCII("certificates")); 246 .AppendASCII("certificates"));
211 return true; 247 return true;
212 } 248 }
213 249
214 } // namespace net 250 } // namespace net
215 251
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698