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 threading | 6 import threading |
7 import time | 7 import time |
8 import unittest | 8 import unittest |
9 | 9 |
10 import mock | 10 import mock |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 def test_flush_raises(self): | 88 def test_flush_raises(self): |
89 self.assertIsNone(interface.state.global_monitor) | 89 self.assertIsNone(interface.state.global_monitor) |
90 with self.assertRaises(errors.MonitoringNoConfiguredMonitorError): | 90 with self.assertRaises(errors.MonitoringNoConfiguredMonitorError): |
91 interface.flush() | 91 interface.flush() |
92 | 92 |
93 def test_flush_many(self): | 93 def test_flush_many(self): |
94 interface.state.global_monitor = stubs.MockMonitor() | 94 interface.state.global_monitor = stubs.MockMonitor() |
95 | 95 |
96 # pylint: disable=unused-argument | 96 # pylint: disable=unused-argument |
97 def serialize_to(pb, default_target=None, loop_action=None): | 97 def serialize_to(pb, default_target=None, loop_action=None): |
98 for _ in xrange(5001): | 98 for _ in xrange(1001): |
99 loop_action(pb) | 99 loop_action(pb) |
100 pb.data.add().name = 'foo' | 100 pb.data.add().name = 'foo' |
101 | 101 |
102 # We can't use the mock's call_args_list here because the same object is | 102 # We can't use the mock's call_args_list here because the same object is |
103 # reused as the argument to both calls and cleared inbetween. | 103 # reused as the argument to both calls and cleared inbetween. |
104 data_lengths = [] | 104 data_lengths = [] |
105 def send(proto): | 105 def send(proto): |
106 data_lengths.append(len(proto.data)) | 106 data_lengths.append(len(proto.data)) |
107 interface.state.global_monitor.send.side_effect = send | 107 interface.state.global_monitor.send.side_effect = send |
108 | 108 |
109 fake_metric = mock.Mock() | 109 fake_metric = mock.Mock() |
110 fake_metric.serialize_to = mock.Mock(side_effect=serialize_to) | 110 fake_metric.serialize_to = mock.Mock(side_effect=serialize_to) |
111 interface.state.metrics.add(fake_metric) | 111 interface.state.metrics.add(fake_metric) |
112 | 112 |
113 interface.flush() | 113 interface.flush() |
114 self.assertEquals(2, interface.state.global_monitor.send.call_count) | 114 self.assertEquals(2, interface.state.global_monitor.send.call_count) |
115 self.assertEqual(5000, data_lengths[0]) | 115 self.assertEqual(1000, data_lengths[0]) |
116 self.assertEqual(1, data_lengths[1]) | 116 self.assertEqual(1, data_lengths[1]) |
117 | 117 |
118 def test_register_unregister(self): | 118 def test_register_unregister(self): |
119 fake_metric = mock.Mock() | 119 fake_metric = mock.Mock() |
120 self.assertEqual(0, len(interface.state.metrics)) | 120 self.assertEqual(0, len(interface.state.metrics)) |
121 interface.register(fake_metric) | 121 interface.register(fake_metric) |
122 self.assertEqual(1, len(interface.state.metrics)) | 122 self.assertEqual(1, len(interface.state.metrics)) |
123 interface.unregister(fake_metric) | 123 interface.unregister(fake_metric) |
124 self.assertEqual(0, len(interface.state.metrics)) | 124 self.assertEqual(0, len(interface.state.metrics)) |
125 | 125 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 282 |
283 def test_sleeps_for_minimum_zero_secs(self): | 283 def test_sleeps_for_minimum_zero_secs(self): |
284 self.t.start() | 284 self.t.start() |
285 | 285 |
286 # Flush takes 65 seconds. | 286 # Flush takes 65 seconds. |
287 interface.flush.side_effect = functools.partial(self.increment_time, 65) | 287 interface.flush.side_effect = functools.partial(self.increment_time, 65) |
288 | 288 |
289 self.assertInRange(30, 60, self.stop_event.timeout_wait()) | 289 self.assertInRange(30, 60, self.stop_event.timeout_wait()) |
290 self.assertAlmostEqual(0, self.stop_event.timeout_wait()) | 290 self.assertAlmostEqual(0, self.stop_event.timeout_wait()) |
291 self.assertAlmostEqual(0, self.stop_event.timeout_wait()) | 291 self.assertAlmostEqual(0, self.stop_event.timeout_wait()) |
OLD | NEW |