Skip to content

Instantly share code, notes, and snippets.

@jasondentler
Created August 11, 2012 02:07
Show Gist options
  • Save jasondentler/3319958 to your computer and use it in GitHub Desktop.
Save jasondentler/3319958 to your computer and use it in GitHub Desktop.
Integration Testing Part 2
public static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
private static string GetRavenDbServicePath()
{
//HKLM\System\CurrentControlSet\Services\<%serviceNa me%>\ImagePath
const string path = @"System\CurrentControlSet\Services\RavenDB";
using (var reg = Registry.LocalMachine.OpenSubKey(path, false))
{
Debug.Assert(reg != null, "reg != null");
return (string)reg.GetValue("ImagePath", null);
}
}
private static string GetRavenDbServicePath()
{
const string path = @"System\CurrentControlSet\Services\RavenDB";
using (var reg = Registry.LocalMachine.OpenSubKey(path, false))
{
Debug.Assert(reg != null, "reg != null");
return (string)reg.GetValue("ImagePath", null);
}
}
private Process Start(int portNumber)
{
var path = Settings.RavenDbExecutablePath;
var args = string.Format("/ram --set=Raven/Port=={0}", portNumber);
var si = new ProcessStartInfo(path, args)
{
UseShellExecute = false,
CreateNoWindow = true
};
Console.WriteLine("Starting RavenDB in-memory server on port {0}", portNumber);
var process = Process.Start(si);
return process;
}
private void Stop(Process process)
{
Console.WriteLine("Stopping RavenDB in-memory server");
if (process.HasExited)
return;
if (process.StartInfo.RedirectStandardInput)
{
process.StandardInput.WriteLine("q");
process.WaitForExit((int) TimeSpan.FromSeconds(5).TotalMilliseconds);
if (process.HasExited)
return;
Console.WriteLine("RavenDB process appears to be hung. Attempting to kill it.");
}
process.Kill();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment