diff --git a/taskflow/tests/unit/test_utils_lock_utils.py b/taskflow/tests/unit/test_utils_lock_utils.py index 475ee059b..30a120a37 100644 --- a/taskflow/tests/unit/test_utils_lock_utils.py +++ b/taskflow/tests/unit/test_utils_lock_utils.py @@ -28,6 +28,9 @@ from taskflow.utils import lock_utils # real overlaps then they will still exist. NAPPY_TIME = 0.05 +# We will spend this amount of time doing some "fake" work. +WORK_TIMES = [(0.01 + x/100.0) for x in range(0, 5)] + def _find_overlaps(times, start, end): overlaps = 0 @@ -41,29 +44,41 @@ def _spawn_variation(readers, writers, max_workers=None): start_stops = collections.deque() lock = lock_utils.ReaderWriterLock() - def read_func(): + def read_func(ident): with lock.read_lock(): - start_stops.append(('r', time.time(), time.time())) + # TODO(harlowja): sometime in the future use a monotonic clock here + # to avoid problems that can be caused by ntpd resyncing the clock + # while we are actively running. + enter_time = time.time() + time.sleep(WORK_TIMES[ident % len(WORK_TIMES)]) + exit_time = time.time() + start_stops.append((lock.READER, enter_time, exit_time)) time.sleep(NAPPY_TIME) - def write_func(): + def write_func(ident): with lock.write_lock(): - start_stops.append(('w', time.time(), time.time())) + enter_time = time.time() + time.sleep(WORK_TIMES[ident % len(WORK_TIMES)]) + exit_time = time.time() + start_stops.append((lock.WRITER, enter_time, exit_time)) time.sleep(NAPPY_TIME) if max_workers is None: max_workers = max(0, readers) + max(0, writers) if max_workers > 0: with futures.ThreadPoolExecutor(max_workers=max_workers) as e: - for i in range(0, readers): - e.submit(read_func) - for i in range(0, writers): - e.submit(write_func) + count = 0 + for _i in range(0, readers): + e.submit(read_func, count) + count += 1 + for _i in range(0, writers): + e.submit(write_func, count) + count += 1 writer_times = [] reader_times = [] - for (t, start, stop) in list(start_stops): - if t == 'w': + for (lock_type, start, stop) in list(start_stops): + if lock_type == lock.WRITER: writer_times.append((start, stop)) else: reader_times.append((start, stop))