blob: 50a0bdff94cd94ad048e6f0cdfeebf0771f0292e [file] [log] [blame]
Daniel Erat3e3f7f42010-03-29 17:19:14 -07001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import time
6from autotest_lib.client.common_lib import error
7
8
9def poll_for_condition(
10 condition, exception=None, timeout=10, sleep_interval=0.1):
11 """Poll until a condition becomes true.
12
13 condition: function taking no args and returning bool
14 exception: exception to throw if condition doesn't become true
15 timeout: maximum number of seconds to wait
16 sleep_interval: time to sleep between polls
17
18 Raises:
19 'exception' arg if supplied; error.TestError otherwise
20 """
21 start_time = time.time()
22 while True:
23 if condition():
24 return
25 if time.time() + sleep_interval - start_time > timeout:
26 raise exception if exception else error.TestError(
27 'Timed out waiting for condition')
28 time.sleep(sleep_interval)