Cyr*_*don 9 .net c# parallel-processing multithreading unit-testing
类包含应该只创建一次的属性.创建过程是通过Func<T>参数传递的.这是缓存方案的一部分.
测试时要注意,无论有多少线程尝试访问该元素,创建只会发生一次.
单元测试的机制是在访问器周围启动大量线程,并计算调用创建函数的次数.
这根本不是确定性的,没有任何保证可以有效地测试多线程访问.也许一次只有一个线程可以锁定.(实际上,getFunctionExecuteCount如果lock不存在,则在7到9之间......在我的机器上,没有任何保证在CI服务器上它将是相同的)
如何以确定的方式重写单元测试?如何确保lock多个线程多次触发?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Example.Test
{
public class MyObject<T> where T : class
{
private readonly object _lock = new object();
private T _value = null;
public T Get(Func<T> creator)
{
if (_value == null)
{
lock (_lock)
{
if (_value == null)
{
_value = creator();
}
}
}
return _value;
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void MultipleParallelGetShouldLaunchGetFunctionOnlyOnce()
{
int getFunctionExecuteCount = 0;
var cache = new MyObject<string>();
Func<string> creator = () =>
{
Interlocked.Increment(ref getFunctionExecuteCount);
return "Hello World!";
};
// Launch a very big number of thread to be sure
Parallel.ForEach(Enumerable.Range(0, 100), _ =>
{
cache.Get(creator);
});
Assert.AreEqual(1, getFunctionExecuteCount);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最糟糕的情况是如果有人破坏了lock代码,测试服务器有一些滞后.此测试不应通过:
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Example.Test
{
public class MyObject<T> where T : class
{
private readonly object _lock = new object();
private T _value = null;
public T Get(Func<T> creator)
{
if (_value == null)
{
// oups, some intern broke the code
//lock (_lock)
{
if (_value == null)
{
_value = creator();
}
}
}
return _value;
}
}
[TestFixture]
public class UnitTest1
{
[Test]
public void MultipleParallelGetShouldLaunchGetFunctionOnlyOnce()
{
int getFunctionExecuteCount = 0;
var cache = new MyObject<string>();
Func<string> creator = () =>
{
Interlocked.Increment(ref getFunctionExecuteCount);
return "Hello World!";
};
Parallel.ForEach(Enumerable.Range(0, 2), threadIndex =>
{
// testing server has lag
Thread.Sleep(threadIndex * 1000);
cache.Get(creator);
});
// 1 test passed :'(
Assert.AreEqual(1, getFunctionExecuteCount);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了使它具有确定性,你只需要两个线程并确保其中一个在函数内部阻塞,而另一个也试图进入内部.
[TestMethod]
public void MultipleParallelGetShouldLaunchGetFunctionOnlyOnce()
{
var evt = new ManualResetEvent(false);
int functionExecuteCount = 0;
var cache = new MyObject<object>();
Func<object> creator = () =>
{
Interlocked.Increment(ref functionExecuteCount);
evt.WaitOne();
return new object();
};
var t1 = Task.Run(() => cache.Get(creator));
var t2 = Task.Run(() => cache.Get(creator));
// Wait for one task to get inside the function
while (functionExecuteCount == 0)
Thread.Yield();
// Allow the function to finish executing
evt.Set();
// Wait for completion
Task.WaitAll(t1, t2);
Assert.AreEqual(1, functionExecuteCount);
Assert.AreEqual(t1.Result, t2.Result);
}
Run Code Online (Sandbox Code Playgroud)
你可能想在这个测试中设置超时:)
这是一个允许测试更多案例的变体:
public void MultipleParallelGetShouldLaunchGetFunctionOnlyOnce()
{
var evt = new ManualResetEvent(false);
int functionExecuteCount = 0;
var cache = new MyObject<object>();
Func<object> creator = () =>
{
Interlocked.Increment(ref functionExecuteCount);
evt.WaitOne();
return new object();
};
object r1 = null, r2 = null;
var t1 = new Thread(() => { r1 = cache.Get(creator); });
t1.Start();
var t2 = new Thread(() => { r2 = cache.Get(creator); });
t2.Start();
// Make sure both threads are blocked
while (t1.ThreadState != ThreadState.WaitSleepJoin)
Thread.Yield();
while (t2.ThreadState != ThreadState.WaitSleepJoin)
Thread.Yield();
// Let them continue
evt.Set();
// Wait for completion
t1.Join();
t2.Join();
Assert.AreEqual(1, functionExecuteCount);
Assert.IsNotNull(r1);
Assert.AreEqual(r1, r2);
}
Run Code Online (Sandbox Code Playgroud)
如果你想延迟第二次调用,你将无法使用Thread.Sleep,因为它会导致线程进入WaitSleepJoin状态:
线程被阻止.这可能是调用
Thread.Sleep或Thread.Join请求锁定的结果 - 例如,通过调用Monitor.Enter或Monitor.Wait- 或等待线程同步对象,例如ManualResetEvent.
而且我们无法分辨线程是否正在睡觉或等待你ManualResetEvent...
但是你可以在忙碌的等待中轻松替换睡眠.注释掉lock,并t2改为:
var t2 = new Thread(() =>
{
var sw = Stopwatch.StartNew();
while (sw.ElapsedMilliseconds < 1000)
Thread.Yield();
r2 = cache.Get(creator);
});
Run Code Online (Sandbox Code Playgroud)
现在测试将失败.
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |