简体   繁体   中英

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

I've added 2 C# integration tests for an existing SQLite application. Each test creates a database file (using a Guid to guarantee uniqueness) and does a couple of operations on the database. 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".

I suspect SQLite's own temporary files are getting in the way. My question in summary is: can I do something to get SQLite to tidy up after itself properly at the end of a test ?

Some more details, just in case any of this is relevant:

  • I'm using 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:

    1. File Report20180829-1035.diagsession
    2. Folder 39fbfdd5-5f6e-4563-9822-cb153e30e5b6.PackageExtraction
    3. Folder 39FBFDD5-5F6E-4563-9822-CB153E30E5B6.scratch
    4. Folder E41DD259-1CAA-4FEF-A779-003804F6B783
    5. Folder E41DD259-1CAA-4FEF-A779-003804F6B783.scratch
    6. Folder EAFBACB1-B978-419A-98B9-07258EB35C44
    7. Folder 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. When the test next runs, they're all removed except for 1.

The presence of the temporary SQLite files is a red herring. The real problem with my code is a line within the MS MobileServiceSQLLiteStore wrapper that I'm calling:

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. So my fix was to ensure the file empty, unlocked file existed beforehand:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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