| 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, |
| 8 |
and can be checked by calling timed_out(). Passing a timeout value of 0.0 or less |
| 9 |
means a timeout will never be triggered, i.e. timed_out() will always return False. |
| 10 |
""" |
| 11 |
|
| 12 |
import time |
| 13 |
|
| 14 |
|
| 15 |
class 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() |