146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)# Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import os
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import shutil
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)import tempfile
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)import unittest
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.core import util
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestWait(unittest.TestCase):
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  @staticmethod
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def testNonTimeout():
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    def test():
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return True
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    util.WaitFor(test, 0.1)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def testTimeout(self):
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    def test():
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return False
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.assertRaises(util.TimeoutException, lambda: util.WaitFor(test, 0.1))
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  def testCallable(self):
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    """Test methods and anonymous functions, functions are tested elsewhere."""
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    class Test(object):
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      def Method(self):
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        return 'test'
294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    util.WaitFor(Test().Method, 0.1)
304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    util.WaitFor(lambda: 1, 0.1)
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    # Test noncallable condition.
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertRaises(TypeError, lambda: util.WaitFor('test', 0.1))
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  def testReturn(self):
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self.assertEquals('test', util.WaitFor(lambda: 'test', 0.1))
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class TestGetSequentialFileName(unittest.TestCase):
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def __init__(self, *args, **kwargs):
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    super(TestGetSequentialFileName, self).__init__(*args, **kwargs)
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.test_directory = None
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def setUp(self):
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.test_directory = tempfile.mkdtemp()
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def testGetSequentialFileNameNoOtherSequentialFile(self):
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    next_json_test_file_path = util.GetSequentialFileName(
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        os.path.join(self.test_directory, 'test'))
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.assertEquals(os.path.join(self.test_directory, 'test_000'),
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      next_json_test_file_path)
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def testGetSequentialFileNameWithOtherSequentialFiles(self):
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    # Create test_000.json, test_001.json, test_002.json in test directory.
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for i in xrange(3):
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      with open(
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          os.path.join(self.test_directory, 'test_%03d.json' % i), 'w') as _:
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        pass
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    next_json_test_file_path = util.GetSequentialFileName(
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        os.path.join(self.test_directory, 'test'))
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.assertEquals(os.path.join(self.test_directory, 'test_003'),
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                      next_json_test_file_path)
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  def tearDown(self):
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    shutil.rmtree(self.test_directory)
66