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