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

Side by Side Diff: swarm_client/tests/lru_test.py

Issue 69143004: Delete swarm_client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/
Patch Set: Created 7 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « swarm_client/tests/isolateserver_test.py ('k') | swarm_client/tests/net_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import json
7 import logging
8 import os
9 import sys
10 import tempfile
11 import unittest
12
13 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14 sys.path.insert(0, ROOT_DIR)
15
16 from utils import lru
17
18
19 class LRUDictTest(unittest.TestCase):
20 @staticmethod
21 def prepare_lru_dict(keys):
22 """Returns new LRUDict with given |keys| added one by one."""
23 lru_dict = lru.LRUDict()
24 for key in keys:
25 lru_dict.add(key, None)
26 return lru_dict
27
28 def assert_order(self, lru_dict, expected_keys):
29 """Asserts order of keys in |lru_dict| is |expected_keys|.
30
31 expected_keys[0] is supposedly oldest key, expected_keys[-1] is newest.
32
33 Destroys |lru_dict| state in the process.
34 """
35 # Check keys iteration works.
36 self.assertEqual(lru_dict.keys_set(), set(expected_keys))
37
38 # Check pop_oldest returns keys in expected order.
39 actual_keys = []
40 while lru_dict:
41 oldest_key, _ = lru_dict.pop_oldest()
42 actual_keys.append(oldest_key)
43 self.assertEqual(actual_keys, expected_keys)
44
45 def assert_same_data(self, lru_dict, regular_dict):
46 """Asserts that given |lru_dict| contains same data as |regular_dict|."""
47 self.assertEqual(lru_dict.keys_set(), set(regular_dict.keys()))
48 self.assertEqual(set(lru_dict.itervalues()), set(regular_dict.values()))
49
50 for k, v in regular_dict.items():
51 self.assertEqual(lru_dict.get(k), v)
52
53 def test_basic_dict_funcs(self):
54 lru_dict = lru.LRUDict()
55
56 # Add a bunch.
57 data = {1: 'one', 2: 'two', 3: 'three'}
58 for k, v in data.items():
59 lru_dict.add(k, v)
60 # Check its there.
61 self.assert_same_data(lru_dict, data)
62
63 # Replace value.
64 lru_dict.add(1, 'one!!!')
65 data[1] = 'one!!!'
66 self.assert_same_data(lru_dict, data)
67
68 # Check pop works.
69 self.assertEqual(lru_dict.pop(2), 'two')
70 data.pop(2)
71 self.assert_same_data(lru_dict, data)
72
73 # Pop missing key.
74 with self.assertRaises(KeyError):
75 lru_dict.pop(2)
76
77 # Touch has no effect on set of keys and values.
78 lru_dict.touch(1)
79 self.assert_same_data(lru_dict, data)
80
81 # Touch fails on missing key.
82 with self.assertRaises(KeyError):
83 lru_dict.touch(22)
84
85 def test_magic_methods(self):
86 # Check __nonzero__, __len__ and __contains__ for empty dict.
87 lru_dict = lru.LRUDict()
88 self.assertFalse(lru_dict)
89 self.assertEqual(len(lru_dict), 0)
90 self.assertFalse(1 in lru_dict)
91
92 # Dict with one item.
93 lru_dict.add(1, 'one')
94 self.assertTrue(lru_dict)
95 self.assertEqual(len(lru_dict), 1)
96 self.assertTrue(1 in lru_dict)
97 self.assertFalse(2 in lru_dict)
98
99 def test_order(self):
100 data = [1, 2, 3]
101
102 # Edge cases.
103 self.assert_order(self.prepare_lru_dict([]), [])
104 self.assert_order(self.prepare_lru_dict([1]), [1])
105
106 # No touches.
107 self.assert_order(self.prepare_lru_dict(data), data)
108
109 # Touching newest item is noop.
110 lru_dict = self.prepare_lru_dict(data)
111 lru_dict.touch(3)
112 self.assert_order(lru_dict, data)
113
114 # Touch to move to newest.
115 lru_dict = self.prepare_lru_dict(data)
116 lru_dict.touch(2)
117 self.assert_order(lru_dict, [1, 3, 2])
118
119 # Pop newest.
120 lru_dict = self.prepare_lru_dict(data)
121 lru_dict.pop(1)
122 self.assert_order(lru_dict, [2, 3])
123
124 # Pop in the middle.
125 lru_dict = self.prepare_lru_dict(data)
126 lru_dict.pop(2)
127 self.assert_order(lru_dict, [1, 3])
128
129 # Pop oldest.
130 lru_dict = self.prepare_lru_dict(data)
131 lru_dict.pop(3)
132 self.assert_order(lru_dict, [1, 2])
133
134 # Add oldest.
135 lru_dict = self.prepare_lru_dict(data)
136 lru_dict.batch_insert_oldest([(4, 4), (5, 5)])
137 self.assert_order(lru_dict, [4, 5] + data)
138
139 # Add newest.
140 lru_dict = self.prepare_lru_dict(data)
141 lru_dict.add(4, 4)
142 self.assert_order(lru_dict, data + [4])
143
144 def test_load_save(self):
145 def save_and_load(lru_dict):
146 tmp_name = tempfile.mktemp()
147 try:
148 lru_dict.save(tmp_name)
149 return lru.LRUDict.load(tmp_name)
150 finally:
151 try:
152 os.unlink(tmp_name)
153 except OSError:
154 pass
155
156 data = [1, 2, 3]
157
158 # Edge case.
159 empty = save_and_load(lru.LRUDict())
160 self.assertFalse(empty)
161
162 # Normal flow.
163 lru_dict = save_and_load(self.prepare_lru_dict(data))
164 self.assert_order(lru_dict, data)
165
166 # After touches.
167 lru_dict = self.prepare_lru_dict(data)
168 lru_dict.touch(2)
169 lru_dict = save_and_load(lru_dict)
170 self.assert_order(lru_dict, [1, 3, 2])
171
172 # After pop.
173 lru_dict = self.prepare_lru_dict(data)
174 lru_dict.pop(2)
175 lru_dict = save_and_load(lru_dict)
176 self.assert_order(lru_dict, [1, 3])
177
178 # After add.
179 lru_dict = self.prepare_lru_dict(data)
180 lru_dict.add(4, 4)
181 lru_dict.batch_insert_oldest([(5, 5), (6, 6)])
182 lru_dict = save_and_load(lru_dict)
183 self.assert_order(lru_dict, [5, 6] + data + [4])
184
185 def test_corrupted_state_file(self):
186 def load_from_state(state_text):
187 tmp_name = tempfile.mktemp()
188 try:
189 with open(tmp_name, 'wt') as f:
190 f.write(state_text)
191 return lru.LRUDict.load(tmp_name)
192 finally:
193 os.unlink(tmp_name)
194
195 # Loads correct state just fine.
196 self.assertIsNotNone(load_from_state(json.dumps([
197 ['key1', 'value1'],
198 ['key2', 'value2'],
199 ])))
200
201 # Not a json.
202 with self.assertRaises(ValueError):
203 load_from_state('garbage, not a state')
204
205 # Not a list.
206 with self.assertRaises(ValueError):
207 load_from_state('{}')
208
209 # Not a list of pairs.
210 with self.assertRaises(ValueError):
211 load_from_state(json.dumps([
212 ['key', 'value', 'and whats this?'],
213 ]))
214
215 # Duplicate keys.
216 with self.assertRaises(ValueError):
217 load_from_state(json.dumps([
218 ['key', 'value'],
219 ['key', 'another_value'],
220 ]))
221
222
223 if __name__ == '__main__':
224 VERBOSE = '-v' in sys.argv
225 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR)
226 unittest.main()
OLDNEW
« no previous file with comments | « swarm_client/tests/isolateserver_test.py ('k') | swarm_client/tests/net_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698