OLD | NEW |
| (Empty) |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 import unittest | |
6 | |
7 import mock | |
8 | |
9 from infra_libs.ts_mon.common import metrics | |
10 from infra_libs.ts_mon.common import helpers | |
11 | |
12 | |
13 class ScopedIncrementCounterTest(unittest.TestCase): | |
14 | |
15 def test_success(self): | |
16 counter = mock.Mock(metrics.CounterMetric) | |
17 with helpers.ScopedIncrementCounter(counter): | |
18 pass | |
19 counter.increment.assert_called_once_with({'status': 'success'}) | |
20 | |
21 def test_exception(self): | |
22 counter = mock.Mock(metrics.CounterMetric) | |
23 with self.assertRaises(Exception): | |
24 with helpers.ScopedIncrementCounter(counter): | |
25 raise Exception() | |
26 counter.increment.assert_called_once_with({'status': 'failure'}) | |
27 | |
28 def test_custom_status(self): | |
29 counter = mock.Mock(metrics.CounterMetric) | |
30 with helpers.ScopedIncrementCounter(counter) as sc: | |
31 sc.set_status('foo') | |
32 counter.increment.assert_called_once_with({'status': 'foo'}) | |
33 | |
34 def test_set_failure(self): | |
35 counter = mock.Mock(metrics.CounterMetric) | |
36 with helpers.ScopedIncrementCounter(counter) as sc: | |
37 sc.set_failure() | |
38 counter.increment.assert_called_once_with({'status': 'failure'}) | |
39 | |
40 def test_custom_status_and_exception(self): | |
41 counter = mock.Mock(metrics.CounterMetric) | |
42 with self.assertRaises(Exception): | |
43 with helpers.ScopedIncrementCounter(counter) as sc: | |
44 sc.set_status('foo') | |
45 raise Exception() | |
46 counter.increment.assert_called_once_with({'status': 'foo'}) | |
47 | |
48 def test_multiple_custom_status_calls(self): | |
49 counter = mock.Mock(metrics.CounterMetric) | |
50 with helpers.ScopedIncrementCounter(counter) as sc: | |
51 sc.set_status('foo') | |
52 sc.set_status('bar') | |
53 counter.increment.assert_called_once_with({'status': 'bar'}) | |
54 | |
55 def test_custom_label_success(self): | |
56 counter = mock.Mock(metrics.CounterMetric) | |
57 with helpers.ScopedIncrementCounter(counter, 'a', 'b', 'c'): | |
58 pass | |
59 counter.increment.assert_called_once_with({'a': 'b'}) | |
60 | |
61 def test_custom_label_exception(self): | |
62 counter = mock.Mock(metrics.CounterMetric) | |
63 with self.assertRaises(Exception): | |
64 with helpers.ScopedIncrementCounter(counter, 'a', 'b', 'c'): | |
65 raise Exception() | |
66 counter.increment.assert_called_once_with({'a': 'c'}) | |
OLD | NEW |