MbUnit Icarus 在此测试中自毁

发布于 2024-10-31 10:12:24 字数 2586 浏览 1 评论 0原文

我正在尝试使用 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);
        }
    }
}

I'm trying to test a multi-threaded IO class using MbUnit. My goal is to have the test fixture constructor execute 3 times, once for each row on the class. Then, for each instance, execute the tests multiple times on parallell threads.

However, Icarus blows up with an 'index out of range' on TaskRunner. I can't get the full stack, it spawns message boxes too fast.

What am I doing wrong, or is this a bug in 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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-11-07 10:12:24

我不会直接回答有关错误的问题,但我认为以下步骤将有助于找到错误,并且不会在弹出消息框中迷失

  • 减少总文件数,
    子文件夹的值要低得多
    查看错误是否仍然存在于 2 甚至 1 中
    文件计数

    • 您的测试代码不是
      超级简单,应该如此,
      为测试编写测试
      所以你知道他们正在跑步
      正确,也许是那些随机的下一个
      是问题所在,也许是某事
      否则,测试应该很容易。

    • 找出什么测试破坏了
      系统,您的代码包含 3 个测试,
      和构造函数,注释另外两个
      测试并看看哪一个产生
      错误

    • 您的线程重复 150
      看起来很恶心,也许可以尝试小一点
      如果错误是基本的,则为 2 或 3 之类的数字
      即使 2 根线也可能会断裂,如果你
      运行 150 个线程我可以理解
      消息框的麻烦

    • 添加日志记录并尝试捕获 - catch
      该索引异常并记录您的
      上课后仔细陈述
      检查一下我想你会看到
      问题更加清晰。

现在你无法弄清楚我认为的问题,你有太多的变量,更不用说你没有为你的 Cache 类提供代码,它可能包含一些导致它的简单错误,在 MBunit 功能甚至开始出现之前。

I wont answer direct question about bug but I think following steps will help find the error and not get lost in popping message boxes

  • decrease numbers of totalfiles ,
    subfolders to much lower values to
    see if error persists in 2 or even 1
    file counts

    • your tests code isn't
      super easy, as it should be,
      write tests for tests
      so you know they are running
      correct, maybe those random nexts
      are the problem, maybe something
      else, tests should be easy.

    • figure out what test breaks the
      system, your code contains 3 tests,
      and constructor, comment two other
      tests and see which one produces
      error

    • your threaded repeat 150
      looks pretty sick, maybe try smaller
      number like 2 or 3 if error is basic
      even 2 threads might break, if you
      run 150 threads I can understand
      your trouble with message boxes

    • add logging and try catch - catch
      that index exception and log your
      class state carefully, after
      inspecting it I think youll see
      problem much more clearly.

Right now you cant figure out the problem I think, you got too many variables , not to mention you didnt provide code for your Cache class which might contain some simple error that is causing it, before MBunit features even begin to show up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文