# Ubuntu CI Engine
# Copyright 2015 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import unittest

from ucitests import assertions

from ucivms import timeouts


class TestExponentialBackoff(unittest.TestCase):

    def test_no_timeouts(self):
        timo = timeouts.ExponentialBackoff(1, 2, 0)
        assertions.assertLength(self, 0, list(iter(timo)))

    def test_up_to_5mins(self):
        timo = timeouts.ExponentialBackoff(0, 300, 10)
        values = list(iter(timo))
        # There is as many timeouts than retries
        assertions.assertLength(self, 10, values)
        # The first duration is explicit
        self.assertEqual(0, values[0])
        # The sum of wait times is equal to up_to
        self.assertEqual(300, sum(values))
