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.
5 #include "components/sync_driver/tab_node_pool.h"
7 #include "sync/api/sync_change.h"
8 #include "sync/protocol/session_specifics.pb.h"
9 #include "sync/protocol/sync.pb.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace browser_sync
{
14 class SyncTabNodePoolTest
: public testing::Test
{
16 SyncTabNodePoolTest() { pool_
.SetMachineTag("tag"); }
18 int GetMaxUsedTabNodeId() const { return pool_
.max_used_tab_node_id_
; }
20 void AddFreeTabNodes(size_t size
, const int node_ids
[]);
25 void SyncTabNodePoolTest::AddFreeTabNodes(
26 size_t size
, const int node_ids
[]) {
27 for (size_t i
= 0; i
< size
; ++i
) {
28 pool_
.free_nodes_pool_
.insert(node_ids
[i
]);
34 TEST_F(SyncTabNodePoolTest
, TabNodeIdIncreases
) {
35 syncer::SyncChangeList changes
;
36 // max_used_tab_node_ always increases.
38 EXPECT_EQ(10, GetMaxUsedTabNodeId());
40 EXPECT_EQ(10, GetMaxUsedTabNodeId());
41 pool_
.AddTabNode(1000);
42 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
43 pool_
.ReassociateTabNode(1000, 1);
44 pool_
.ReassociateTabNode(5, 2);
45 pool_
.ReassociateTabNode(10, 3);
46 // Freeing a tab node does not change max_used_tab_node_id_.
47 pool_
.FreeTabNode(1000, &changes
);
48 EXPECT_TRUE(changes
.empty());
49 pool_
.FreeTabNode(5, &changes
);
50 EXPECT_TRUE(changes
.empty());
51 pool_
.FreeTabNode(10, &changes
);
52 EXPECT_TRUE(changes
.empty());
53 for (int i
= 0; i
< 3; ++i
) {
54 pool_
.AssociateTabNode(pool_
.GetFreeTabNode(&changes
), i
+ 1);
55 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
57 EXPECT_TRUE(changes
.empty());
58 EXPECT_EQ(1000, GetMaxUsedTabNodeId());
59 EXPECT_TRUE(pool_
.Empty());
62 TEST_F(SyncTabNodePoolTest
, OldTabNodesAddAndRemove
) {
63 syncer::SyncChangeList changes
;
64 // VerifyOldTabNodes are added.
67 EXPECT_EQ(2u, pool_
.Capacity());
68 EXPECT_TRUE(pool_
.Empty());
69 EXPECT_TRUE(pool_
.IsUnassociatedTabNode(1));
70 EXPECT_TRUE(pool_
.IsUnassociatedTabNode(2));
71 pool_
.ReassociateTabNode(1, 2);
72 EXPECT_TRUE(pool_
.Empty());
73 pool_
.AssociateTabNode(2, 3);
74 EXPECT_FALSE(pool_
.IsUnassociatedTabNode(1));
75 EXPECT_FALSE(pool_
.IsUnassociatedTabNode(2));
76 pool_
.FreeTabNode(2, &changes
);
77 EXPECT_TRUE(changes
.empty());
78 // 2 should be returned to free node pool_.
79 EXPECT_EQ(2u, pool_
.Capacity());
80 // Should be able to free 1.
81 pool_
.FreeTabNode(1, &changes
);
82 EXPECT_FALSE(pool_
.Empty());
83 EXPECT_TRUE(pool_
.Full());
84 EXPECT_EQ(1, pool_
.GetFreeTabNode(&changes
));
85 EXPECT_TRUE(changes
.empty());
86 pool_
.AssociateTabNode(1, 1);
87 EXPECT_EQ(2, pool_
.GetFreeTabNode(&changes
));
88 EXPECT_TRUE(changes
.empty());
89 pool_
.AssociateTabNode(2, 1);
90 EXPECT_TRUE(pool_
.Empty());
91 EXPECT_FALSE(pool_
.Full());
92 EXPECT_FALSE(pool_
.Full());
95 TEST_F(SyncTabNodePoolTest
, OldTabNodesReassociation
) {
96 // VerifyOldTabNodes are reassociated correctly.
100 EXPECT_EQ(3u, pool_
.Capacity());
101 EXPECT_TRUE(pool_
.Empty());
102 EXPECT_TRUE(pool_
.IsUnassociatedTabNode(4));
103 pool_
.ReassociateTabNode(4, 5);
104 pool_
.AssociateTabNode(5, 6);
105 pool_
.AssociateTabNode(6, 7);
107 syncer::SyncChangeList changes
;
108 pool_
.FreeTabNode(5, &changes
);
109 pool_
.FreeTabNode(6, &changes
);
110 EXPECT_TRUE(changes
.empty());
111 // 5 and 6 nodes should not be unassociated.
112 EXPECT_FALSE(pool_
.IsUnassociatedTabNode(5));
113 EXPECT_FALSE(pool_
.IsUnassociatedTabNode(6));
114 // Free node pool should have 5 and 6.
115 EXPECT_FALSE(pool_
.Empty());
116 EXPECT_EQ(3u, pool_
.Capacity());
119 pool_
.FreeTabNode(4, &changes
);
120 EXPECT_TRUE(changes
.empty());
121 EXPECT_TRUE(pool_
.Full());
122 std::set
<int> free_sync_ids
;
123 for (int i
= 0; i
< 3; ++i
) {
124 free_sync_ids
.insert(pool_
.GetFreeTabNode(&changes
));
125 // GetFreeTabNode will return the same value till the node is
127 pool_
.AssociateTabNode(pool_
.GetFreeTabNode(&changes
), i
+ 1);
130 EXPECT_TRUE(pool_
.Empty());
131 EXPECT_EQ(3u, free_sync_ids
.size());
132 EXPECT_EQ(1u, free_sync_ids
.count(4));
133 EXPECT_EQ(1u, free_sync_ids
.count(5));
134 EXPECT_EQ(1u, free_sync_ids
.count(6));
137 TEST_F(SyncTabNodePoolTest
, Init
) {
138 EXPECT_TRUE(pool_
.Empty());
139 EXPECT_TRUE(pool_
.Full());
142 TEST_F(SyncTabNodePoolTest
, AddGet
) {
143 syncer::SyncChangeList changes
;
144 int free_nodes
[] = {5, 10};
145 AddFreeTabNodes(2, free_nodes
);
147 EXPECT_EQ(2U, pool_
.Capacity());
148 EXPECT_EQ(5, pool_
.GetFreeTabNode(&changes
));
149 pool_
.AssociateTabNode(5, 1);
150 EXPECT_FALSE(pool_
.Empty());
151 EXPECT_FALSE(pool_
.Full());
152 EXPECT_EQ(2U, pool_
.Capacity());
153 // 5 is now used, should return 10.
154 EXPECT_EQ(10, pool_
.GetFreeTabNode(&changes
));
157 TEST_F(SyncTabNodePoolTest
, All
) {
158 syncer::SyncChangeList changes
;
159 EXPECT_TRUE(pool_
.Empty());
160 EXPECT_TRUE(pool_
.Full());
161 EXPECT_EQ(0U, pool_
.Capacity());
163 // GetFreeTabNode returns the lowest numbered free node.
164 EXPECT_EQ(0, pool_
.GetFreeTabNode(&changes
));
165 EXPECT_EQ(1U, changes
.size());
166 EXPECT_FALSE(pool_
.Empty());
167 EXPECT_TRUE(pool_
.Full());
168 EXPECT_EQ(1U, pool_
.Capacity());
170 // Associate 5, next free node should be 10.
171 pool_
.AssociateTabNode(0, 1);
172 EXPECT_EQ(1, pool_
.GetFreeTabNode(&changes
));
173 EXPECT_EQ(2U, changes
.size());
175 pool_
.AssociateTabNode(1, 2);
176 EXPECT_TRUE(pool_
.Empty());
177 EXPECT_FALSE(pool_
.Full());
178 EXPECT_EQ(2U, pool_
.Capacity());
179 // Release them in reverse order.
180 pool_
.FreeTabNode(1, &changes
);
181 pool_
.FreeTabNode(0, &changes
);
182 EXPECT_EQ(2U, pool_
.Capacity());
183 EXPECT_FALSE(pool_
.Empty());
184 EXPECT_TRUE(pool_
.Full());
185 EXPECT_EQ(0, pool_
.GetFreeTabNode(&changes
));
186 EXPECT_TRUE(changes
.empty());
187 EXPECT_FALSE(pool_
.Empty());
188 EXPECT_TRUE(pool_
.Full());
189 EXPECT_EQ(2U, pool_
.Capacity());
190 EXPECT_FALSE(pool_
.Empty());
191 EXPECT_TRUE(pool_
.Full());
192 pool_
.AssociateTabNode(0, 1);
193 EXPECT_EQ(2U, pool_
.Capacity());
194 EXPECT_EQ(1, pool_
.GetFreeTabNode(&changes
));
195 EXPECT_TRUE(changes
.empty());
196 pool_
.AssociateTabNode(1, 2);
197 EXPECT_TRUE(pool_
.Empty());
198 EXPECT_FALSE(pool_
.Full());
199 EXPECT_EQ(2U, pool_
.Capacity());
200 // Release them again.
201 pool_
.FreeTabNode(1, &changes
);
202 pool_
.FreeTabNode(0, &changes
);
203 EXPECT_FALSE(pool_
.Empty());
204 EXPECT_TRUE(pool_
.Full());
205 EXPECT_EQ(2U, pool_
.Capacity());
207 EXPECT_TRUE(pool_
.Empty());
208 EXPECT_TRUE(pool_
.Full());
209 EXPECT_EQ(0U, pool_
.Capacity());
212 TEST_F(SyncTabNodePoolTest
, GetFreeTabNodeCreate
) {
213 syncer::SyncChangeList changes
;
214 EXPECT_EQ(0, pool_
.GetFreeTabNode(&changes
));
215 EXPECT_TRUE(changes
[0].IsValid());
216 EXPECT_EQ(syncer::SyncChange::ACTION_ADD
, changes
[0].change_type());
217 EXPECT_TRUE(changes
[0].sync_data().IsValid());
218 sync_pb::EntitySpecifics entity
= changes
[0].sync_data().GetSpecifics();
219 sync_pb::SessionSpecifics
specifics(entity
.session());
220 EXPECT_EQ(0, specifics
.tab_node_id());
223 TEST_F(SyncTabNodePoolTest
, TabPoolFreeNodeLimits
) {
224 // Allocate TabNodePool::kFreeNodesHighWatermark + 1 nodes and verify that
225 // freeing the last node reduces the free node pool size to
226 // kFreeNodesLowWatermark.
227 syncer::SyncChangeList changes
;
228 SessionID session_id
;
229 std::vector
<int> used_sync_ids
;
230 for (size_t i
= 1; i
<= TabNodePool::kFreeNodesHighWatermark
+ 1; ++i
) {
231 session_id
.set_id(i
);
232 int sync_id
= pool_
.GetFreeTabNode(&changes
);
233 pool_
.AssociateTabNode(sync_id
, i
);
234 used_sync_ids
.push_back(sync_id
);
237 // Free all except one node.
238 int last_sync_id
= used_sync_ids
.back();
239 used_sync_ids
.pop_back();
241 for (size_t i
= 0; i
< used_sync_ids
.size(); ++i
) {
242 pool_
.FreeTabNode(used_sync_ids
[i
], &changes
);
245 // Except one node all nodes should be in FreeNode pool.
246 EXPECT_FALSE(pool_
.Full());
247 EXPECT_FALSE(pool_
.Empty());
248 // Total capacity = 1 Associated Node + kFreeNodesHighWatermark free node.
249 EXPECT_EQ(TabNodePool::kFreeNodesHighWatermark
+ 1, pool_
.Capacity());
251 // Freeing the last sync node should drop the free nodes to
252 // kFreeNodesLowWatermark.
253 pool_
.FreeTabNode(last_sync_id
, &changes
);
254 EXPECT_FALSE(pool_
.Empty());
255 EXPECT_TRUE(pool_
.Full());
256 EXPECT_EQ(TabNodePool::kFreeNodesLowWatermark
, pool_
.Capacity());
261 } // namespace browser_sync