上下文
我最近发布了timer class for review on Code Review。我有一种直觉感觉有并发错误,因为我曾经看过1单元测试失败,但无法重现失败。因此我的帖子到代码审查。
我得到了一些很好的反馈,突出了代码中的各种竞争条件。 (我想)我理解了问题和解决方案,但在进行任何修复之前,我想通过单元测试来暴露这些错误。当我尝试时,我意识到这很困难。各种堆栈交换答案建议我必须控制线程的执行以暴露bug,并且任何人为的时间不一定可移植到不同的机器上。除了我试图解决的问题之外,这似乎是很多偶然的复杂性。
相反,我尝试使用the best static analysis (SA) tool for python,PyLint,看看它是否会挑出任何错误,但它不能。为什么人类可以通过代码审查(实质上是SA)找到错误,但SA工具不能?
害怕尝试get Valgrind working with python(听起来像牦牛皮),我决定在修复错误时不要先复制它们。现在我在泡菜中。
现在是代码。
from threading import Timer, Lock
from time import time
class NotRunningError(Exception): pass
class AlreadyRunningError(Exception): pass
class KitchenTimer(object):
'''
Loosely models a clockwork kitchen timer with the following differences:
You can start the timer with arbitrary duration (e.g. 1.2 seconds).
The timer calls back a given function when time's up.
Querying the time remaining has 0.1 second accuracy.
'''
PRECISION_NUM_DECIMAL_PLACES = 1
RUNNING = "RUNNING"
STOPPED = "STOPPED"
TIMEUP = "TIMEUP"
def __init__(self):
self._stateLock = Lock()
with self._stateLock:
self._state = self.STOPPED
self._timeRemaining = 0
def start(self, duration=1, whenTimeup=None):
'''
Starts the timer to count down from the given duration and call whenTimeup when time's up.
'''
with self._stateLock:
if self.isRunning():
raise AlreadyRunningError
else:
self._state = self.RUNNING
self.duration = duration
self._userWhenTimeup = whenTimeup
self._startTime = time()
self._timer = Timer(duration, self._whenTimeup)
self._timer.start()
def stop(self):
'''
Stops the timer, preventing whenTimeup callback.
'''
with self._stateLock:
if self.isRunning():
self._timer.cancel()
self._state = self.STOPPED
self._timeRemaining = self.duration - self._elapsedTime()
else:
raise NotRunningError()
def isRunning(self):
return self._state == self.RUNNING
def isStopped(self):
return self._state == self.STOPPED
def isTimeup(self):
return self._state == self.TIMEUP
@property
def timeRemaining(self):
if self.isRunning():
self._timeRemaining = self.duration - self._elapsedTime()
return round(self._timeRemaining, self.PRECISION_NUM_DECIMAL_PLACES)
def _whenTimeup(self):
with self._stateLock:
self._state = self.TIMEUP
self._timeRemaining = 0
if callable(self._userWhenTimeup):
self._userWhenTimeup()
def _elapsedTime(self):
return time() - self._startTime
问题
在此代码示例的上下文中,我如何公开竞争条件,修复它们并证明它们已被修复?
加分
适用于其他实现和问题的测试框架的额外点,而不是专门针对此代码。
外卖
我的想法是,重现已识别的竞争条件的技术解决方案是控制两个线程的同步,以确保它们按照将暴露错误的顺序执行。这里重要的一点是,他们已经已经确定竞争条件。我发现识别竞争条件的最佳方法是将代码用于代码审查,并鼓励更多专家对其进行分析。
答案 0 :(得分:8)
传统上,使用信号量强制执行多线程代码中的竞争条件,因此您可以强制线程等待,直到另一个线程在继续之前达到某个边缘条件。
例如,您的对象有一些代码可以检查如果对象已在运行,则不会调用start
。您可以通过执行以下操作来强制执行此条件以确保其行为符合预期:
KitchenTimer
AlreadyRunningError
要完成其中一些操作,您可能需要扩展KitchenTimer类。正式单元测试通常使用模拟对象,这些对象被定义为在关键时刻阻塞。模拟对象是一个比我在这里可以解决的更大的话题,但谷歌搜索“python模拟对象”将会出现很多文档和许多实现可供选择。
以下是一种可以强制代码抛出AlreadyRunningError
的方法:
import threading
class TestKitchenTimer(KitchenTimer):
_runningLock = threading.Condition()
def start(self, duration=1, whenTimeUp=None):
KitchenTimer.start(self, duration, whenTimeUp)
with self._runningLock:
print "waiting on _runningLock"
self._runningLock.wait()
def resume(self):
with self._runningLock:
self._runningLock.notify()
timer = TestKitchenTimer()
# Start the timer in a subthread. This thread will block as soon as
# it is started.
thread_1 = threading.Thread(target = timer.start, args = (10, None))
thread_1.start()
# Attempt to start the timer in a second thread, causing it to throw
# an AlreadyRunningError.
try:
thread_2 = threading.Thread(target = timer.start, args = (10, None))
thread_2.start()
except AlreadyRunningError:
print "AlreadyRunningError"
timer.resume()
timer.stop()
阅读代码,确定您要测试的一些边界条件,然后考虑在何处需要暂停计时器以强制出现该条件,并添加条件,信号量,事件等以使其成为可能发生。例如如果,当计时器运行whenTimeUp回调时,另一个线程试图阻止它会发生什么?您可以通过使计时器在输入_whenTimeUp:
后立即等待来强制执行该条件import threading
class TestKitchenTimer(KitchenTimer):
_runningLock = threading.Condition()
def _whenTimeup(self):
with self._runningLock:
self._runningLock.wait()
KitchenTimer._whenTimeup(self)
def resume(self):
with self._runningLock:
self._runningLock.notify()
def TimeupCallback():
print "TimeupCallback was called"
timer = TestKitchenTimer()
# The timer thread will block when the timer expires, but before the callback
# is invoked.
thread_1 = threading.Thread(target = timer.start, args = (1, TimeupCallback))
thread_1.start()
sleep(2)
# The timer is now blocked. In the parent thread, we stop it.
timer.stop()
print "timer is stopped: %r" % timer.isStopped()
# Now allow the countdown thread to resume.
timer.resume()
对要测试的类进行子类化并不是一种很好的方法来测试它:为了测试每个方法中的竞争条件,你必须基本上覆盖所有方法,并且在这一点上有一个好处要说明你并没有真正测试原始代码。相反,您可能会发现将信号量放在KitchenTimer对象中更加清晰,但默认情况下初始化为None,并且在获取或等待锁定之前让方法检查if testRunningLock is not None:
。然后,您可以对正在提交的实际代码强制进行比赛。
关于Python模拟框架的一些阅读可能会有所帮助。事实上,我不确定模拟是否有助于测试此代码:它几乎完全是自包含的,并且不依赖于许多外部对象。但是模拟教程有时会触及这些问题。我没有使用过这些,但是关于这些的文档就像一个开始的好地方:
答案 1 :(得分:8)
测试线程(un)安全代码的最常见解决方案是启动大量线程并希望获得最佳。问题我,我可以想象其他人,有这个问题,它依赖于机会,它使测试“沉重”。
我刚才遇到这个问题,我想要精确而不是蛮力。结果是一条测试代码通过让线程与颈部竞争来引起竞争条件。
spam = []
def set_spam():
spam[:] = foo()
use(spam)
如果从多个线程调用set_spam
,则在修改和使用spam
之间存在竞争条件。让我们尝试一致地重现它。
class TriggeredThread(threading.Thread):
def __init__(self, sequence=None, *args, **kwargs):
self.sequence = sequence
self.lock = threading.Condition()
self.event = threading.Event()
threading.Thread.__init__(self, *args, **kwargs)
def __enter__(self):
self.lock.acquire()
while not self.event.is_set():
self.lock.wait()
self.event.clear()
def __exit__(self, *args):
self.lock.release()
if self.sequence:
next(self.sequence).trigger()
def trigger(self):
with self.lock:
self.event.set()
self.lock.notify()
然后演示这个帖子的用法:
spam = [] # Use a list to share values across threads.
results = [] # Register the results.
def set_spam():
thread = threading.current_thread()
with thread: # Acquires the lock.
# Set 'spam' to thread name
spam[:] = [thread.name]
# Thread 'releases' the lock upon exiting the context.
# The next thread is triggered and this thread waits for a trigger.
with thread:
# Since each thread overwrites the content of the 'spam'
# list, this should only result in True for the last thread.
results.append(spam == [thread.name])
threads = [
TriggeredThread(name='a', target=set_spam),
TriggeredThread(name='b', target=set_spam),
TriggeredThread(name='c', target=set_spam)]
# Create a shifted sequence of threads and share it among the threads.
thread_sequence = itertools.cycle(threads[1:] + threads[:1])
for thread in threads:
thread.sequence = thread_sequence
# Start each thread
[thread.start() for thread in threads]
# Trigger first thread.
# That thread will trigger the next thread, and so on.
threads[0].trigger()
# Wait for each thread to finish.
[thread.join() for thread in threads]
# The last thread 'has won the race' overwriting the value
# for 'spam', thus [False, False, True].
# If set_spam were thread-safe, all results would be true.
assert results == [False, False, True], "race condition triggered"
assert results == [True, True, True], "code is thread-safe"
我想我已经解释了这个结构,所以你可以根据自己的情况实施它。我认为这非常适合“加分”部分:
适用于其他实现和问题的测试框架的额外点,而不是专门针对此代码。
每个线程问题都以自己的特定方式解决。在上面的例子中,我通过在线程之间共享一个值来引发竞争条件。使用全局变量(例如模块属性)时可能会出现类似问题。解决此类问题的关键可能是使用线程本地存储:
# The thread local storage is a global.
# This may seem weird at first, but it isn't actually shared among threads.
data = threading.local()
data.spam = [] # This list only exists in this thread.
results = [] # Results *are* shared though.
def set_spam():
thread = threading.current_thread()
# 'get' or set the 'spam' list. This actually creates a new list.
# If the list was shared among threads this would cause a race-condition.
data.spam = getattr(data, 'spam', [])
with thread:
data.spam[:] = [thread.name]
with thread:
results.append(data.spam == [thread.name])
# Start the threads as in the example above.
assert all(results) # All results should be True.
常见的线程问题是多线程同时读取和/或写入数据持有者的问题。通过实现读写锁来解决此问题。读写锁的实际实现可能不同。您可以选择先读锁定,先写入锁定或随机选择。
我确信有一些例子描述了这种锁定技术。我可能稍后会写一个例子,因为这已经是一个很长的答案了。 ; - )
查看the threading module documentation并稍微尝试一下。由于每个线程问题都不同,因此适用不同的解决方案。
关于线程的主题,请查看Python GIL(全局解释器锁)。重要的是要注意,线程实际上可能不是优化性能的最佳方法(但这不是您的目标)。我发现这个演示文稿非常好:https://www.youtube.com/watch?v=zEaosS1U5qY
答案 2 :(得分:4)
您可以使用大量线程进行测试:
import sys, random, thread
def timeup():
sys.stdout.write("Timer:: Up %f" % time())
def trdfunc(kt, tid):
while True :
sleep(1)
if not kt.isRunning():
if kt.start(1, timeup):
sys.stdout.write("[%d]: started\n" % tid)
else:
if random.random() < 0.1:
kt.stop()
sys.stdout.write("[%d]: stopped\n" % tid)
sys.stdout.write("[%d] remains %f\n" % ( tid, kt.timeRemaining))
kt = KitchenTimer()
kt.start(1, timeup)
for i in range(1, 100):
thread.start_new_thread ( trdfunc, (kt, i) )
trdfunc(kt, 0)
我看到了几个问题:
当线程看到计时器未运行并尝试启动它时, 代码通常会因为上下文切换而引发异常 测试并开始。我认为提出异常太多了。或者你可以 有一个原子testAndStart函数
停止时出现类似问题。您可以实现testAndStop 功能
即使是来自timeRemaining
函数的代码:
if self.isRunning():
self._timeRemaining = self.duration - self._elapsedTime()
需要某种原子性,也许你需要先抓住锁 测试是运行。
如果您打算在线程之间共享此类,则需要解决这些问题。
答案 3 :(得分:3)
总的来说 - 这不是可行的解决方案。您可以通过使用调试器(在代码中的某些位置设置断点)来重现此竞争条件,而不是在遇到其中一个断点时 - 冻结线程并运行代码直到它到达另一个断点,然后冻结此线程并解冻第一个断点线程,您可以使用此技术以任何方式交错线程执行。)
问题是 - 你拥有的线程和代码越多,交错副作用的方法就越多。实际上 - 它将以指数方式增长。一般来说,没有可行的解决方案来测试它。只有在一些简单的情况下才有可能。
这个问题的解决方案是众所周知的。编写能够识别副作用的代码,使用锁,信号量或队列等同步原语控制副作用,或者尽可能使用不可变数据。
也许更实际的方法是使用运行时检查来强制正确的呼叫顺序。例如(伪代码):
class RacyObject:
def __init__(self):
self.__cnt = 0
...
def isReadyAndLocked(self):
acquire_object_lock
if self.__cnt % 2 != 0:
# another thread is ready to start the Job
return False
if self.__is_ready:
self.__cnt += 1
return True
# Job is in progress or doesn't ready yet
return False
release_object_lock
def doJobAndRelease(self):
acquire_object_lock
if self.__cnt % 2 != 1:
raise RaceConditionDetected("Incorrect order")
self.__cnt += 1
do_job()
release_object_lock
如果在调用isReadyAndLock
之前未检查doJobAndRelease
,则此代码将抛出异常。只需一个线程即可轻松测试。
obj = RacyObject()
...
# correct usage
if obj.isReadyAndLocked()
obj.doJobAndRelease()