Lil*_*ver 6 mbunit multithreading unit-testing gallio icarus
我正在尝试使用MbUnit测试多线程IO类.我的目标是让测试夹具构造函数执行3次,对于类中的每一行执行一次.然后,对于每个实例,在并行线程上多次执行测试.
但是,Icarus在TaskRunner上爆发了"索引超出范围".我无法获得完整的堆栈,它会过快地生成消息框.
我做错了什么,或者这是MbUnit/Gallio中的错误?
using System;
using System.Collections.Generic;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using System.IO;
namespace ImageResizer.Plugins.DiskCache.Tests {
[TestFixture]
[Row(0,50,false)]
[Row(0,50,true)]
[Row(8000,100,true)]
public class CustomDiskCacheTest {
public CustomDiskCacheTest(int subfolders, int totalFiles, bool hashModifiedDate) {
char c = System.IO.Path.DirectorySeparatorChar;
string folder = System.IO.Path.GetTempPath().TrimEnd(c) + c + System.IO.Path.GetRandomFileName();
cache = new CustomDiskCache(folder,subfolders,hashModifiedDate);
this.quantity = totalFiles;
for (int i = 0; i < quantity;i++){
cache.GetCachedFile(i.ToString(),"test",delegate(Stream s){
s.WriteByte(32); //Just one space
},defaultDate, 10);
}
}
int quantity;
CustomDiskCache cache = null;
DateTime defaultDate = new DateTime(2011, 1, 1);
[ThreadedRepeat(150)]
[Test(Order=1)]
public void TestAccess() {
CacheResult r =
cache.GetCachedFile(new Random().Next(0, quantity).ToString(), "test",
delegate(Stream s) { Assert.Fail("No files have been modified, this should not execute"); }, defaultDate, 100);
Assert.IsTrue(System.IO.File.Exists(r.PhysicalPath));
Assert.IsTrue(r.Result == CacheQueryResult.Hit);
}
volatile int seed = 0;
[Test (Order=2)]
[ThreadedRepeat(20)]
public void TestUpdate() {
//try to get a unique date time value
DateTime newTime = DateTime.UtcNow.AddDays(seed++);
CacheResult r =
cache.GetCachedFile(new Random().Next(0, quantity).ToString(), "test",
delegate(Stream s) {
s.WriteByte(32); //Just one space
}, newTime, 100);
Assert.AreEqual<DateTime>(newTime, System.IO.File.GetLastWriteTimeUtc(r.PhysicalPath));
Assert.IsTrue(r.Result == CacheQueryResult.Miss);
}
[Test(Order=3)]
public void TestClear() {
System.IO.Directory.Delete(cache.PhysicalCachePath, true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不会直接回答有关错误的问题,但我认为以下步骤将有助于找到错误,并且不会迷失在弹出的消息框中
将总文件数、子文件夹数减少到更低的值,看看错误是否仍然存在于 2 个甚至 1 个文件计数中
您的测试代码并不是非常简单,因为它应该如此,为测试编写测试,以便您知道它们运行正确,也许那些随机的下一个是问题,也许是其他原因,测试应该很容易。
找出哪个测试破坏了系统,您的代码包含 3 个测试和构造函数,注释另外两个测试并查看哪一个产生错误
你的线程重复 150 看起来很恶心,如果错误是基本的,也许尝试更小的数字,比如 2 或 3,即使 2 个线程也可能会中断,如果你运行 150 个线程,我可以理解你的消息框问题
添加日志记录并尝试捕获 - 捕获该索引异常并仔细记录您的类状态,在检查之后我认为您会更清楚地看到问题。
现在你无法弄清楚我认为的问题,你有太多的变量,更不用说你没有为你的 Cache 类提供代码,它可能包含一些导致它的简单错误,在 MBunit 功能甚至开始出现之前。
归档时间: |
|
查看次数: |
374 次 |
最近记录: |