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 | |
| 12 | from autotest_lib.server.cros import frontend_wrappers |
| 13 | from autotest_lib.server import frontend |
| 14 | |
| 15 | class FrontendWrappersTest(mox.MoxTestBase): |
| 16 | """Unit tests for dynamic_suite.Reimager. |
| 17 | |
| 18 | @var _FLAKY_FLAG: for use in tests that need to simulate random failures. |
| 19 | """ |
| 20 | |
| 21 | _FLAKY_FLAG = None |
| 22 | |
| 23 | def setUp(self): |
| 24 | super(FrontendWrappersTest, self).setUp() |
| 25 | self._FLAKY_FLAG = False |
| 26 | |
| 27 | |
| 28 | def testRetryDecoratorSucceeds(self): |
| 29 | """Tests that a wrapped function succeeds without retrying.""" |
| 30 | timeout_min = .1 |
| 31 | timeout_sec = timeout_min * 60 |
| 32 | @frontend_wrappers.retry(Exception, |
| 33 | timeout_min=timeout_min, |
| 34 | delay_sec=1) |
| 35 | def succeed(): |
| 36 | return True |
| 37 | |
| 38 | deadline = time.time() + timeout_sec |
| 39 | self.assertTrue(succeed()) |
| 40 | self.assertTrue(time.time() < deadline) |
| 41 | |
| 42 | |
| 43 | def testRetryDecoratorFlakySucceeds(self): |
| 44 | """Tests that a wrapped function can retry and succeed.""" |
| 45 | timeout_min = .1 |
| 46 | timeout_sec = timeout_min * 60 |
| 47 | @frontend_wrappers.retry(Exception, |
| 48 | timeout_min=timeout_min, |
| 49 | delay_sec=1) |
| 50 | def flaky_succeed(): |
| 51 | if self._FLAKY_FLAG: |
| 52 | return True |
| 53 | self._FLAKY_FLAG = True |
| 54 | raise Exception |
| 55 | |
| 56 | deadline = time.time() + timeout_sec |
| 57 | self.assertTrue(flaky_succeed()) |
| 58 | self.assertTrue(time.time() < deadline) |
| 59 | |
| 60 | |
| 61 | def testRetryDecoratorFailss(self): |
| 62 | """Tests that a wrapped function retries til the timeout, then fails.""" |
| 63 | timeout_min = .01 |
| 64 | timeout_sec = timeout_min * 60 |
| 65 | @frontend_wrappers.retry(Exception, |
| 66 | timeout_min=timeout_min, |
| 67 | delay_sec=1) |
| 68 | def fail(): |
| 69 | raise Exception() |
| 70 | |
| 71 | deadline = time.time() + timeout_sec |
| 72 | self.assertRaises(Exception, fail) |
| 73 | self.assertTrue(time.time() >= deadline) |