OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import functools | 5 import functools |
6 import operator | 6 import operator |
7 import time | 7 import time |
8 import unittest | 8 import unittest |
9 | 9 |
10 import mock | 10 import mock |
11 | 11 |
12 from infra_libs.ts_mon.common import errors | 12 from infra_libs.ts_mon.common import errors |
13 from infra_libs.ts_mon.common import metric_store | 13 from infra_libs.ts_mon.common import metric_store |
14 from infra_libs.ts_mon.common import metrics | 14 from infra_libs.ts_mon.common import metrics |
15 from infra_libs.ts_mon.common.test import stubs | 15 from infra_libs.ts_mon.common.test import stubs |
16 | 16 |
17 | 17 |
| 18 class DefaultModifyFnTest(unittest.TestCase): |
| 19 def test_adds(self): |
| 20 fn = metric_store.default_modify_fn('foo') |
| 21 self.assertEquals(5, fn(2, 3)) |
| 22 self.assertEquals(5, fn(3, 2)) |
| 23 |
| 24 def test_negative(self): |
| 25 fn = metric_store.default_modify_fn('foo') |
| 26 with self.assertRaises(errors.MonitoringDecreasingValueError) as cm: |
| 27 fn(2, -1) |
| 28 self.assertIn('"foo"', str(cm.exception)) |
| 29 |
| 30 |
18 class MetricStoreTestBase(object): | 31 class MetricStoreTestBase(object): |
19 """Abstract base class for testing MetricStore implementations. | 32 """Abstract base class for testing MetricStore implementations. |
20 | 33 |
21 This class doesn't inherit from unittest.TestCase to prevent it from being | 34 This class doesn't inherit from unittest.TestCase to prevent it from being |
22 run automatically by expect_tests. | 35 run automatically by expect_tests. |
23 | 36 |
24 Your subclass should inherit from this and unittest.TestCase, and set | 37 Your subclass should inherit from this and unittest.TestCase, and set |
25 METRIC_STORE_CLASS to the implementation you want to test. See | 38 METRIC_STORE_CLASS to the implementation you want to test. See |
26 InProcessMetricStoreTest in this file for an example. | 39 InProcessMetricStoreTest in this file for an example. |
27 """ | 40 """ |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 self.store.set('foo', (('field', 'value'),), 42) | 139 self.store.set('foo', (('field', 'value'),), 42) |
127 self.store.reset_for_unittest(name='bar') | 140 self.store.reset_for_unittest(name='bar') |
128 self.assertEquals(42, self.store.get('foo', (('field', 'value'),))) | 141 self.assertEquals(42, self.store.get('foo', (('field', 'value'),))) |
129 | 142 |
130 self.store.reset_for_unittest(name='foo') | 143 self.store.reset_for_unittest(name='foo') |
131 self.assertIsNone(self.store.get('foo', (('field', 'value'),))) | 144 self.assertIsNone(self.store.get('foo', (('field', 'value'),))) |
132 | 145 |
133 | 146 |
134 class InProcessMetricStoreTest(MetricStoreTestBase, unittest.TestCase): | 147 class InProcessMetricStoreTest(MetricStoreTestBase, unittest.TestCase): |
135 METRIC_STORE_CLASS = metric_store.InProcessMetricStore | 148 METRIC_STORE_CLASS = metric_store.InProcessMetricStore |
136 | |
137 | |
138 class CombineModificationsTest(unittest.TestCase): | |
139 def test_set_set(self): | |
140 self.assertEqual( | |
141 metric_store.Modification('two', (), 'set', (2, False)), | |
142 metric_store.combine_modifications( | |
143 metric_store.Modification('one', (), 'set', (1, False)), | |
144 metric_store.Modification('two', (), 'set', (2, False)))) | |
145 | |
146 def test_set_incr(self): | |
147 self.assertEqual( | |
148 metric_store.Modification('one', (), 'set', (3, False)), | |
149 metric_store.combine_modifications( | |
150 metric_store.Modification('one', (), 'set', (1, False)), | |
151 metric_store.Modification('two', (), 'incr', (2, operator.add)))) | |
152 | |
153 def test_incr_set(self): | |
154 self.assertEqual( | |
155 metric_store.Modification('two', (), 'set', (2, False)), | |
156 metric_store.combine_modifications( | |
157 metric_store.Modification('one', (), 'incr', (1, operator.add)), | |
158 metric_store.Modification('two', (), 'set', (2, False)))) | |
159 | |
160 def test_incr_incr(self): | |
161 self.assertEqual( | |
162 metric_store.Modification('one', (), 'incr', (3, operator.add)), | |
163 metric_store.combine_modifications( | |
164 metric_store.Modification('one', (), 'incr', (1, operator.add)), | |
165 metric_store.Modification('two', (), 'incr', (2, operator.add)))) | |
166 | |
167 def test_none_set(self): | |
168 self.assertEqual( | |
169 metric_store.Modification('two', (), 'set', (2, False)), | |
170 metric_store.combine_modifications( | |
171 None, | |
172 metric_store.Modification('two', (), 'set', (2, False)))) | |
173 | |
174 def test_none_incr(self): | |
175 self.assertEqual( | |
176 metric_store.Modification('two', (), 'incr', (2, operator.add)), | |
177 metric_store.combine_modifications( | |
178 None, | |
179 metric_store.Modification('two', (), 'incr', (2, operator.add)))) | |
180 | |
181 def test_bad_type(self): | |
182 with self.assertRaises(errors.UnknownModificationTypeError): | |
183 metric_store.combine_modifications( | |
184 metric_store.Modification('one', (), 'set', (1, False)), | |
185 metric_store.Modification('two', (), 'bad', (2, False))) | |
OLD | NEW |