xref: /llvm-project/cross-project-tests/debuginfo-tests/dexter/dex/utils/Timeout.py (revision f98ee40f4b5d7474fc67e82824bf6abbaedb7b1c)
1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""Utility class to check for timeouts. Timer starts when the object is initialized,
8and can be checked by calling timed_out(). Passing a timeout value of 0.0 or less
9means a timeout will never be triggered, i.e. timed_out() will always return False.
10"""
11
12import time
13
14
15class Timeout(object):
16    def __init__(self, duration: float):
17        self.start = self.now
18        self.duration = duration
19
20    def timed_out(self):
21        if self.duration <= 0.0:
22            return False
23        return self.elapsed > self.duration
24
25    @property
26    def elapsed(self):
27        return self.now - self.start
28
29    @property
30    def now(self):
31        return time.time()
32