c语言用deno输入重定向
问题总结(SUMMARY OF THE PROBLEM)
Recently, I have been working on an open-source Deno project centered around automatically generating documentation for your code based on your tests.
最近,我一直在致力于一个开源的Deno项目,该项目的重点是根据测试为代码自动生成文档。
During the testing process, particularly, when Deno interacts with external resources, I was getting a strange error that looked like this :
在测试过程中,尤其是在Deno与外部资源交互时,我遇到了一个奇怪的错误,看起来像这样:
AssertionError: Test case is leaking resources.
Before {
…
}
After {
…
}
解 (SOLUTION)
This is caused by Deno’s internal architecture which holds a record of open files, sockets and other concepts.
这是由Deno的内部体系结构引起的,该体系结构记录着打开文件,套接字和其他概念。
You can close a single resource by its RID :
您可以通过其RID关闭单个资源:
function Deno.close(rid: number): void;
Some functions provide easy access to this RID id. For example:
某些功能可轻松访问此RID ID。 例如:
const file: File = await Deno.open("/foo/bar.txt", { read: true});
Deno.close(file.rid);
Then, some other functions expose methods to close the resource internally :
然后,其他一些函数公开用于内部关闭资源的方法:
const res: Response = await fetch("http://www.google.com.br");
await res.body.close(); // or await res.body.cancel();
However, some functions are not that easy to handle. For example, Deno.createSync() does not return a handle for the opened resource. For these kind of situations, at any moment in a Deno program, you can get a list of open resources to target specific resources or iterate over all open resources.
但是,某些功能并不是那么容易处理。 例如对于Deno.createSync()不返回打开的资源的句柄。 对于这种情况,在Deno程序中的任何时候,您都可以获取一个开放资源列表以定位特定资源或遍历所有开放资源。
const openResources: ResourceMap = Deno.resources();/* Logging openResources will output something like the following
{
0: "...",
1: "..."
...
}
Here, keys are RID (resource IDs) and values are string representations of the module that opened the resource.
*/
If you are looking for a quick solution for your tests without messing with RID ids, you can simply ignore this mechanism by restructuring your tests as follows:
如果您正在寻找一种快速的测试解决方案而又不弄乱RID ID,则可以通过如下重组测试来简单地忽略此机制:
Deno.test({
name: "test name",
fn () {
// or async fn () {
/* your tests */
},
// following two options deactivate open resource checking
sanitizeResources: false,
sanitizeOps: false
});
参考资料 (REFERENCES)
Being a new technology, it was somewhat hard to get a clear answer. As always, a Stackoverflow question was one of the few pertinent elements:
作为一项新技术,很难获得明确的答案。 与往常一样,Stackoverflow问题是为数不多的相关元素之一:
Then, of course, there is the official documentation which provides more clarification.
然后,当然还有提供更多说明的官方文档。
Unfortunate that the documentation doesn’t appear among the top results during a Google search. Also, this problem is discussed only within the context of testing but I would assume this is as much relevant for your run-of-the-mill Deno project to prevent memory leaks. If you think otherwise, don’t hesitate share with me.
不幸的是,该文档没有出现在Google搜索的热门结果中。 此外,仅在测试的上下文中讨论了此问题,但我认为这与日常的Deno项目密切相关,以防止内存泄漏。 如果您有其他想法,请不要犹豫与我分享。
翻译自: https://medium.com/@emirgokhanozcelik/deno-test-case-is-leaking-resources-139b296db0c1
c语言用deno输入重定向