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

Side by Side Diff: blimp/client/app/blimp_discardable_memory_allocator_unittest.cc

Issue 2624903006: Remove all blimp client code. (Closed)
Patch Set: Update buildbot configuration Created 3 years, 11 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
(Empty)
1 // Copyright 2016 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 "blimp/client/app/blimp_discardable_memory_allocator.h"
6
7 #include <stddef.h>
8
9 #include "base/memory/discardable_memory.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/test/gtest_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace blimp {
15 namespace client {
16 namespace {
17
18 const size_t kOneKilobyte = 1024;
19 const size_t kAlmostOneMegabyte = 1023 * kOneKilobyte;
20 const size_t kOneMegabyte = 1024 * kOneKilobyte;
21
22 TEST(BlimpDiscardableMemoryAllocator, Basic) {
23 BlimpDiscardableMemoryAllocator allocator(kOneMegabyte);
24 std::unique_ptr<base::DiscardableMemory> chunk;
25 // Make sure the chunk is locked when allocated. In debug mode, we will
26 // dcheck.
27 chunk = allocator.AllocateLockedDiscardableMemory(kOneKilobyte);
28 chunk->Unlock();
29
30 // Make sure we can lock a chunk.
31 EXPECT_TRUE(chunk->Lock());
32 chunk->Unlock();
33 }
34
35 TEST(BlimpDiscardableMemoryAllocator, DiscardChunks) {
36 BlimpDiscardableMemoryAllocator allocator(kOneMegabyte);
37
38 std::unique_ptr<base::DiscardableMemory> chunk_to_remove =
39 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
40 chunk_to_remove->Unlock();
41
42 // Allocating a second chunk should deallocate the first one due to memory
43 // pressure, since we only have one megabyte available.
44 std::unique_ptr<base::DiscardableMemory> chunk_to_keep =
45 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
46
47 // Fail to get a lock because allocating the second chunk removed the first.
48 EXPECT_FALSE(chunk_to_remove->Lock());
49
50 chunk_to_keep->Unlock();
51 }
52
53 TEST(BlimpDiscardableMemoyAllocator, DiscardChunksOnUnlock) {
54 BlimpDiscardableMemoryAllocator allocator(kOneMegabyte);
55
56 std::unique_ptr<base::DiscardableMemory> chunk_to_remove =
57 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
58 std::unique_ptr<base::DiscardableMemory> chunk_to_keep =
59 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
60
61 // We should have both the allocated chunks.
62 EXPECT_NE(nullptr, chunk_to_remove->data());
63 EXPECT_NE(nullptr, chunk_to_keep->data());
64
65 // Unlocking the first chunk should deallocate it due to memory pressure.
66 chunk_to_remove->Unlock();
67 EXPECT_FALSE(chunk_to_remove->Lock());
68
69 chunk_to_keep->Unlock();
70 }
71
72 TEST(BlimpDiscardableMemoryAllocator, DontDiscardLiveChunks) {
73 BlimpDiscardableMemoryAllocator allocator(kOneMegabyte);
74
75 std::unique_ptr<base::DiscardableMemory> chunk_one =
76 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
77 std::unique_ptr<base::DiscardableMemory> chunk_two =
78 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
79 std::unique_ptr<base::DiscardableMemory> chunk_three =
80 allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
81
82 // These accesses will fail if the underlying weak ptr has been deallocated.
83 EXPECT_NE(nullptr, chunk_one->data());
84 EXPECT_NE(nullptr, chunk_two->data());
85 EXPECT_NE(nullptr, chunk_three->data());
86
87 chunk_one->Unlock();
88 chunk_two->Unlock();
89 chunk_three->Unlock();
90 }
91
92 #if DCHECK_IS_ON()
93 TEST(BlimpDiscardableMemoryAllocator, DiscardWhileUnlockedLive) {
94 std::unique_ptr<BlimpDiscardableMemoryAllocator> allocator =
95 base::MakeUnique<BlimpDiscardableMemoryAllocator>();
96
97 std::unique_ptr<base::DiscardableMemory> chunk_one =
98 allocator->AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
99
100 // These accesses will fail if the underlying weak ptr has been deallocated.
101 EXPECT_NE(nullptr, chunk_one->data());
102
103 chunk_one->Unlock();
104
105 allocator.reset();
106
107 EXPECT_DCHECK_DEATH(chunk_one.reset());
108
109 // Leak the DiscardableMemory instance, to avoid triggering the DCHECK.
110 chunk_one.release();
111 }
112 #endif // DCHECK_IS_ON()
113
114 } // namespace
115 } // namespace client
116 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/client/app/blimp_discardable_memory_allocator.cc ('k') | blimp/client/app/blimp_startup.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698