c# - 为什么用`count`参数0调用`StreamReader.ReadAsync`会推进底层流的`Position`?
问题描述
在使用System.IO.StreamReader
.NET Framework 的类时,我注意到在我看来奇怪的不一致行为:
调用该方法Read(Char[], Int32, Int32)
并传递参数(即读取零字节),底层对象的属性正如预期的那样。0
count
Position
Stream
0
然而,以同样的方式调用该ReadAsync(Char[], Int32, Int32)
方法,Position
底层Stream
对象的属性并不像我预期的那样为零,而是流内容的长度;我希望它在同步测试用例中为零。
这是框架实现中的错误还是我遗漏了什么?
以下测试代码演示:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Threading.Tasks;
namespace ExplorationTesting
{
[TestClass]
public class TestStreamReader
{
public static Stream CreateStream(string fromString)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(fromString);
writer.Flush();
stream.Position = 0;
return stream;
}
[TestMethod] // Test Passes
public void TestReadingZeroBytesFromStreamReader_StreamPositionIsZero()
{
using (Stream stream = CreateStream("Foo"))
using (StreamReader reader = new StreamReader(stream)) {
Assert.AreEqual(0, stream.Position);
char[] buffer = new char[] { };
var r = reader.Read(buffer, 0, 0);
Assert.AreEqual(0, r);
Assert.AreEqual(0, stream.Position);
}
}
[TestMethod] // Test Fails
public async Task TestReadingZeroBytesFromStreamReader_StreamPositionIsZero_Async()
{
using (Stream stream = CreateStream("Foo"))
using (StreamReader reader = new StreamReader(stream)) {
Assert.AreEqual(0, stream.Position);
char[] buffer = new char[] { };
var r = await reader.ReadAsync(buffer, 0, 0);
Assert.AreEqual(0, r);
Assert.AreEqual(0, stream.Position); // Assert.AreEqual failed. Expected:<0>. Actual:<3>.
}
}
}
}
解决方案
推荐阅读
- python - 以最低要求导出 Conda 环境
- c++ - 使用 getline() 时 while 和 if 的区别
- c - 在 c 中,当我在条件语句中分配一个字符串时,它有错误
- google-forms - 当我尝试打开受限的 Google 表单时如何更改帐户?
- r - 尝试在 R 中进行左连接合并,但想要选择特定的列。接收错误信息
- python - 如何在python中使用pandas将数据设置为csv文件
- swiftui - 如何获取 SwiftUI View 的当前颜色?
- opencart - 电子邮件订单 opencart 中的库存状态
- python - Python pandas在另一列的元素列表中查找一列的元素
- angular - 如何在 spec.test 文件中定义“component.QRcode”