Lock test tweaks

- Use the lock provided reader/writer constants
  to differentiate on.
- Add a fake amount of work time to simulate some
  activity happening when acquiring the lock.

Change-Id: I8c6c2db35d4c3324a6476e45399b6fa6f91a61e1
This commit is contained in:
Joshua Harlow 2014-03-10 11:08:07 -07:00 committed by Joshua Harlow
parent a6930eb537
commit 60630e1fcb

View File

@ -28,6 +28,9 @@ from taskflow.utils import lock_utils
# real overlaps then they will still exist. # real overlaps then they will still exist.
NAPPY_TIME = 0.05 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): def _find_overlaps(times, start, end):
overlaps = 0 overlaps = 0
@ -41,29 +44,41 @@ def _spawn_variation(readers, writers, max_workers=None):
start_stops = collections.deque() start_stops = collections.deque()
lock = lock_utils.ReaderWriterLock() lock = lock_utils.ReaderWriterLock()
def read_func(): def read_func(ident):
with lock.read_lock(): 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) time.sleep(NAPPY_TIME)
def write_func(): def write_func(ident):
with lock.write_lock(): 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) time.sleep(NAPPY_TIME)
if max_workers is None: if max_workers is None:
max_workers = max(0, readers) + max(0, writers) max_workers = max(0, readers) + max(0, writers)
if max_workers > 0: if max_workers > 0:
with futures.ThreadPoolExecutor(max_workers=max_workers) as e: with futures.ThreadPoolExecutor(max_workers=max_workers) as e:
for i in range(0, readers): count = 0
e.submit(read_func) for _i in range(0, readers):
for i in range(0, writers): e.submit(read_func, count)
e.submit(write_func) count += 1
for _i in range(0, writers):
e.submit(write_func, count)
count += 1
writer_times = [] writer_times = []
reader_times = [] reader_times = []
for (t, start, stop) in list(start_stops): for (lock_type, start, stop) in list(start_stops):
if t == 'w': if lock_type == lock.WRITER:
writer_times.append((start, stop)) writer_times.append((start, stop))
else: else:
reader_times.append((start, stop)) reader_times.append((start, stop))