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?
- Is there any way to put these test in its own cc.net task and put just that task on a queue?
- Is there any msbuild/nant/ccnet task or whatever that handles queuing?
- 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?
- 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();
}
}
}