我有一些代码使用某些库来处理某些Web服务的API,后者又使用httplib,它使用套接字,而套接字有时会让客户跟踪堆栈跟踪:
File "/opt/zenoss/lib/python2.7/httplib.py", line 1027, in getresponse
response.begin()
File "/opt/zenoss/lib/python2.7/httplib.py", line 407, in begin
version, status, reason = self._read_status()
File "/opt/zenoss/lib/python2.7/httplib.py", line 365, in _read_status
line = self.fp.readline()
File "/opt/zenoss/lib/python2.7/socket.py", line 430, in readline
data = recv(1)
error: [Errno 104] Connection reset by peer
如何故意重现它,所以我可以测试处理它的代码?我正在考虑使用测试用例在socket库中模拟readline方法的unittest,但是我应该从那个模拟方法抛出哪个异常?
UPD :看起来应该是:
raise socket.error(104, 'Connection reset by peer')
问题还在:“现在我应该如何模拟套接字库?”。