[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/websocket_endpoint_lock_manager.h" |
| 6 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 7 | #include "base/logging.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include "base/macros.h" |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
| 10 | #include "base/run_loop.h" |
| 11 | #include "base/time/time.h" |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 12 | #include "net/base/ip_address.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 13 | #include "net/base/net_errors.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 14 | #include "net/log/net_log_with_source.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 15 | #include "net/socket/next_proto.h" |
| 16 | #include "net/socket/socket_test_util.h" |
| 17 | #include "net/socket/stream_socket.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18 | #include "net/test/gtest_util.h" |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 19 | #include "net/traffic_annotation/network_traffic_annotation.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 20 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 23 | using net::test::IsOk; |
| 24 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 25 | namespace net { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // A StreamSocket implementation with no functionality at all. |
| 30 | // TODO(ricea): If you need to use this in another file, please move it to |
| 31 | // socket_test_util.h. |
| 32 | class FakeStreamSocket : public StreamSocket { |
| 33 | public: |
Chris Watkins | 2a30ec2 | 2017-11-30 05:14:50 | [diff] [blame] | 34 | FakeStreamSocket() = default; |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 35 | |
| 36 | // StreamSocket implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 37 | int Connect(const CompletionCallback& callback) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 38 | return ERR_FAILED; |
| 39 | } |
| 40 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 41 | void Disconnect() override { return; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 42 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | bool IsConnected() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 44 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 45 | bool IsConnectedAndIdle() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 46 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 47 | int GetPeerAddress(IPEndPoint* address) const override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 48 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 49 | int GetLocalAddress(IPEndPoint* address) const override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 50 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 51 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 52 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 53 | void SetSubresourceSpeculation() override { return; } |
| 54 | void SetOmniboxSpeculation() override { return; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 55 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 56 | bool WasEverUsed() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 57 | |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 58 | bool WasAlpnNegotiated() const override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 59 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 60 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 61 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 62 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 63 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 64 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 65 | out->clear(); |
| 66 | } |
| 67 | |
| 68 | void ClearConnectionAttempts() override {} |
| 69 | |
| 70 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
| 71 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 72 | int64_t GetTotalReceivedBytes() const override { |
| 73 | NOTIMPLEMENTED(); |
| 74 | return 0; |
| 75 | } |
| 76 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 77 | void ApplySocketTag(const SocketTag& tag) override {} |
| 78 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 79 | // Socket implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 80 | int Read(IOBuffer* buf, |
| 81 | int buf_len, |
| 82 | const CompletionCallback& callback) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 83 | return ERR_FAILED; |
| 84 | } |
| 85 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 86 | int Write(IOBuffer* buf, |
| 87 | int buf_len, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 88 | const CompletionCallback& callback, |
| 89 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 90 | return ERR_FAILED; |
| 91 | } |
| 92 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 93 | int SetReceiveBufferSize(int32_t size) override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 94 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 95 | int SetSendBufferSize(int32_t size) override { return ERR_FAILED; } |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 96 | |
| 97 | private: |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 98 | NetLogWithSource net_log_; |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
| 101 | }; |
| 102 | |
| 103 | class FakeWaiter : public WebSocketEndpointLockManager::Waiter { |
| 104 | public: |
| 105 | FakeWaiter() : called_(false) {} |
| 106 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 107 | void GotEndpointLock() override { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 108 | CHECK(!called_); |
| 109 | called_ = true; |
| 110 | } |
| 111 | |
| 112 | bool called() const { return called_; } |
| 113 | |
| 114 | private: |
| 115 | bool called_; |
| 116 | }; |
| 117 | |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 118 | class BlockingWaiter : public FakeWaiter { |
| 119 | public: |
| 120 | void WaitForLock() { |
| 121 | while (!called()) { |
| 122 | run_loop_.Run(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void GotEndpointLock() override { |
| 127 | FakeWaiter::GotEndpointLock(); |
| 128 | run_loop_.Quit(); |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | base::RunLoop run_loop_; |
| 133 | }; |
| 134 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 135 | class WebSocketEndpointLockManagerTest : public ::testing::Test { |
| 136 | protected: |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 137 | WebSocketEndpointLockManagerTest() { |
| 138 | websocket_endpoint_lock_manager_.SetUnlockDelayForTesting( |
| 139 | base::TimeDelta()); |
| 140 | } |
| 141 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 142 | ~WebSocketEndpointLockManagerTest() override { |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 143 | // Permit any pending asynchronous unlock operations to complete. |
| 144 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 145 | // If this check fails then subsequent tests may fail. |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 146 | CHECK(websocket_endpoint_lock_manager_.IsEmpty()); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 149 | IPEndPoint DummyEndpoint() { |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 150 | return IPEndPoint(IPAddress::IPv4Localhost(), 80); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void UnlockDummyEndpoint(int times) { |
| 154 | for (int i = 0; i < times; ++i) { |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 155 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 156 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 160 | static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); } |
| 161 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 162 | WebSocketEndpointLockManager websocket_endpoint_lock_manager_; |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 163 | }; |
| 164 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 165 | TEST_F(WebSocketEndpointLockManagerTest, LockEndpointReturnsOkOnce) { |
| 166 | FakeWaiter waiters[2]; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 167 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 168 | &waiters[0]), |
| 169 | IsOk()); |
| 170 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 171 | DummyEndpoint(), &waiters[1])); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 172 | |
| 173 | UnlockDummyEndpoint(2); |
| 174 | } |
| 175 | |
| 176 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledOnOk) { |
| 177 | FakeWaiter waiter; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 178 | EXPECT_THAT( |
| 179 | websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), &waiter), |
| 180 | IsOk()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 181 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 182 | EXPECT_FALSE(waiter.called()); |
| 183 | |
| 184 | UnlockDummyEndpoint(1); |
| 185 | } |
| 186 | |
| 187 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledImmediately) { |
| 188 | FakeWaiter waiters[2]; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 189 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 190 | &waiters[0]), |
| 191 | IsOk()); |
| 192 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 193 | DummyEndpoint(), &waiters[1])); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 194 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 195 | EXPECT_FALSE(waiters[1].called()); |
| 196 | |
| 197 | UnlockDummyEndpoint(2); |
| 198 | } |
| 199 | |
| 200 | TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockCalledWhenUnlocked) { |
| 201 | FakeWaiter waiters[2]; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 202 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 203 | &waiters[0]), |
| 204 | IsOk()); |
| 205 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 206 | DummyEndpoint(), &waiters[1])); |
| 207 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 208 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 209 | EXPECT_TRUE(waiters[1].called()); |
| 210 | |
| 211 | UnlockDummyEndpoint(1); |
| 212 | } |
| 213 | |
| 214 | TEST_F(WebSocketEndpointLockManagerTest, |
| 215 | EndpointUnlockedIfWaiterAlreadyDeleted) { |
| 216 | FakeWaiter first_lock_holder; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 217 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 218 | &first_lock_holder), |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 219 | IsOk()); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 220 | |
| 221 | { |
| 222 | FakeWaiter short_lived_waiter; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 223 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 224 | DummyEndpoint(), &short_lived_waiter)); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 225 | } |
| 226 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 227 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 228 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 229 | |
| 230 | FakeWaiter second_lock_holder; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 231 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint( |
| 232 | DummyEndpoint(), &second_lock_holder), |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 233 | IsOk()); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 234 | |
| 235 | UnlockDummyEndpoint(1); |
| 236 | } |
| 237 | |
| 238 | TEST_F(WebSocketEndpointLockManagerTest, RememberSocketWorks) { |
| 239 | FakeWaiter waiters[2]; |
| 240 | FakeStreamSocket dummy_socket; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 241 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 242 | &waiters[0]), |
| 243 | IsOk()); |
| 244 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 245 | DummyEndpoint(), &waiters[1])); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 246 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 247 | websocket_endpoint_lock_manager_.RememberSocket(&dummy_socket, |
| 248 | DummyEndpoint()); |
| 249 | websocket_endpoint_lock_manager_.UnlockSocket(&dummy_socket); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 250 | RunUntilIdle(); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 251 | EXPECT_TRUE(waiters[1].called()); |
| 252 | |
| 253 | UnlockDummyEndpoint(1); |
| 254 | } |
| 255 | |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 256 | // UnlockEndpoint() should cause any sockets remembered for this endpoint |
| 257 | // to be forgotten. |
| 258 | TEST_F(WebSocketEndpointLockManagerTest, SocketAssociationForgottenOnUnlock) { |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 259 | FakeWaiter waiter; |
| 260 | FakeStreamSocket dummy_socket; |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 261 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 262 | EXPECT_THAT( |
| 263 | websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), &waiter), |
| 264 | IsOk()); |
| 265 | websocket_endpoint_lock_manager_.RememberSocket(&dummy_socket, |
| 266 | DummyEndpoint()); |
| 267 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 268 | RunUntilIdle(); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 269 | EXPECT_TRUE(websocket_endpoint_lock_manager_.IsEmpty()); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // When ownership of the endpoint is passed to a new waiter, the new waiter can |
| 273 | // call RememberSocket() again. |
| 274 | TEST_F(WebSocketEndpointLockManagerTest, NextWaiterCanCallRememberSocketAgain) { |
| 275 | FakeWaiter waiters[2]; |
| 276 | FakeStreamSocket dummy_sockets[2]; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 277 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 278 | &waiters[0]), |
| 279 | IsOk()); |
| 280 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 281 | DummyEndpoint(), &waiters[1])); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 282 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 283 | websocket_endpoint_lock_manager_.RememberSocket(&dummy_sockets[0], |
| 284 | DummyEndpoint()); |
| 285 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 286 | RunUntilIdle(); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 287 | EXPECT_TRUE(waiters[1].called()); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 288 | websocket_endpoint_lock_manager_.RememberSocket(&dummy_sockets[1], |
| 289 | DummyEndpoint()); |
[email protected] | 6bcd67d | 2014-08-05 16:31:24 | [diff] [blame] | 290 | |
| 291 | UnlockDummyEndpoint(1); |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 292 | } |
| 293 | |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 294 | // Calling UnlockSocket() after UnlockEndpoint() does nothing. |
| 295 | TEST_F(WebSocketEndpointLockManagerTest, |
| 296 | UnlockSocketAfterUnlockEndpointDoesNothing) { |
| 297 | FakeWaiter waiters[3]; |
| 298 | FakeStreamSocket dummy_socket; |
| 299 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 300 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 301 | &waiters[0]), |
| 302 | IsOk()); |
| 303 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 304 | DummyEndpoint(), &waiters[1])); |
| 305 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 306 | DummyEndpoint(), &waiters[2])); |
| 307 | websocket_endpoint_lock_manager_.RememberSocket(&dummy_socket, |
| 308 | DummyEndpoint()); |
| 309 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
| 310 | websocket_endpoint_lock_manager_.UnlockSocket(&dummy_socket); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 311 | RunUntilIdle(); |
| 312 | EXPECT_TRUE(waiters[1].called()); |
| 313 | EXPECT_FALSE(waiters[2].called()); |
| 314 | |
| 315 | UnlockDummyEndpoint(2); |
| 316 | } |
| 317 | |
| 318 | // UnlockEndpoint() should always be asynchronous. |
| 319 | TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsAsynchronous) { |
| 320 | FakeWaiter waiters[2]; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 321 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 322 | &waiters[0]), |
| 323 | IsOk()); |
| 324 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 325 | DummyEndpoint(), &waiters[1])); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 326 | |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 327 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 328 | EXPECT_FALSE(waiters[1].called()); |
| 329 | RunUntilIdle(); |
| 330 | EXPECT_TRUE(waiters[1].called()); |
| 331 | |
| 332 | UnlockDummyEndpoint(1); |
| 333 | } |
| 334 | |
| 335 | // UnlockEndpoint() should normally have a delay. |
| 336 | TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsDelayed) { |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 337 | using base::TimeTicks; |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 338 | |
ricea | ba68362 | 2015-02-01 18:16:53 | [diff] [blame] | 339 | // This 1ms delay is too short for very slow environments (usually those |
| 340 | // running memory checkers). In those environments, the code takes >1ms to run |
| 341 | // and no delay is needed. Rather than increase the delay and slow down the |
| 342 | // test everywhere, the test doesn't explicitly verify that a delay has been |
| 343 | // applied. Instead it just verifies that the whole thing took >=1ms. 1ms is |
| 344 | // easily enough for normal compiles even on Android, so the fact that there |
| 345 | // is a delay is still checked on every platform. |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 346 | const base::TimeDelta unlock_delay = base::TimeDelta::FromMilliseconds(1); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 347 | websocket_endpoint_lock_manager_.SetUnlockDelayForTesting(unlock_delay); |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 348 | FakeWaiter fake_waiter; |
| 349 | BlockingWaiter blocking_waiter; |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 350 | EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), |
| 351 | &fake_waiter), |
| 352 | IsOk()); |
| 353 | EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint( |
| 354 | DummyEndpoint(), &blocking_waiter)); |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 355 | |
| 356 | TimeTicks before_unlock = TimeTicks::Now(); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 357 | websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint()); |
ricea | 6da0ff6 | 2015-01-27 08:15:19 | [diff] [blame] | 358 | blocking_waiter.WaitForLock(); |
| 359 | TimeTicks after_unlock = TimeTicks::Now(); |
| 360 | EXPECT_GE(after_unlock - before_unlock, unlock_delay); |
Bence Béky | da280c6 | 2018-04-12 15:08:37 | [diff] [blame^] | 361 | websocket_endpoint_lock_manager_.SetUnlockDelayForTesting(base::TimeDelta()); |
ricea | bafe0f2 | 2015-01-23 05:31:25 | [diff] [blame] | 362 | UnlockDummyEndpoint(1); |
| 363 | } |
| 364 | |
[email protected] | 65486614 | 2014-06-24 22:53:31 | [diff] [blame] | 365 | } // namespace |
| 366 | |
| 367 | } // namespace net |