1#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import unittest
19import time
20
21import vts.utils.python.common.timeout_utils as timeout_utils
22
23#TODO use milliseconds after float number timeout is implemented in timeout_utils
24
25
26class TimeoutUtilsTest(unittest.TestCase):
27    '''Test methods for timeout_util module.'''
28
29    def setUp(self):
30        """SetUp tasks"""
31        self.count = 0
32
33    def test_timeout_no_except_no_timeout(self):
34        """Tests the functionality of timeout decorator function."""
35        @timeout_utils.timeout(1, no_exception=True)
36        def increase(self):
37            self.count += 1;
38
39        increase(self)
40        self.assertEqual(self.count, 1)
41
42    def test_timeout_no_except_with_timeout(self):
43        """Tests the functionality of timeout decorator function."""
44        @timeout_utils.timeout(1, no_exception=True)
45        def increase(self):
46            time.sleep(2)
47            self.count += 1;
48
49        increase(self)
50        self.assertEqual(self.count, 0)
51
52    def test_timeout_with_except_with_timeout(self):
53        """Tests the functionality of timeout decorator function."""
54        @timeout_utils.timeout(1)
55        def increase(self):
56            time.sleep(2)
57            self.count += 1;
58
59        try:
60            increase(self)
61        except timeout_utils.TimeoutError:
62            pass
63
64        self.assertEqual(self.count, 0)
65
66    def test_timeout_with_except_no_timeout(self):
67        """Tests the functionality of timeout decorator function."""
68        @timeout_utils.timeout(1)
69        def increase(self):
70            self.count += 1;
71
72        increase(self)
73
74        self.assertEqual(self.count, 1)