简体   繁体   English

Windows SQLite集成测试清除(“无法打开数据库文件”)

[英]Windows SQLite Integration Test Cleanup (“unable to open database file”)

I've added 2 C# integration tests for an existing SQLite application. 我为现有的SQLite应用程序添加了2个C#集成测试。 Each test creates a database file (using a Guid to guarantee uniqueness) and does a couple of operations on the database. 每个测试都会创建一个数据库文件(使用Guid来保证唯一性),并对数据库执行一些操作。 The tests pass in isolation but fail if run one after the other. 测试是孤立通过的,但是如果一个接一个地运行则失败。 The second test fails at the point of attempting to create a new SQLiteConnection, with "unable to open database file". 第二次测试在尝试创建一个新的SQLiteConnection时失败,并带有“无法打开数据库文件”。

I suspect SQLite's own temporary files are getting in the way. 我怀疑SQLite自己的临时文件正在妨碍您。 My question in summary is: can I do something to get SQLite to tidy up after itself properly at the end of a test ? 我的总结问题是: 在测试结束时,我可以做些什么使SQLite收拾自如吗?

Some more details, just in case any of this is relevant: 以防万一,其中一些更详细:

  • I'm using MobileServiceSQLLiteStore 我正在使用MobileServiceSQLLiteStore
  • I can see a number of files and folders in %TEMP% after the first test has completed, and before the second test attempts to create its database file: 在第一个测试完成之后,在第二个测试尝试创建其数据库文件之前,我可以在%TEMP%中看到许多文件和文件夹:

    1. File Report20180829-1035.diagsession 文件报告20180829-1035.diagsession
    2. Folder 39fbfdd5-5f6e-4563-9822-cb153e30e5b6.PackageExtraction 文件夹39fbfdd5-5f6e-4563-9822-cb153e30e5b6.PackageExtraction
    3. Folder 39FBFDD5-5F6E-4563-9822-CB153E30E5B6.scratch 文件夹39FBFDD5-5F6E-4563-9822-CB153E30E5B6.scratch
    4. Folder E41DD259-1CAA-4FEF-A779-003804F6B783 文件夹E41DD259-1CAA-4FEF-A779-003804F6B783
    5. Folder E41DD259-1CAA-4FEF-A779-003804F6B783.scratch 文件夹E41DD259-1CAA-4FEF-A779-003804F6B783.scratch
    6. Folder EAFBACB1-B978-419A-98B9-07258EB35C44 文件夹EAFBACB1-B978-419A-98B9-07258EB35C44
    7. Folder EAFBACB1-B978-419A-98B9-07258EB35C44.scratch 文件夹EAFBACB1-B978-419A-98B9-07258EB35C44.scratch

      • These are all removed at the end of the test apart from 1 and 2 -- and a new Report.39FBFDD5-5F6E-4563-9822-CB153E30E5B6 is left behind. 在测试结束时,除了1和2外,所有这些都被删除了,然后留下了新的Report.39FBFDD5-5F6E-4563-9822-CB153E30E5B6。 When the test next runs, they're all removed except for 1. 下次运行测试时,除了1以外,所有的都被删除。

The presence of the temporary SQLite files is a red herring. 临时SQLite文件的出现是红色的鲱鱼。 The real problem with my code is a line within the MS MobileServiceSQLLiteStore wrapper that I'm calling: 我的代码的真正问题是我正在调用的MS MobileServiceSQLLiteStore包装器中的一行:

MobileServiceClient.EnsureFileExists(dbPath);

This ultimately resolves to 最终解决

if (!File.Exists(path)) File.Create(path);

As this Stack Overflow answer points out, File.Create() will leave the file locked. 正如堆栈溢出答案所指出的那样,File.Create()将使文件保持锁定状态。 So my fix was to ensure the file empty, unlocked file existed beforehand: 所以我的解决方法是确保文件空的,未锁定的文件事先存在:

if (File.Exists(path)) File.Delete(path);
File.Create(path).Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM