Build/Test Issues

Cleanup during NCrunch process exit?

Started by GreenMoose on 3,648 views

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

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
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.
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.

Post a reply

Log in to reply.