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

Side by Side Diff: appengine/monorail/services/test/cachemanager_svc_test.py

Issue 1868553004: Open Source Monorail (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 8 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 govered by a BSD-style
3 # license that can be found in the LICENSE file or at
4 # https://developers.google.com/open-source/licenses/bsd
5
6 """Tests for the cachemanager service."""
7
8 import unittest
9
10 import mox
11
12 from framework import sql
13 from services import cachemanager_svc
14 from services import caches
15 from services import service_manager
16 from testing import testing_helpers
17
18
19 class CacheManagerServiceTest(unittest.TestCase):
20
21 def setUp(self):
22 self.mox = mox.Mox()
23 self.cnxn = 'fake connection'
24 self.cache_manager = cachemanager_svc.CacheManager()
25 self.cache_manager.invalidate_tbl = self.mox.CreateMock(
26 sql.SQLTableManager)
27
28 def tearDown(self):
29 self.mox.UnsetStubs()
30 self.mox.ResetAll()
31
32 def testMakeCache(self):
33 ram_cache = self.cache_manager.MakeCache('issue')
34 self.assertTrue(isinstance(ram_cache, caches.RamCache))
35 self.assertTrue(ram_cache in self.cache_manager.cache_registry['issue'])
36
37 def testMakeCache_UnknownKind(self):
38 self.assertRaises(AssertionError, self.cache_manager.MakeCache, 'foo')
39
40 def testProcessInvalidateRows_Empty(self):
41 rows = []
42 self.cache_manager._ProcessInvalidationRows(rows)
43 self.assertEqual(0, self.cache_manager.processed_invalidations_up_to)
44
45 def testProcessInvalidateRows_Some(self):
46 ram_cache = self.cache_manager.MakeCache('issue')
47 ram_cache.CacheAll({
48 33: 'issue 33',
49 34: 'issue 34',
50 })
51 rows = [(1, 'issue', 34),
52 (2, 'project', 789),
53 (3, 'issue', 39)]
54 self.cache_manager._ProcessInvalidationRows(rows)
55 self.assertEqual(3, self.cache_manager.processed_invalidations_up_to)
56 self.assertTrue(ram_cache.HasItem(33))
57 self.assertFalse(ram_cache.HasItem(34))
58
59 def testProcessInvalidateRows_All(self):
60 ram_cache = self.cache_manager.MakeCache('issue')
61 ram_cache.CacheAll({
62 33: 'issue 33',
63 34: 'issue 34',
64 })
65 rows = [(991, 'issue', 34),
66 (992, 'project', 789),
67 (993, 'issue', cachemanager_svc.INVALIDATE_ALL_KEYS)]
68 self.cache_manager._ProcessInvalidationRows(rows)
69 self.assertEqual(993, self.cache_manager.processed_invalidations_up_to)
70 self.assertEqual({}, ram_cache.cache)
71
72 def SetUpDoDistributedInvalidation(self, rows):
73 self.cache_manager.invalidate_tbl.Select(
74 self.cnxn, cols=['timestep', 'kind', 'cache_key'],
75 where=[('timestep > %s', [0])],
76 order_by=[('timestep DESC', [])],
77 limit=cachemanager_svc.MAX_INVALIDATE_ROWS_TO_CONSIDER
78 ).AndReturn(rows)
79
80 def testDoDistributedInvalidation_Empty(self):
81 rows = []
82 self.SetUpDoDistributedInvalidation(rows)
83 self.mox.ReplayAll()
84 self.cache_manager.DoDistributedInvalidation(self.cnxn)
85 self.mox.VerifyAll()
86 self.assertEqual(0, self.cache_manager.processed_invalidations_up_to)
87
88 def testDoDistributedInvalidation_Some(self):
89 ram_cache = self.cache_manager.MakeCache('issue')
90 ram_cache.CacheAll({
91 33: 'issue 33',
92 34: 'issue 34',
93 })
94 rows = [(1, 'issue', 34),
95 (2, 'project', 789),
96 (3, 'issue', 39)]
97 self.SetUpDoDistributedInvalidation(rows)
98 self.mox.ReplayAll()
99 self.cache_manager.DoDistributedInvalidation(self.cnxn)
100 self.mox.VerifyAll()
101 self.assertEqual(3, self.cache_manager.processed_invalidations_up_to)
102 self.assertTrue(ram_cache.HasItem(33))
103 self.assertFalse(ram_cache.HasItem(34))
104
105 def testStoreInvalidateRows_UnknownKind(self):
106 self.assertRaises(
107 AssertionError,
108 self.cache_manager.StoreInvalidateRows, self.cnxn, 'foo', [1, 2])
109
110 def SetUpStoreInvalidateRows(self, rows):
111 self.cache_manager.invalidate_tbl.InsertRows(
112 self.cnxn, ['kind', 'cache_key'], rows)
113
114 def testStoreInvalidateRows(self):
115 rows = [('issue', 1), ('issue', 2)]
116 self.SetUpStoreInvalidateRows(rows)
117 self.mox.ReplayAll()
118 self.cache_manager.StoreInvalidateRows(self.cnxn, 'issue', [1, 2])
119 self.mox.VerifyAll()
120
121 def SetUpStoreInvalidateAll(self, kind):
122 self.cache_manager.invalidate_tbl.InsertRow(
123 self.cnxn, kind=kind, cache_key=cachemanager_svc.INVALIDATE_ALL_KEYS,
124 ).AndReturn(44)
125 self.cache_manager.invalidate_tbl.Delete(
126 self.cnxn, kind=kind, where=[('timestep < %s', [44])])
127
128 def testStoreInvalidateAll(self):
129 self.SetUpStoreInvalidateAll('issue')
130 self.mox.ReplayAll()
131 self.cache_manager.StoreInvalidateAll(self.cnxn, 'issue')
132 self.mox.VerifyAll()
133
134
135 class RamCacheConsolidateTest(unittest.TestCase):
136
137 def setUp(self):
138 self.mox = mox.Mox()
139 self.cnxn = 'fake connection'
140 self.cache_manager = cachemanager_svc.CacheManager()
141 self.cache_manager.invalidate_tbl = self.mox.CreateMock(
142 sql.SQLTableManager)
143 self.services = service_manager.Services(
144 cache_manager=self.cache_manager)
145 self.servlet = cachemanager_svc.RamCacheConsolidate(
146 'req', 'res', services=self.services)
147
148 def testHandleRequest_NothingToDo(self):
149 mr = testing_helpers.MakeMonorailRequest()
150 self.cache_manager.invalidate_tbl.SelectValue(
151 mr.cnxn, 'COUNT(*)').AndReturn(112)
152 self.cache_manager.invalidate_tbl.SelectValue(
153 mr.cnxn, 'COUNT(*)').AndReturn(112)
154 self.mox.ReplayAll()
155
156 json_data = self.servlet.HandleRequest(mr)
157 self.mox.VerifyAll()
158 self.assertEqual(json_data['old_count'], 112)
159 self.assertEqual(json_data['new_count'], 112)
160
161 def testHandleRequest_Truncate(self):
162 mr = testing_helpers.MakeMonorailRequest()
163 self.cache_manager.invalidate_tbl.SelectValue(
164 mr.cnxn, 'COUNT(*)').AndReturn(4012)
165 self.cache_manager.invalidate_tbl.Select(
166 mr.cnxn, ['timestep'],
167 order_by=[('timestep DESC', [])],
168 limit=cachemanager_svc.MAX_INVALIDATE_ROWS_TO_CONSIDER
169 ).AndReturn([[3012]]) # Actual would be 1000 rows ending with 3012.
170 self.cache_manager.invalidate_tbl.Delete(
171 mr.cnxn, where=[('timestep < %s', [3012])])
172 self.cache_manager.invalidate_tbl.SelectValue(
173 mr.cnxn, 'COUNT(*)').AndReturn(1000)
174 self.mox.ReplayAll()
175
176 json_data = self.servlet.HandleRequest(mr)
177 self.mox.VerifyAll()
178 self.assertEqual(json_data['old_count'], 4012)
179 self.assertEqual(json_data['new_count'], 1000)
180
181
182 if __name__ == '__main__':
183 unittest.main()
OLDNEW
« no previous file with comments | « appengine/monorail/services/test/api_svc_v1_test.py ('k') | appengine/monorail/services/test/caches_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698