| OLD | NEW |
| 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 json | 5 import json |
| 6 import os | 6 import os |
| 7 import tempfile | 7 import tempfile |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 import mock | 10 import mock |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 self.assertEqual(set(result1), set(pollers.FilePoller.field_keys)) | 182 self.assertEqual(set(result1), set(pollers.FilePoller.field_keys)) |
| 183 self.assertEqual(set(result2), set(pollers.FilePoller.field_keys)) | 183 self.assertEqual(set(result2), set(pollers.FilePoller.field_keys)) |
| 184 | 184 |
| 185 data1 = result1.copy() | 185 data1 = result1.copy() |
| 186 data2 = result2.copy() | 186 data2 = result2.copy() |
| 187 data1['random'] = 'value' # Extra field, should be ignored. | 187 data1['random'] = 'value' # Extra field, should be ignored. |
| 188 del data2['project_id'] # Missing field, should become 'unknown'. | 188 del data2['project_id'] # Missing field, should become 'unknown'. |
| 189 data2['duration_s'] = 5 | 189 data2['duration_s'] = 5 |
| 190 data2['pending_s'] = 1 | 190 data2['pending_s'] = 1 |
| 191 data2['total_s'] = data2['pending_s'] + data2['duration_s'] | 191 data2['total_s'] = data2['pending_s'] + data2['duration_s'] |
| 192 data2['steps'] = [ |
| 193 {'step_name': 'compile', 'duration_s': 2, 'result': 'r1'}, |
| 194 ] |
| 192 with temporary_directory(prefix='poller-test-') as tempdir: | 195 with temporary_directory(prefix='poller-test-') as tempdir: |
| 193 filename = self.create_data_file(tempdir, [data1, data2]) | 196 filename = self.create_data_file(tempdir, [data1, data2]) |
| 194 p = pollers.FilePoller(filename, {}) | 197 p = pollers.FilePoller(filename, {}) |
| 195 self.assertTrue(p.poll()) | 198 self.assertTrue(p.poll()) |
| 196 fake_increment.assert_any_call(result1) | 199 fake_increment.assert_any_call(result1) |
| 197 fake_increment.assert_any_call(result2) | 200 fake_increment.assert_any_call(result2) |
| 198 fake_add.assert_any_call(data2['duration_s'], result2) | 201 fake_add.assert_any_call(data2['duration_s'], result2) |
| 199 fake_add.assert_any_call(data2['pending_s'], result2) | 202 fake_add.assert_any_call(data2['pending_s'], result2) |
| 200 fake_add.assert_any_call(data2['total_s'], result2) | 203 fake_add.assert_any_call(data2['total_s'], result2) |
| 201 self.assertFalse(os.path.isfile(filename)) | 204 self.assertFalse(os.path.isfile(filename)) |
| 202 # Make sure the rotated file is still there - for debugging. | 205 # Make sure the rotated file is still there - for debugging. |
| 203 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) | 206 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) |
| 204 | 207 |
| 205 def test_file_has_bad_data(self): | 208 def test_file_has_bad_data(self): |
| 206 """Mostly a smoke test: don't crash on bad data.""" | 209 """Mostly a smoke test: don't crash on bad data.""" |
| 207 with temporary_directory(prefix='poller-test-') as tempdir: | 210 with temporary_directory(prefix='poller-test-') as tempdir: |
| 208 filename = self.create_data_file(tempdir, []) | 211 filename = self.create_data_file(tempdir, []) |
| 209 with open(filename, 'a') as f: | 212 with open(filename, 'a') as f: |
| 210 f.write('}') | 213 f.write('}') |
| 211 p = pollers.FilePoller(filename, {}) | 214 p = pollers.FilePoller(filename, {}) |
| 212 self.assertTrue(p.poll()) | 215 self.assertTrue(p.poll()) |
| 213 self.assertFalse(os.path.isfile(filename)) | 216 self.assertFalse(os.path.isfile(filename)) |
| 214 # Make sure the rotated file is still there - for debugging. | 217 # Make sure the rotated file is still there - for debugging. |
| 215 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) | 218 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) |
| 216 | 219 |
| 217 def test_safe_remove_error(self): | 220 def test_safe_remove_error(self): |
| 218 """Smoke test: the function should not raise an exception.""" | 221 """Smoke test: the function should not raise an exception.""" |
| 219 pollers.safe_remove('nonexistent-file') | 222 pollers.safe_remove('nonexistent-file') |
| OLD | NEW |