Tom*_*Tom 7 c python linux unit-testing
我在Linux上有一个Python和C应用程序,它应该在从磁盘读取文件时正确处理IO错误.应用程序的大部分是用Python编写的,带有C扩展,用于执行IO.在此扩展中,检测到IO错误.
有两种情况可能会出现错误.
stat
)比使用时读取的文件大fread
.我可以很容易地测试和处理1号案例.但是,我还想为案例2编写单元测试.但是,我不知道如何为测试触发"假的"IO错误.这甚至可能吗?有没有更好的方法来测试这种错误?
errno(3)仅设置EIO
为
Run Code Online (Sandbox Code Playgroud)EIO Input/output error (POSIX.1)
另外,根据阅读(2):
Run Code Online (Sandbox Code Playgroud)EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling terminal, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.
并根据写(2):
Run Code Online (Sandbox Code Playgroud)EIO A low-level I/O error occurred while modifying the inode.
因此,模拟特定的错误代码可能很困难; 注意到有其他系统调用用于I/O,特别是writev可以获得(2)和(间接)MMAP(2) ,但read(2)
并write(2)
是最常见的.
另请注意,文件系统和Linux内核(例如其VFS层)都是缓存数据.你可以得到EIO
更晚或永远.请参阅sync(2)和fsync(2)
但是,一般来说,大多数软件并不EIO
特别处理其他错误代码; 你可能通过获取另一个错误代码来测试,例如
Run Code Online (Sandbox Code Playgroud)EDQUOT The user's quota of disk blocks on the filesystem containing the file referred to by fd has been exhausted.
因此,您可能通过限制磁盘配额(参见quotactl(2),setquota(8)等...)和文件空间(参见setrlimit(2) with RLIMIT_FSIZE
,prlimit(1),ulimit
内置bash(1)等进行测试. ...)
如果你真的想特意伪装,EIO
你可能会对设备造成物理损坏(或者可能只是在错误的时刻拔掉USB磁盘),或者在用户空间(FUSE)中编写自己的文件系统来模拟它.我不认为这是值得的努力(因为当某些东西得到EIO
整个计算机变得非常快速无法使用,并且用户将注意到无论如何....并且因为大多数软件同样处理所有错误代码 - 除了EINTR
)
在代码的C部分中,您可能希望使用strerror(3)(可能使用syslog(3))和/或perror(3).我不确定是否值得努力处理EIO
大多数其他错误.
注意:许多关键领域都有标准,用于定义如何处理错误以及应该开发和测试代码,例如汽车中的ISO26262或航空电子设备中的DO-178B.遵循您的域名标准.