我有一个单元测试,要验证我是否已经处理了曾经附加到主用户界面的文档。该单元测试必须是异步的,因为一切都需要在STA线程下运行,并且我必须等待用户界面被创建。
我有一个助手,可以将操作分派到STA线程上。
我在测试的主体中创建了memory对象,然后将其传递给下面的异步方法。
请查看用###标注的代码行,以查看实际的问题行。dotMemory报告对象尚不存在,但我已经做出了一个断言,证明对象确实存在。
(可以在 https://gist.github.com/bradphelan/cb4f484fbf6a7f9829de0dd52036fd63 找到STA助手类)
这是与异步和dotMemory相关的问题吗?
我有一个助手,可以将操作分派到STA线程上。
我在测试的主体中创建了memory对象,然后将其传递给下面的异步方法。
请查看用###标注的代码行,以查看实际的问题行。dotMemory报告对象尚不存在,但我已经做出了一个断言,证明对象确实存在。
(可以在 https://gist.github.com/bradphelan/cb4f484fbf6a7f9829de0dd52036fd63 找到STA助手类)
这是与异步和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);
}
}
Task<Document> CreateFoo()
而不是Document CreateFoo()
,那么你就有点道理了,但是那样的话代码将无法编译。 - bradgonesurfing