Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

[Isolated] not tearing down dotnet.exe process?
GreenMoose
#1 Posted : Sunday, April 14, 2024 4:24:11 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 507

Thanks: 145 times
Was thanked: 66 time(s) in 64 post(s)
[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)

Code:

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:
Quote:

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

Output Test2:
Quote:

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.
Remco
#2 Posted : Monday, April 15, 2024 12:16:10 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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.
GreenMoose
#3 Posted : Monday, April 15, 2024 6:41:21 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 507

Thanks: 145 times
Was thanked: 66 time(s) in 64 post(s)
Ok thanks. Though the doc says
Quote:

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).
Remco
#4 Posted : Monday, April 15, 2024 6:43:46 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
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.
1 user thanked Remco for this useful post.
GreenMoose on 4/15/2024(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.030 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download