Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite.Interop.dll locked after running Visual Studio 2012 Unit Test Framework tests

  • Visual Studio 2012
  • SQLite 1.0.82.0 (from nuget)

I am trying to use the "Run All" command in the "Test Explorer" The following error happens after you run the test once ... after that it will not build anymore, until you restart visual studio

Here is the build error

The Process cannot access the file 'SQLite.Interop.dll' because it is being used by another process

here is the code

using System.Data.SQLite;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.Sqlite
{
    [TestClass]
    public class Test_Sqlite_Locking
    {
        [TestMethod]
        public void can_create_table()
        {
            using(var fact = new SQLiteFactory())            
            using (var conn = fact.CreateConnection())
            {
                conn.ConnectionString = "Data Source=:memory:;Version=3;New=True;";
                conn.Open();
                //conn.Close();                
            }

            //SQLiteConnection.ClearAllPools();
            //GC.Collect();
        }
    }
}

I have tried, closing connection, calling ClearAllPools, GC.Collect, and creating the SQLiteConnection directly (instead of the Factory) ... still same issue

This DOES work if you DEBUG ALL TESTS ... but it is when you just Run the tests that this seems to lock it up

like image 242
ObeseCoder Avatar asked Oct 16 '12 16:10

ObeseCoder


4 Answers

I could not find that option in VS2012 (at least not for standard unit tests), therefore I came up with another solution:

The problem you are facing comes from the fact that the unit test runner remains loaded so that repeated test runs are faster. Since SQLite.Interop.dll will probably not change too often, I changed the CopyToOutputDirectory option to PreserveNewest rather than the default Always.

You can do this by opening the properties (F4) view and selecting the SQLite.Interop.dll file in your solution. The only time this will probably still lock up is when you upgrade to a newer version of SQLite and restarting VS2012 then works OK for me.

like image 55
Philipp Aumayr Avatar answered Nov 08 '22 01:11

Philipp Aumayr


I worked around this by using the following as a pre-build event on the affected test projects:

for 64-bit:

taskkill /F /IM vstest.executionengine.exe /FI "MEMUSAGE gt 1"

or for 32-bit:

taskkill /F /IM vstest.executionengine.x86.exe /FI "MEMUSAGE gt 1"

This silently kills the execution engine before building the test project. The /FI "MEMUSAGE gt 1" stops the command (and therefore the build) from failing if the execution engine isn't running.

like image 27
Arjailer Avatar answered Nov 08 '22 02:11

Arjailer


Try this:

  • In VS.NET, click on Tools \ Options
  • When the dialog appears, click on "Test Tools"
  • Click on "Test Execution"
  • Uncheck the box "Keep test execution engine running between tests"
  • Click OK and restart VS.NET

You should be able run SQLite based tests now without having to restart.

like image 8
MB. Avatar answered Nov 08 '22 02:11

MB.


In Visual Studio 2013 - Go to "Test > Test Settings > keep Test Execution Engine Runing" and uncheck it! Works for me.

like image 4
Daniel Moreira Avatar answered Nov 08 '22 01:11

Daniel Moreira