Skip to content

Commit

Permalink
增加 Yield 和 Promotor 方案的单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivony committed Apr 27, 2016
1 parent c7f597e commit 77bf6ae
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
1 change: 1 addition & 0 deletions Ivony.Caching.Test/Ivony.Caching.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="DiskCacheTesting.cs" />
<Compile Include="MonitorTesting.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TaskTesting.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ivony.Caching\Ivony.Caching.csproj">
Expand Down
102 changes: 102 additions & 0 deletions Ivony.Caching.Test/TaskTesting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ivony.Caching.Test
{
[TestClass]
public class TaskTesting
{

/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext { get; set; }

#region 附加测试特性
//
// 编写测试时,可以使用以下附加特性:
//
// 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// 在运行每个测试之前,使用 TestInitialize 来运行代码
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// 在每个测试运行完之后,使用 TestCleanup 来运行代码
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion




[TestMethod]
public void YieldTest()
{
Task.Run( async () =>
{
string output = null;
Func<Task> foo = async () =>
{
output = "before yield";
await Task.Yield();
output = "after yield";
};
var task = foo();
await Task.Delay( TimeSpan.FromSeconds( 1 ) );
Assert.AreEqual( output, "after yield" );
} ).Wait();
}



[TestMethod]
public void PromotorTest()
{
Task.Run( async () =>
{
var output = (string) null;
var promotor = new TaskCompletionSource<object>();
Func<Task> foo = async () =>
{
output = "before promotor";
await promotor.Task;
output = "after promotor";
};
var task = foo();
await Task.Delay( TimeSpan.FromSeconds( 1 ) );
Assert.AreEqual( output, "before promotor" );
promotor.SetResult( null );
await Task.Delay( TimeSpan.FromSeconds( 1 ) );
Assert.AreEqual( output, "after promotor" );
} ).Wait();
}
}
}
2 changes: 1 addition & 1 deletion Ivony.Caching/DiskCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task<Stream> ReadStream( string cacheKey )
if ( readTask != null ) //如果当前正在读,则以当前读取结果返回。
return readTask.Result;

else //如果当前正在写,等写完后再读取一次
else //如果当前正在写,则再读取一次
return await ReadStream( cacheKey );

}
Expand Down

0 comments on commit 77bf6ae

Please sign in to comment.