github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/utils.py (about) 1 # Copyright 2016 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 import time 16 17 18 class TimeOut(object): 19 def __init__(self, wait): 20 self.wait_time = wait 21 self.expire_time = time.time() + wait 22 23 def is_timed_out(self): 24 return time.time() > self.expire_time 25 26 def __call__(self, *args, **kwargs): 27 return time.time() > self.expire_time 28 29 30 def wait_until(lbd, time_out=None): 31 end_time = None 32 if time_out is not None: 33 end_time = time.time() + time_out 34 35 while (end_time is None 36 or time.time() < end_time) and \ 37 not lbd(): 38 time.sleep(0.1)