| 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 ] | |
| 195 with temporary_directory(prefix='poller-test-') as tempdir: | 192 with temporary_directory(prefix='poller-test-') as tempdir: |
| 196 filename = self.create_data_file(tempdir, [data1, data2]) | 193 filename = self.create_data_file(tempdir, [data1, data2]) |
| 197 p = pollers.FilePoller(filename, {}) | 194 p = pollers.FilePoller(filename, {}) |
| 198 self.assertTrue(p.poll()) | 195 self.assertTrue(p.poll()) |
| 199 fake_increment.assert_any_call(result1) | 196 fake_increment.assert_any_call(result1) |
| 200 fake_increment.assert_any_call(result2) | 197 fake_increment.assert_any_call(result2) |
| 201 fake_add.assert_any_call(data2['duration_s'], result2) | 198 fake_add.assert_any_call(data2['duration_s'], result2) |
| 202 fake_add.assert_any_call(data2['pending_s'], result2) | 199 fake_add.assert_any_call(data2['pending_s'], result2) |
| 203 fake_add.assert_any_call(data2['total_s'], result2) | 200 fake_add.assert_any_call(data2['total_s'], result2) |
| 204 self.assertFalse(os.path.isfile(filename)) | 201 self.assertFalse(os.path.isfile(filename)) |
| 205 # Make sure the rotated file is still there - for debugging. | 202 # Make sure the rotated file is still there - for debugging. |
| 206 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) | 203 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) |
| 207 | 204 |
| 208 def test_file_has_bad_data(self): | 205 def test_file_has_bad_data(self): |
| 209 """Mostly a smoke test: don't crash on bad data.""" | 206 """Mostly a smoke test: don't crash on bad data.""" |
| 210 with temporary_directory(prefix='poller-test-') as tempdir: | 207 with temporary_directory(prefix='poller-test-') as tempdir: |
| 211 filename = self.create_data_file(tempdir, []) | 208 filename = self.create_data_file(tempdir, []) |
| 212 with open(filename, 'a') as f: | 209 with open(filename, 'a') as f: |
| 213 f.write('}') | 210 f.write('}') |
| 214 p = pollers.FilePoller(filename, {}) | 211 p = pollers.FilePoller(filename, {}) |
| 215 self.assertTrue(p.poll()) | 212 self.assertTrue(p.poll()) |
| 216 self.assertFalse(os.path.isfile(filename)) | 213 self.assertFalse(os.path.isfile(filename)) |
| 217 # Make sure the rotated file is still there - for debugging. | 214 # Make sure the rotated file is still there - for debugging. |
| 218 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) | 215 self.assertTrue(os.path.isfile(pollers.rotated_filename(filename))) |
| 219 | 216 |
| 220 def test_safe_remove_error(self): | 217 def test_safe_remove_error(self): |
| 221 """Smoke test: the function should not raise an exception.""" | 218 """Smoke test: the function should not raise an exception.""" |
| 222 pollers.safe_remove('nonexistent-file') | 219 pollers.safe_remove('nonexistent-file') |
| OLD | NEW |