8

我有一个系统,它在收到消息后将其排入队列(写入表),另一个进程轮询数据库并将其出列以进行处理。在我的自动测试中,我在同一个过程中合并了操作,但不能(从概念上)合并来自这两个操作的 NH 会话。

自然 - 出现问题。

我已经阅读了有关让 SQLite-InMemory-NHibernate 组合在测试世界中工作的所有内容,但是由于“没有这样的表”错误,我现在遇到了随机失败的测试。说清楚 - “随机”意味着具有相同确切配置和代码的相同测试有时会失败。

我有以下 SQLite 配置:

return SQLiteConfiguration
 .Standard
 .ConnectionString(x => x.Is("Data Source=:memory:; Version=3; New=True; Pooling=True; Max Pool Size=1;"))
 .Raw(NHibernate.Cfg.Environment.ReleaseConnections, "on_close");

在我的测试(每次测试)开始时,我获取“静态”会话提供程序,并请求它刷新现有的数据库,并重新创建模式:

public void PurgeDatabaseOrCreateNew()
{
    using (var session = GetNewSession())
    using (var tx = session.BeginTransaction())
    {
            PurgeDatabaseOrCreateNew(session);
            tx.Commit();
    }
}

private void PurgeDatabaseOrCreateNew(ISession session)
{
    //http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx
    new SchemaExport(_Configuration)
        .Execute(false, true, false, session.Connection, null);
}

所以是的,它在另一个会话上,但是连接在 SQLite 上汇集,所以我创建的下一个会话将看到生成的模式。然而,虽然它在大多数情况下都有效 - 有时后来的“入队”操作会失败,因为它看不到我传入消息的表。此外 - 这似乎在每个测试套件运行最多发生一到两次;并不是所有的测试都失败了,只有第一个(有时是另一个。不太确定是不是第二个)。

最糟糕的部分自然是随机性。我已经告诉自己我已经修复了好几次了,只是因为它只是“停止了失败”。随意。

这发生在 FW4.0、System.Data.SQLite x86 版本、Win7 64b 和 2008R2(总共三台不同的机器)、NH2.1.2、配置了 FNH、TestDriven.NET 32b 进程和 NUnit 控制台 32b 进程上。

帮助?

4

2 回答 2

8

嗨,我很确定我和你有完全相同的问题。我在每个集成测试中打开和关闭多个会话。在挖掘了 SQLite 连接池和我自己的一些实验之后,我得出了以下结论:

SQLite 池代码使用 Wea​​kReferences 缓存连接,这不是缓存的最佳选择,因为当没有对连接的正常(强)引用并且 GC 运行时,对连接的引用将被清除。由于您无法预测 GC 何时运行,这解释了“随机性”。尝试GC.Collect();在关闭一个会话和打开另一个会话之间添加一个,您的测试将始终失败。

我的解决方案是自己在打开会话之间缓存连接,如下所示:

public class BaseIntegrationTest
{
    private static ISessionFactory _sessionFactory;
    private static Configuration _configuration;
    private static SchemaExport _schemaExport;

    // I cache the whole session because I don't want it and the
    // underlying connection to get closed.
    // The "Connection" property of the ISession is what we actually want.
    // Using the NHibernate SQLite Driver to get the connection would probably
    // work too.
    private static ISession _keepConnectionAlive;

    static BaseIntegrationTest()
    {
        _configuration = new Configuration();
        _configuration.Configure();
        _configuration.AddAssembly(typeof(Product).Assembly);
        _sessionFactory = _configuration.BuildSessionFactory();
        _schemaExport = new SchemaExport(_configuration);

        _keepConnectionAlive = _sessionFactory.OpenSession();
    }

    [SetUp]
    protected void RecreateDB()
    {
        _schemaExport.Execute(false, true, false, _keepConnectionAlive.Connection, null);
    }

    protected ISession OpenSession()
    {
        return _sessionFactory.OpenSession(_keepConnectionAlive.Connection);
    }
}

我的每个集成测试都继承自此类,并调用 OpenSession() 来获取会话。由于 [SetUp] 属性,NUnit 在每次测试之前都会调用 RecreateDB。

我希望这可以帮助您或其他任何遇到此错误的人。

于 2012-01-05T14:55:39.660 回答
0

唯一想到的是,您在测试后随机打开会话。在打开另一个 ISession 之前,您必须确保关闭任何现有的 ISession。如果您不使用 using() 语句或手动调用 Dispose(),则会话可能在某处仍处于活动状态,从而导致这些随机异常。

于 2011-12-21T17:25:49.953 回答