1

We have tests using WatiN that we want to run on our CruiseControl.NET server. We have got it working with one single build. When we enable these tests in other builds they fail when they run at the same time. We would like to avoid putting all the builds running these tests on the same cc.net queue, because these tests are just a small portion of the whole build time. We would also like to avoid having seperate build projects for these tests because that would literally double the amount of builds in our cc.net setup.

What options do we have?

  1. Is there any way to put these test in its own cc.net task and put just that task on a queue?
  2. Is there any msbuild/nant/ccnet task or whatever that handles queuing?
  3. Is there any command line tool that we can run from our msbuild script that handles queuing of command line tasks, so that we can run our tests using a nunit command line calls?
  4. Is there any other smart solutions to this problem?

If we dont find any existing solution to this problem, we will probably build something ourselves, if so, which solution would be recommended?

EDIT: This was my final implementation of the mutex:

public class SystemLevelLock : IDisposable
{
    private readonly string _id;
    private bool _isAquired;
    private Mutex _mutex;

    public SystemLevelLock(string id)
    {
        _id = id;
        Aquire();
    }

    public SystemLevelLock() : this(GetApplicationId()) { }

    private void Aquire()
    {
        try
        {
            var mutex = GetMutex();
            _isAquired = mutex.WaitOne(TimeSpan.FromMinutes(1), false);
            if (!_isAquired)
                throw new Exception("System level mutex could not be aquired");
        }
        catch (AbandonedMutexException)
        {
            // Mutex was abandoned by another process (it probably crashed)
            // Mutex was aquired by this process instead
        }
    }

    private Mutex GetMutex() { return _mutex ?? (_mutex = MakeMutex()); }

    private Mutex MakeMutex()
    {
        var mutexId = string.Format("Global\\{{{0}}}", _id);
        var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
        var securitySettings = new MutexSecurity();
        securitySettings.AddAccessRule(allowEveryoneRule);
        var mutex = new Mutex(false, mutexId);
        mutex.SetAccessControl(securitySettings);
        return mutex;
    }

    private void Release()
    {
        if (_isAquired)
            _mutex.ReleaseMutex();
    }

    public void Dispose() { Release(); }

}

And this is the Browser class implementation that uses the mutex. This is also using the SpecFlow scenariocontext to store the browser instance and lock for this scenario.

public static class Browser
{

    public static IE Current
    {
        get
        {
            if (!IsStarted())
                Start();
            return ScenarioBrowser;
        }
    }

    private static IE ScenarioBrowser
    {
        get
        {
            if (ScenarioContext.Current.ContainsKey("browser"))
                return ScenarioContext.Current["browser"] as IE;
            return null;
        }
        set
        {
            if (value == null)
            {
                if (ScenarioContext.Current.ContainsKey("browser"))
                    ScenarioContext.Current.Remove("browser");
            }
            else
                ScenarioContext.Current["browser"] = value;
        }
    }

    private static IDisposable BrowserLock
    {
        get
        {
            if (ScenarioContext.Current.ContainsKey("browserLock"))
                return ScenarioContext.Current["browserLock"] as IDisposable;
            return null;
        }
        set
        {
            if (value == null)
            {
                if (ScenarioContext.Current.ContainsKey("browserLock"))
                    ScenarioContext.Current.Remove("browserLock");
            }
            else
                ScenarioContext.Current["browserLock"] = value;
        }
    }

    private static void LockBrowser()
    {
        BrowserLock = MakeBrowserLock();
    }

    private static void ReleaseBrowser()
    {
        BrowserLock.Dispose();
        BrowserLock = null;
    }

    private static SystemLevelLock MakeBrowserLock() { return new SystemLevelLock("WatiNBrowserLock"); }

    private static void Start()
    {
        LockBrowser();
        var browser = new IE();
        ScenarioBrowser = browser;
    }

    public static bool IsStarted() { return ScenarioBrowser != null; }

    public static void Close()
    {
        try
        {
            var browser = ScenarioBrowser;
            ScenarioBrowser = null;
            browser.Close();
            browser.Dispose();
        }
        finally
        {
            ReleaseBrowser();
        }
    }

}
MatteS
  • 1,542
  • 1
  • 16
  • 35

1 Answers1

1

It seems like your tests are not isolated. You need ensure that the tests set up and run and in their own isolated environments. Do the tests depend on shared resources? Tests/Servers/Urls etc?

Since your IE is a shared resource (are you sure it's not the site). perhaps you could pause the the process with a custom task that waits on a system level named mutex.

EDIT: This was my (MatteS) final implementation of the mutex:

public class SystemLevelLock : IDisposable
{
    private readonly string _id;
    private bool _isAquired;
    private Mutex _mutex;

    public SystemLevelLock(string id)
    {
        _id = id;
        Aquire();
    }

    public SystemLevelLock() : this(GetApplicationId()) { }

    private void Aquire()
    {
        try
        {
            var mutex = GetMutex();
            _isAquired = mutex.WaitOne(TimeSpan.FromMinutes(1), false);
            if (!_isAquired)
                throw new Exception("System level mutex could not be aquired");
        }
        catch (AbandonedMutexException)
        {
            // Mutex was abandoned by another process (it probably crashed)
            // Mutex was aquired by this process instead
        }
    }

    private Mutex GetMutex() { return _mutex ?? (_mutex = MakeMutex()); }

    private Mutex MakeMutex()
    {
        var mutexId = string.Format("Global\\{{{0}}}", _id);
        var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
        var securitySettings = new MutexSecurity();
        securitySettings.AddAccessRule(allowEveryoneRule);
        var mutex = new Mutex(false, mutexId);
        mutex.SetAccessControl(securitySettings);
        return mutex;
    }

    private void Release()
    {
        if (_isAquired)
            _mutex.ReleaseMutex();
    }

    public void Dispose() { Release(); }

}

And this is the Browser class implementation that uses the mutex. This is also using the SpecFlow scenariocontext to store the browser instance and lock for this scenario.

public static class Browser
{

    public static IE Current
    {
        get
        {
            if (!IsStarted())
                Start();
            return ScenarioBrowser;
        }
    }

    private static IE ScenarioBrowser
    {
        get
        {
            if (ScenarioContext.Current.ContainsKey("browser"))
                return ScenarioContext.Current["browser"] as IE;
            return null;
        }
        set
        {
            if (value == null)
            {
                if (ScenarioContext.Current.ContainsKey("browser"))
                    ScenarioContext.Current.Remove("browser");
            }
            else
                ScenarioContext.Current["browser"] = value;
        }
    }

    private static IDisposable BrowserLock
    {
        get
        {
            if (ScenarioContext.Current.ContainsKey("browserLock"))
                return ScenarioContext.Current["browserLock"] as IDisposable;
            return null;
        }
        set
        {
            if (value == null)
            {
                if (ScenarioContext.Current.ContainsKey("browserLock"))
                    ScenarioContext.Current.Remove("browserLock");
            }
            else
                ScenarioContext.Current["browserLock"] = value;
        }
    }

    private static void LockBrowser()
    {
        BrowserLock = MakeBrowserLock();
    }

    private static void ReleaseBrowser()
    {
        BrowserLock.Dispose();
        BrowserLock = null;
    }

    private static SystemLevelLock MakeBrowserLock() { return new SystemLevelLock("WatiNBrowserLock"); }

    private static void Start()
    {
        LockBrowser();
        var browser = new IE();
        ScenarioBrowser = browser;
    }

    public static bool IsStarted() { return ScenarioBrowser != null; }

    public static void Close()
    {
        try
        {
            var browser = ScenarioBrowser;
            ScenarioBrowser = null;
            browser.Close();
            browser.Dispose();
        }
        finally
        {
            ReleaseBrowser();
        }
    }

}
MatteS
  • 1,542
  • 1
  • 16
  • 35
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Yes im sure its not the site. Although the tests would run fine on the same site, they do run on its own site. I have replicated the scenario on my machine trying to run 2 WatiN tests in paralell, and it doesnt work. Google'v told me WatiN cant handle multiple tests running in paralell because of different reasons, be it shared session state, shared windows session, ie driver issues, or what not. Anyway, can you show me how to do that mutex magic using C# code? – MatteS Sep 05 '11 at 10:35
  • Found some info regarding that mutex, ill try writing an MSBuild task for this and evaluate. – MatteS Sep 05 '11 at 11:50
  • I didnt make it an MSBuild task, but instead used event definitions BeforeScenario and AfterScenario that SpecFlow provides to create a system level mutex. This works very well. I did look at this code to make sure it was made stable :http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c – MatteS Sep 05 '11 at 13:19
  • It would really useful if you edit the answer with the specifics please? That way future users can see how you achieved it. – Preet Sangha Sep 05 '11 at 23:38
  • Right! Added the solution in code to your answer. Hope that was what u ment =) – MatteS Sep 06 '11 at 10:06