我有一个单元测试,我试图验证我已经处理了一个曾经附加到主用户界面的文档.单元测试必须是异步的,因为一切都需要在STA线程下运行,我必须等待创建的用户界面.
我在测试的主体中创建了内存对象,然后将其传递给异步方法,如下所示.
请参阅使用###注释的代码行以查看实际问题行. dotMemory报告该对象尚不存在,但我已经做了一个断言,证明该对象确实存在.
(STA Helper课程可在https://gist.github.com/bradphelan/cb4f484fbf6a7f9829de0dd52036fd63找到)
这与async和dotMemory有关吗?
[Collection("Memory leaks")] public class MemLeakSpec { public MemLeakSpec(ITestOutputHelper output) { DotMemoryUnitTestOutput.SetoutputMethod(output.WriteLine); } [Fact] [DotMemoryUnit(FailIfRunWithoutSupport = true)] public void ShouldCollectProjectX() { dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectAsyncX(memory)).Wait(); }); } class Document { } class Container { public Document Document; }; Document CreateFoo() => new Document(); private async Task ShouldCollectProjectAsyncX(Memory memory) { await Task.Delay(TimeSpan.FromMilliseconds(50)); Container container = new Container(); memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(0); Document documentA = CreateFoo(); container.Document = documentA; // Verify with dotMemory that the object exists. // ### This fails even though I have verified // ### the document exists memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(1); // Open a new project which should dispose the old one and thus // remove any reference to GenericWeinCadFolder container.Document = null; memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(0); GC.KeepAlive(container); } }
我已经创建了相同的上述测试的同步版本,它不会失败.在ShouldCollectAsync和ShouldCollectSync下面有两个测试.异步的一个失败,同步一个通过.
[Collection("Memory leaks")] public class MemLeakSpec { public MemLeakSpec(ITestOutputHelper output) { DotMemoryUnitTestOutput.SetoutputMethod(output.WriteLine); } [Fact] [DotMemoryUnit(FailIfRunWithoutSupport = true)] public void ShouldCollectAsync() { dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectAsyncX(memory)).Wait(); }); } /// This test is almost identical to the ShouldCollectAsync /// but it passes [Fact] [DotMemoryUnit(FailIfRunWithoutSupport = true)] public void ShouldCollectSync () { dotMemory.Check (memory => { STAThread.Run(() => ShouldCollectProjectSync(memory)); }); } class Document { } class Container { public Document Document; }; Document CreateFoo() => new Document(); private async Task ShouldCollectProjectSync(Memory memory) { Container container = new Container(); memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(0); Document documentA = CreateFoo(); container.Document = documentA; // Verify with dotMemory that the object exists. // #### Passes here memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(1); GC.KeepAlive(documentA); GC.KeepAlive(container); } private async Task ShouldCollectProjectAsyncX(Memory memory) { await Task.Delay(TimeSpan.FromMilliseconds(50)); Container container = new Container(); memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(0); Document documentA = CreateFoo(); container.Document = documentA; // Verify with dotMemory that the object exists. // #### FAILS here memory.Getobjects(@where => @where.Type.Is<Document>()) .ObjectsCount.Should() .Be(1); GC.KeepAlive(documentA); GC.KeepAlive(container); } }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。