1#!/usr/bin/python 2# 3# Copyright 2015 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unittests for server/cros/dynamic_suite/frontend_wrappers.py""" 8 9import mox 10import unittest 11 12import common 13 14from autotest_lib.server.cros.dynamic_suite import frontend_wrappers 15 16 17class FrontendWrappersTest(mox.MoxTestBase): 18 """Unit tests for frontend_wrappers global functions.""" 19 20 def testConvertTimeoutToRetryBasic(self): 21 """Test converting timeout and delay values to retry attempts.""" 22 backoff = 2 23 timeout_min = 10 24 delay_sec = 10 25 26 max_retry = frontend_wrappers.convert_timeout_to_retry(backoff, 27 timeout_min, 28 delay_sec) 29 30 self.assertEquals(max_retry, 6) 31 32 def testConvertTimeoutToRetryLimit(self): 33 """Test approaching a change in attempt amount.""" 34 backoff = 2 35 delay_sec = 10 36 timeout_min_lower_limit = 42.499999 37 timeout_min_at_limit = 42.5 38 timeout_min_upper_limit = 42.599999 39 40 max_retry_lower_limit = frontend_wrappers.convert_timeout_to_retry( 41 backoff, timeout_min_lower_limit, delay_sec) 42 43 max_retry_at_limit = frontend_wrappers.convert_timeout_to_retry( 44 backoff, timeout_min_at_limit, delay_sec) 45 46 max_retry_upper_limit = frontend_wrappers.convert_timeout_to_retry( 47 backoff, timeout_min_upper_limit, delay_sec) 48 49 # Eight attempts with a backoff factor of two should be sufficient 50 # for timeouts up to 2550 seconds (or 42.5 minutes). 51 self.assertEquals(max_retry_lower_limit, 8) 52 self.assertEquals(max_retry_at_limit, 8) 53 54 # We expect to see nine attempts, as we are above the 42.5 minute 55 # threshold. 56 self.assertEquals(max_retry_upper_limit, 9) 57 58 def testConvertTimeoutToRetrySmallTimeout(self): 59 """Test converting to retry attempts when a small timeout is used.""" 60 backoff = 2 61 timeout_min = 0.01 62 delay_sec = 10 63 64 max_retry = frontend_wrappers.convert_timeout_to_retry(backoff, 65 timeout_min, 66 delay_sec) 67 68 # The number of attempts should be less than one using the formula 69 # outlined in the function, but, we always round up to the nearest 70 # integer. 71 self.assertEquals(max_retry, 1) 72 73 def testConvertTimeoutToRetrySmallDelay(self): 74 """Test converting to retry attempts when the delay is small.""" 75 backoff = 2 76 timeout_min = 30 77 delay_sec = 0.01 78 79 max_retry = frontend_wrappers.convert_timeout_to_retry(backoff, 80 timeout_min, 81 delay_sec) 82 83 # The number of retries shouldn't be too large despite the small 84 # delay as a result of backing off in an exponential fashion. 85 self.assertEquals(max_retry, 18) 86 87 88if __name__ == '__main__': 89 unittest.main() 90