Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS 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. |
| 4 | |
| 5 | """Unit tests for server/cros/frontend_wrappers.py.""" |
| 6 | |
| 7 | import logging |
| 8 | import mox |
| 9 | import time |
| 10 | import unittest |
| 11 | |
Chris Masone | 6f10908 | 2012-07-18 14:21:38 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.cros import retry |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 13 | from autotest_lib.server.cros import frontend_wrappers |
| 14 | from autotest_lib.server import frontend |
| 15 | |
| 16 | class FrontendWrappersTest(mox.MoxTestBase): |
| 17 | """Unit tests for dynamic_suite.Reimager. |
| 18 | |
| 19 | @var _FLAKY_FLAG: for use in tests that need to simulate random failures. |
| 20 | """ |
| 21 | |
| 22 | _FLAKY_FLAG = None |
| 23 | |
| 24 | def setUp(self): |
| 25 | super(FrontendWrappersTest, self).setUp() |
| 26 | self._FLAKY_FLAG = False |
| 27 | |
| 28 | |
| 29 | def testRetryDecoratorSucceeds(self): |
| 30 | """Tests that a wrapped function succeeds without retrying.""" |
| 31 | timeout_min = .1 |
| 32 | timeout_sec = timeout_min * 60 |
Chris Masone | 6f10908 | 2012-07-18 14:21:38 -0700 | [diff] [blame] | 33 | @retry.retry(Exception, timeout_min=timeout_min, delay_sec=1) |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 34 | def succeed(): |
| 35 | return True |
| 36 | |
| 37 | deadline = time.time() + timeout_sec |
| 38 | self.assertTrue(succeed()) |
| 39 | self.assertTrue(time.time() < deadline) |
| 40 | |
| 41 | |
| 42 | def testRetryDecoratorFlakySucceeds(self): |
| 43 | """Tests that a wrapped function can retry and succeed.""" |
| 44 | timeout_min = .1 |
| 45 | timeout_sec = timeout_min * 60 |
Chris Masone | 6f10908 | 2012-07-18 14:21:38 -0700 | [diff] [blame] | 46 | @retry.retry(Exception, timeout_min=timeout_min, delay_sec=1) |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 47 | def flaky_succeed(): |
| 48 | if self._FLAKY_FLAG: |
| 49 | return True |
| 50 | self._FLAKY_FLAG = True |
| 51 | raise Exception |
| 52 | |
| 53 | deadline = time.time() + timeout_sec |
| 54 | self.assertTrue(flaky_succeed()) |
| 55 | self.assertTrue(time.time() < deadline) |
| 56 | |
| 57 | |
Chris Masone | c905a32 | 2012-04-10 10:14:27 -0700 | [diff] [blame] | 58 | def testRetryDecoratorFails(self): |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 59 | """Tests that a wrapped function retries til the timeout, then fails.""" |
| 60 | timeout_min = .01 |
| 61 | timeout_sec = timeout_min * 60 |
Chris Masone | 6f10908 | 2012-07-18 14:21:38 -0700 | [diff] [blame] | 62 | @retry.retry(Exception, timeout_min=timeout_min, delay_sec=1) |
Chris Masone | 8ac6671 | 2012-02-15 14:21:02 -0800 | [diff] [blame] | 63 | def fail(): |
| 64 | raise Exception() |
| 65 | |
| 66 | deadline = time.time() + timeout_sec |
| 67 | self.assertRaises(Exception, fail) |
| 68 | self.assertTrue(time.time() >= deadline) |