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

Notification

Icon
Error

Cleanup during NCrunch process exit?
GreenMoose
#1 Posted : Wednesday, April 20, 2016 7:58:51 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)
Hi.

I have a problem trying to do some file cleanup when NCrunch test process is exiting (idea is to keep a SQL CE db alive while process is alive, and delete it when process exits). I've tried both ways below but cannot get the file to be deleted, am I missing something?
What I did to test out the "cleanup" behavior was:
1) In static ctor of a fixture, create a file e.g. "e:\foo.txt"
2) in the "cleanup" methods below ("destructor" methods), delete "e:\foo.txt".

The foo.txt gets created but never deleted even though process is terminated.


http://stackoverflow.com/a/13258842
Code:

class StaticClass 
{
   static StaticClass() {
       AppDomain.CurrentDomain.ProcessExit +=
           StaticClass_Dtor;
   }

   static void StaticClass_Dtor(object sender, EventArgs e) {
        // clean it up
   }
}


and http://stackoverflow.com/a/18709110
Code:
public static class Foo
{
    private static readonly Destructor Finalise = new Destructor();

    static Foo()
    {
        // One time only constructor.
    }

    private sealed class Destructor
    {
        ~Destructor()
        {
            // One time only destructor.
        }
    }
}


Thanks.
Remco
#2 Posted : Wednesday, April 20, 2016 10:39:44 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi,

Unfortunately, there isn't a reliable way to do this, as NCrunch terminates its processes externally (by the engine itself).

In earlier versions, the process was responsible for terminating itself in response to requests from the engine, but this proved problematic as sometimes user code would prevent the process from being torn down. Since the process can often be highly unstable, it's important that the engine kills it entirely. This means that the standard cleanup procedure for the process doesn't have a chance to run.

I'd suggest looking at alternative ways of handling this scenario. Although it's often considered good practice for tests to clean up after themselves, in practice this is never reliable as a test run can be aborted at any time for a variety of reasons. A better design is usually to instead try to clean up on initialisation of the next test run - in this way, you can be sure the cleanup happens.
1 user thanked Remco for this useful post.
GreenMoose on 4/29/2016(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.020 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download