Daily Usage Issues

[Isolated] not tearing down dotnet.exe process?

Started by GreenMoose on 1,834 views

[v5.6.0.1/vs2022/net8]

Given below code using [Isolated] attribute, shouldn't the dotnet.exe process be terminated once a test completes?
(second test will fail due to dotnet.exe process is still hanging around)


using System.Reflection;
using NCrunch.Framework;
using NUnit.Framework;

namespace Tests;

[TestFixture]
internal class NCrunchAtomicTest
{
    #region Public members

    [Test]
    [Isolated]
    [ExclusivelyUses("File")]
    public void LockFileTest1()
    {
        LockFile();
    }

    [Test]
    [Isolated]
    [ExclusivelyUses("File")]
    public void LockFileTest2()
    {
        LockFile();
    }

    [SetUp]
    public void NCrunchAtomicTestSetUp()
    {
        string assemblyLocation = Assembly.GetExecutingAssembly().Location;
        string directory = Path.GetDirectoryName(assemblyLocation);
        _filePath = Path.Combine(directory, "file.txt");
        Console.WriteLine($"{Environment.ProcessId:D5} Using file {_filePath}.");
    }

    #endregion

    #region Private members

    private static FileStream? _fileStream;

    private string _filePath;

    private void LockFile()
    {
        _fileStream = new FileStream(_filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite,
            FileShare.None);
    }

    #endregion
}



Output Test1:

46056 Using file C:\NC\WC\37432\148\test\...\bin\Debug\net8.0\file.txt.

Output Test2:

41524 Using file C:\NC\WC\37432\148\test\...\bin\Debug\net8.0\file.txt.
System.IO.IOException : The process cannot access the file 'C:\NC\WC\37432\148\test\...\bin\Debug\net8.0\file.txt' because it is being used by another process.

Edited

I suspect the issue here is likely because the FileStream is not being disposed.

IsolatedAttribute will run the test inside a dedicated process, but it won't tear the process down when the execution is complete. The process hangs around until its space in the process pool is needed, at which point it will self-terminate. In the case of the tests above, the process is hanging around but the engine never uses it again. This results in the file lock exception.
Ok thanks. Though the doc says

Tests that are marked with this attribute will be executed in a process that is spawned specifically for the test alone. When the test completes execution, the process will be torn down and will not be reused for other tests

so maybe it needs a bit of clarification? (I get the impression that the process will exit after each test using the isolated attribute)

(The non disposal of the file stream was just to clarify that the process is kept running).

Edited

Yes, I think the documentation here is wrong. It represents an intention of something that wasn't implemented.

The smarter thing to do here would be to change the engine to match the documentation, actually. I'll see about getting this scoped.
This post has been deleted.

Post a reply

Log in to reply.