单元测试错误条件 - EINTR [英] Unit testing error conditions - EINTR

查看:49
本文介绍了单元测试错误条件 - EINTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,如何对系统调用中的 EINTR 等错误条件进行单元测试.

In short, how do you unit test an error condition such as EINTR on a system call.

我正在研究的一个特定示例(它本身可能就是一个案例)是,当 fclose 返回带有 (errno==EINTR) 的 EOF 时,是否有必要再次调用它.行为取决于 fclose 的实现:

One particular example I'm working on, which could be a case all by itself, is whether it's necessary to call fclose again when it returns EOF with (errno==EINTR). The behavior depends on the implementation of fclose:

// Given an open FILE *fp
while (fclose(fp)==EOF && errno==EINTR) {
    errno = 0;
}

如果在 EINTR 发生时释放 fp,则此调用可能不安全.如何测试何时 (errno==EINTR) 的错误处理?

This call can be unsafe if fp freed when EINTR occurs. How can I test the error handling for when (errno==EINTR)?

推荐答案

在这种特殊情况下,再次调用 fclose() 是不安全的,因为 C 标准说流与文件分离(并且变得不确定),即使如果调用失败.

In this particular case, it's not safe to call fclose() again, as the C standard says the stream is disassociated from the file (and becomes indeterminate) even if the call fails.

这篇关于单元测试错误条件 - EINTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆