2011-04-11 15 views
6

我想用MbUnit测试一个多线程的IO类。我的目标是让测试夹具构造函数执行3次,每次在该类上执行一次。然后,对于每个实例,在并行线程上多次执行测试。MbUnit伊卡洛斯在这个测试上自毁

但是,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); 
     } 
    } 
} 

回答

1

我不会回答关于错误直接的问题,但我认为以下步骤将帮助找出错误,而不是迷失在大跌眼镜的消息框

  • totalfiles的减少数量, 子文件夹值低得多到 看看错误2仍然存在,甚至1 文件计数

    • 你的测试ç ODE不 超级容易,因为它应该是,测试 写测试,所以你知道他们正在运行 正确的,也许那些随机的nextS 都是问题,或许真的 别的,测试应该很容易。

    • 弄清楚打破考什么的 系统,您的代码包含3次测试, 和构造,评论其他两个 测试,看看哪一个产生 错误

    • 您螺纹重复150 看起来很舒服,也许尝试小 数字,如2或3,如果错误是基本 甚至2个线程有可能打破,如果你 运行150个线程我能理解你的 与消息框麻烦

    • 添加日志记录并尝试捕获 - 捕获 那个索引异常并仔细记录你的 类的状态,在检查它之后 我想你会更清楚地看到 问题。

现在你无法弄清楚我觉得这个问题,你有太多的变数,更何况你没有为你的Cache类代码可能含有导致它一些简单的错误,前MBunit功能甚至开始出现。