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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <string>
#include "base/message_loop/message_loop.h"
#include "components/web_cache/browser/web_cache_manager.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using base::TimeDelta;
using content::BrowserThread;
namespace web_cache {
class WebCacheManagerTest : public testing::Test {
protected:
typedef WebCacheManager::StatsMap StatsMap;
typedef WebCacheManager::Allocation Allocation;
typedef WebCacheManager::AllocationStrategy AllocationStrategy;
static const int kRendererID;
static const int kRendererID2;
static const WebCacheManager::RendererInfo kStats;
static const WebCacheManager::RendererInfo kStats2;
WebCacheManagerTest()
: ui_thread_(BrowserThread::UI, &message_loop_) {
}
// Thunks to access protected members of WebCacheManager
static std::map<int, WebCacheManager::RendererInfo>& stats(
WebCacheManager* h) {
return h->stats_;
}
static void SimulateInactivity(WebCacheManager* h, int renderer_id) {
stats(h)[renderer_id].access = Time::Now() - TimeDelta::FromMinutes(
WebCacheManager::kRendererInactiveThresholdMinutes);
h->FindInactiveRenderers();
}
static std::set<int>& active_renderers(WebCacheManager* h) {
return h->active_renderers_;
}
static std::set<int>& inactive_renderers(WebCacheManager* h) {
return h->inactive_renderers_;
}
static void GatherStats(WebCacheManager* h,
std::set<int> renderers,
WebCacheManager::RendererInfo* stats) {
memset(stats, 0, sizeof(WebCacheManager::RendererInfo));
h->GatherStats(renderers, &stats->capacity, &stats->size);
}
static uint64_t GetSize(int tactic,
const WebCacheManager::RendererInfo& stats) {
return WebCacheManager::GetSize(
static_cast<WebCacheManager::AllocationTactic>(tactic), stats.size);
}
static bool AttemptTactic(WebCacheManager* h,
int active_tactic,
const WebCacheManager::RendererInfo& active_stats,
int inactive_tactic,
const WebCacheManager::RendererInfo& inactive_stats,
std::list<std::pair<int, uint64_t>>* strategy) {
return h->AttemptTactic(
static_cast<WebCacheManager::AllocationTactic>(active_tactic),
active_stats.size,
static_cast<WebCacheManager::AllocationTactic>(inactive_tactic),
inactive_stats.size, strategy);
}
static void AddToStrategy(WebCacheManager* h,
std::set<int> renderers,
int tactic,
uint64_t extra_bytes_to_allocate,
std::list<std::pair<int, uint64_t>>* strategy) {
h->AddToStrategy(renderers,
static_cast<WebCacheManager::AllocationTactic>(tactic),
extra_bytes_to_allocate,
strategy);
}
static bool RendererInfoEqual(const WebCacheManager::RendererInfo& lhs,
const WebCacheManager::RendererInfo& rhs) {
return lhs.capacity == rhs.capacity && lhs.size == rhs.size;
}
enum {
DIVIDE_EVENLY = WebCacheManager::DIVIDE_EVENLY,
KEEP_CURRENT_WITH_HEADROOM = WebCacheManager::KEEP_CURRENT_WITH_HEADROOM,
KEEP_CURRENT = WebCacheManager::KEEP_CURRENT,
};
WebCacheManager* manager() { return &manager_; }
private:
WebCacheManager manager_;
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
};
// static
const int WebCacheManagerTest::kRendererID = 146;
// static
const int WebCacheManagerTest::kRendererID2 = 245;
// static
const WebCacheManager::RendererInfo WebCacheManagerTest::kStats = {
base::Time(), 1024 * 1024, 256 * 1024 + 512,
};
// static
const WebCacheManager::RendererInfo WebCacheManagerTest::kStats2 = {
base::Time(), 2 * 1024 * 1024, 2 * 256 * 1024 + 2 * 512,
};
TEST_F(WebCacheManagerTest, AddRemoveRendererTest) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
manager()->Add(kRendererID);
EXPECT_EQ(1U, active_renderers(manager()).count(kRendererID));
EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID));
manager()->Remove(kRendererID);
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
}
TEST_F(WebCacheManagerTest, ActiveInactiveTest) {
manager()->Add(kRendererID);
manager()->ObserveActivity(kRendererID);
EXPECT_EQ(1U, active_renderers(manager()).count(kRendererID));
EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID));
SimulateInactivity(manager(), kRendererID);
EXPECT_EQ(0U, active_renderers(manager()).count(kRendererID));
EXPECT_EQ(1U, inactive_renderers(manager()).count(kRendererID));
manager()->ObserveActivity(kRendererID);
EXPECT_EQ(1U, active_renderers(manager()).count(kRendererID));
EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID));
manager()->Remove(kRendererID);
}
TEST_F(WebCacheManagerTest, ObserveStatsTest) {
manager()->Add(kRendererID);
EXPECT_EQ(1U, stats(manager()).size());
manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
EXPECT_EQ(1U, stats(manager()).size());
EXPECT_TRUE(RendererInfoEqual(kStats, stats(manager())[kRendererID]));
manager()->Remove(kRendererID);
}
TEST_F(WebCacheManagerTest, SetGlobalSizeLimitTest) {
uint64_t limit = manager()->GetDefaultGlobalSizeLimit();
manager()->SetGlobalSizeLimit(limit);
EXPECT_EQ(limit, manager()->global_size_limit());
manager()->SetGlobalSizeLimit(0);
EXPECT_EQ(0U, manager()->global_size_limit());
}
TEST_F(WebCacheManagerTest, GatherStatsTest) {
manager()->Add(kRendererID);
manager()->Add(kRendererID2);
manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
std::set<int> renderer_set;
renderer_set.insert(kRendererID);
WebCacheManager::RendererInfo stats;
GatherStats(manager(), renderer_set, &stats);
EXPECT_TRUE(RendererInfoEqual(kStats, stats));
renderer_set.insert(kRendererID2);
GatherStats(manager(), renderer_set, &stats);
WebCacheManager::RendererInfo expected_stats = kStats;
expected_stats.capacity += kStats2.capacity;
expected_stats.size += kStats2.size;
EXPECT_TRUE(RendererInfoEqual(expected_stats, stats));
manager()->Remove(kRendererID);
manager()->Remove(kRendererID2);
}
TEST_F(WebCacheManagerTest, GetSizeTest) {
EXPECT_EQ(0U, GetSize(DIVIDE_EVENLY, kStats));
EXPECT_LT(256 * 1024u + 512, GetSize(KEEP_CURRENT_WITH_HEADROOM, kStats));
EXPECT_EQ(256 * 1024u + 512, GetSize(KEEP_CURRENT, kStats));
}
TEST_F(WebCacheManagerTest, AttemptTacticTest) {
manager()->Add(kRendererID);
manager()->Add(kRendererID2);
manager()->ObserveActivity(kRendererID);
SimulateInactivity(manager(), kRendererID2);
manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
AllocationStrategy strategy;
manager()->SetGlobalSizeLimit(kStats.size + kStats2.size - 1);
EXPECT_FALSE(AttemptTactic(manager(),
KEEP_CURRENT,
kStats,
KEEP_CURRENT,
kStats2,
&strategy));
EXPECT_TRUE(strategy.empty());
manager()->SetGlobalSizeLimit(kStats.size + kStats2.size);
EXPECT_TRUE(AttemptTactic(manager(), KEEP_CURRENT, kStats, KEEP_CURRENT,
kStats2, &strategy));
EXPECT_EQ(2U, strategy.size());
AllocationStrategy::iterator iter = strategy.begin();
while (iter != strategy.end()) {
if (iter->first == kRendererID)
EXPECT_LE(kStats.size, iter->second);
else if (iter->first == kRendererID2)
EXPECT_LE(kStats2.size, iter->second);
else
ADD_FAILURE(); // Unexpected entry in strategy.
++iter;
}
manager()->Remove(kRendererID);
manager()->Remove(kRendererID2);
}
TEST_F(WebCacheManagerTest, AddToStrategyTest) {
manager()->Add(kRendererID);
manager()->Add(kRendererID2);
std::set<int> renderer_set;
renderer_set.insert(kRendererID);
renderer_set.insert(kRendererID2);
manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
const uint64_t kExtraBytesToAllocate = 10 * 1024;
AllocationStrategy strategy;
AddToStrategy(manager(),
renderer_set,
KEEP_CURRENT,
kExtraBytesToAllocate,
&strategy);
EXPECT_EQ(2U, strategy.size());
uint64_t total_bytes = 0;
AllocationStrategy::iterator iter = strategy.begin();
while (iter != strategy.end()) {
total_bytes += iter->second;
if (iter->first == kRendererID)
EXPECT_LE(kStats.size, iter->second);
else if (iter->first == kRendererID2)
EXPECT_LE(kStats2.size, iter->second);
else
ADD_FAILURE(); // Unexpected entry in strategy.
++iter;
}
uint64_t expected_total_bytes =
kExtraBytesToAllocate + kStats.size + kStats2.size;
EXPECT_GE(expected_total_bytes, total_bytes);
manager()->Remove(kRendererID);
manager()->Remove(kRendererID2);
}
// Regression test for http://crbug.com/12362.
// There are three operations in the following order will cause the crash:
// Remove(kRendererID) -> ObserveActivity(kRendererID) -> Remove(kRendererID2)
// To prevent similar failures in the future, 6 tests are added in total to
// cover all the possible orderings of these three operations.
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_1) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
// The following order will cause a crash in http://crbug.com/12362.
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
}
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_2) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
manager()->ReviseAllocationStrategy();
}
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_3) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
EXPECT_EQ(0U, inactive_renderers(manager()).size());
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
}
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_4) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
EXPECT_EQ(0U, inactive_renderers(manager()).size());
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
}
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_5) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
}
TEST_F(WebCacheManagerTest,
CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_6) {
EXPECT_EQ(0U, active_renderers(manager()).size());
EXPECT_EQ(0U, inactive_renderers(manager()).size());
// Add, Remove, and ObserveActivity trigger deferred
// calls to ReviseAllocationStrategy and that we call it directly after each
// operation to sidestep the need to wait for an unobservable background
// operation.
manager()->Add(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->Add(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID2);
manager()->ReviseAllocationStrategy();
manager()->Remove(kRendererID);
manager()->ReviseAllocationStrategy();
manager()->ObserveActivity(kRendererID);
manager()->ReviseAllocationStrategy();
}
} // namespace web_cache
|