1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/websocket_endpoint_lock_manager.h"
#include <array>
#include "base/check.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "net/base/ip_address.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/next_proto.h"
#include "net/socket/socket_test_util.h"
#include "net/test/gtest_util.h"
#include "net/test/test_with_task_environment.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::test::IsOk;
namespace net {
namespace {
class FakeWaiter : public WebSocketEndpointLockManager::Waiter {
public:
FakeWaiter() = default;
void GotEndpointLock() override {
CHECK(!called_);
called_ = true;
}
bool called() const { return called_; }
private:
bool called_ = false;
};
class BlockingWaiter : public FakeWaiter {
public:
void WaitForLock() {
while (!called()) {
run_loop_.Run();
}
}
void GotEndpointLock() override {
FakeWaiter::GotEndpointLock();
run_loop_.Quit();
}
private:
base::RunLoop run_loop_;
};
class WebSocketEndpointLockManagerTest : public TestWithTaskEnvironment {
protected:
WebSocketEndpointLockManagerTest() {
websocket_endpoint_lock_manager_.SetUnlockDelayForTesting(
base::TimeDelta());
}
~WebSocketEndpointLockManagerTest() override {
// Permit any pending asynchronous unlock operations to complete.
RunUntilIdle();
// If this check fails then subsequent tests may fail.
CHECK(websocket_endpoint_lock_manager_.IsEmpty());
}
IPEndPoint DummyEndpoint() {
return IPEndPoint(IPAddress::IPv4Localhost(), 80);
}
void UnlockDummyEndpoint(int times) {
for (int i = 0; i < times; ++i) {
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
RunUntilIdle();
}
}
static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); }
WebSocketEndpointLockManager websocket_endpoint_lock_manager_;
};
TEST_F(WebSocketEndpointLockManagerTest, LockEndpointReturnsOkOnce) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
UnlockDummyEndpoint(2);
}
TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledOnOk) {
FakeWaiter waiter;
EXPECT_THAT(
websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), &waiter),
IsOk());
RunUntilIdle();
EXPECT_FALSE(waiter.called());
UnlockDummyEndpoint(1);
}
TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockNotCalledImmediately) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
RunUntilIdle();
EXPECT_FALSE(waiters[1].called());
UnlockDummyEndpoint(2);
}
TEST_F(WebSocketEndpointLockManagerTest, GotEndpointLockCalledWhenUnlocked) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
RunUntilIdle();
EXPECT_TRUE(waiters[1].called());
UnlockDummyEndpoint(1);
}
TEST_F(WebSocketEndpointLockManagerTest,
EndpointUnlockedIfWaiterAlreadyDeleted) {
FakeWaiter first_lock_holder;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&first_lock_holder),
IsOk());
{
FakeWaiter short_lived_waiter;
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &short_lived_waiter));
}
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
RunUntilIdle();
FakeWaiter second_lock_holder;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &second_lock_holder),
IsOk());
UnlockDummyEndpoint(1);
}
TEST_F(WebSocketEndpointLockManagerTest, LockReleaserWorks) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
{
WebSocketEndpointLockManager::LockReleaser releaser(
&websocket_endpoint_lock_manager_, DummyEndpoint());
}
RunUntilIdle();
EXPECT_TRUE(waiters[1].called());
UnlockDummyEndpoint(1);
}
// UnlockEndpoint() should cause any LockReleasers for this endpoint to be
// unregistered.
TEST_F(WebSocketEndpointLockManagerTest, LockReleaserForgottenOnUnlock) {
FakeWaiter waiter;
EXPECT_THAT(
websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(), &waiter),
IsOk());
WebSocketEndpointLockManager::LockReleaser releaser(
&websocket_endpoint_lock_manager_, DummyEndpoint());
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
RunUntilIdle();
EXPECT_TRUE(websocket_endpoint_lock_manager_.IsEmpty());
}
// When ownership of the endpoint is passed to a new waiter, the new waiter can
// construct another LockReleaser.
TEST_F(WebSocketEndpointLockManagerTest, NextWaiterCanCreateLockReleaserAgain) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
WebSocketEndpointLockManager::LockReleaser releaser1(
&websocket_endpoint_lock_manager_, DummyEndpoint());
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
RunUntilIdle();
EXPECT_TRUE(waiters[1].called());
WebSocketEndpointLockManager::LockReleaser releaser2(
&websocket_endpoint_lock_manager_, DummyEndpoint());
UnlockDummyEndpoint(1);
}
// Destroying LockReleaser after UnlockEndpoint() does nothing.
TEST_F(WebSocketEndpointLockManagerTest,
DestroyLockReleaserAfterUnlockEndpointDoesNothing) {
std::array<FakeWaiter, 3> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[2]));
{
WebSocketEndpointLockManager::LockReleaser releaser(
&websocket_endpoint_lock_manager_, DummyEndpoint());
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
}
RunUntilIdle();
EXPECT_TRUE(waiters[1].called());
EXPECT_FALSE(waiters[2].called());
UnlockDummyEndpoint(2);
}
// UnlockEndpoint() should always be asynchronous.
TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsAsynchronous) {
std::array<FakeWaiter, 2> waiters;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&waiters[0]),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &waiters[1]));
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
EXPECT_FALSE(waiters[1].called());
RunUntilIdle();
EXPECT_TRUE(waiters[1].called());
UnlockDummyEndpoint(1);
}
// UnlockEndpoint() should normally have a delay.
TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsDelayed) {
using base::TimeTicks;
// This 1ms delay is too short for very slow environments (usually those
// running memory checkers). In those environments, the code takes >1ms to run
// and no delay is needed. Rather than increase the delay and slow down the
// test everywhere, the test doesn't explicitly verify that a delay has been
// applied. Instead it just verifies that the whole thing took >=1ms. 1ms is
// easily enough for normal compiles even on Android, so the fact that there
// is a delay is still checked on every platform.
const base::TimeDelta unlock_delay = base::Milliseconds(1);
websocket_endpoint_lock_manager_.SetUnlockDelayForTesting(unlock_delay);
FakeWaiter fake_waiter;
BlockingWaiter blocking_waiter;
EXPECT_THAT(websocket_endpoint_lock_manager_.LockEndpoint(DummyEndpoint(),
&fake_waiter),
IsOk());
EXPECT_EQ(ERR_IO_PENDING, websocket_endpoint_lock_manager_.LockEndpoint(
DummyEndpoint(), &blocking_waiter));
TimeTicks before_unlock = TimeTicks::Now();
websocket_endpoint_lock_manager_.UnlockEndpoint(DummyEndpoint());
blocking_waiter.WaitForLock();
TimeTicks after_unlock = TimeTicks::Now();
EXPECT_GE(after_unlock - before_unlock, unlock_delay);
websocket_endpoint_lock_manager_.SetUnlockDelayForTesting(base::TimeDelta());
UnlockDummyEndpoint(1);
}
} // namespace
} // namespace net
|