util_unittest.py revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1# Copyright (c) 2012 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. 4import os 5import unittest 6import shutil 7import tempfile 8 9from telemetry.core import util 10 11class TestWait(unittest.TestCase): 12 @staticmethod 13 def testNonTimeout(): 14 def test(): 15 return True 16 util.WaitFor(test, 0.1) 17 18 def testTimeout(self): 19 def test(): 20 return False 21 self.assertRaises(util.TimeoutException, lambda: util.WaitFor(test, 0.1)) 22 23 def testCallable(self): 24 """Test methods and anonymous functions, functions are tested elsewhere.""" 25 class Test(object): 26 def Method(self): 27 return 'test' 28 util.WaitFor(Test().Method, 0.1) 29 30 util.WaitFor(lambda: 1, 0.1) 31 32 # Test noncallable condition. 33 self.assertRaises(TypeError, lambda: util.WaitFor('test', 0.1)) 34 35 def testReturn(self): 36 self.assertEquals('test', util.WaitFor(lambda: 'test', 0.1)) 37 38class TestGetSequentialFileName(unittest.TestCase): 39 def __init__(self, *args, **kwargs): 40 super(TestGetSequentialFileName, self).__init__(*args, **kwargs) 41 self.test_directory = None 42 43 def setUp(self): 44 self.test_directory = tempfile.mkdtemp() 45 46 def testGetSequentialFileNameNoOtherSequentialFile(self): 47 next_json_test_file_path = util.GetSequentialFileName( 48 os.path.join(self.test_directory, 'test')) 49 self.assertEquals(os.path.join(self.test_directory, 'test_000'), 50 next_json_test_file_path) 51 52 def testGetSequentialFileNameWithOtherSequentialFiles(self): 53 # Create test_000.json, test_001.json, test_002.json in test directory. 54 for i in xrange(3): 55 with open( 56 os.path.join(self.test_directory, 'test_%03d.json' % i), 'w') as _: 57 pass 58 next_json_test_file_path = util.GetSequentialFileName( 59 os.path.join(self.test_directory, 'test')) 60 self.assertEquals(os.path.join(self.test_directory, 'test_003'), 61 next_json_test_file_path) 62 63 def tearDown(self): 64 shutil.rmtree(self.test_directory) 65