OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. |
| 5 |
| 6 import json |
| 7 import logging |
| 8 import os |
| 9 import sys |
| 10 import tempfile |
| 11 import time |
| 12 import unittest |
| 13 |
| 14 import test_env_bot_code |
| 15 test_env_bot_code.setup_test_env() |
| 16 |
| 17 from depot_tools import auto_stub |
| 18 from depot_tools import fix_encoding |
| 19 from utils import file_path |
| 20 |
| 21 import file_refresher |
| 22 |
| 23 |
| 24 class TestFileRefresherThread(auto_stub.TestCase): |
| 25 def setUp(self): |
| 26 super(TestFileRefresherThread, self).setUp() |
| 27 self.root_dir = tempfile.mkdtemp(prefix='file_refresher') |
| 28 self.path = os.path.join(self.root_dir, 'target_file') |
| 29 |
| 30 def tearDown(self): |
| 31 file_path.rmtree(self.root_dir) |
| 32 super(TestFileRefresherThread, self).tearDown() |
| 33 |
| 34 def test_works(self): |
| 35 counter = [0] |
| 36 def callback(): |
| 37 counter[0] += 1 |
| 38 return counter[0] |
| 39 r = file_refresher.FileRefresherThread(self.path, callback, 0.1) |
| 40 r.start() |
| 41 time.sleep(1) |
| 42 r.stop() |
| 43 self.assertTrue(0 < counter[0] < 15) # was called reasonable number of times |
| 44 with open(self.path, 'rb') as f: |
| 45 body = json.load(f) |
| 46 self.assertEqual(counter[0], body) # actually updated the file |
| 47 |
| 48 |
| 49 if __name__ == '__main__': |
| 50 fix_encoding.fix_encoding() |
| 51 logging.basicConfig( |
| 52 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) |
| 53 unittest.main() |
OLD | NEW |