Created
August 11, 2012 02:07
-
-
Save jasondentler/3319958 to your computer and use it in GitHub Desktop.
Integration Testing Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int GetRandomUnusedPort() | |
{ | |
var listener = new TcpListener(IPAddress.Any, 0); | |
listener.Start(); | |
var port = ((IPEndPoint)listener.LocalEndpoint).Port; | |
listener.Stop(); | |
return port; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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