diff options
author | Alan McGovern <alan@xamarin.com> | 2013-10-22 17:21:24 +0400 |
---|---|---|
committer | Alan McGovern <alan@xamarin.com> | 2013-10-22 17:21:24 +0400 |
commit | 6b576a95e44addb33f8494d77a1322406312ebe7 (patch) | |
tree | 91462d8fbe27e4edbc3be27898982a4f745f9a34 | |
parent | aaf2b9650baad6c5c9f06f5f431958803f386135 (diff) |
[GuiUnit] Ensure the TcpWriter socket is always disposed correctly
We want to gracefully shut down the socket when possible.
-rw-r--r-- | src/framework/GuiUnit/TestRunner.cs | 22 | ||||
-rwxr-xr-x | src/framework/Runner/TcpWriter.cs | 9 |
2 files changed, 25 insertions, 6 deletions
diff --git a/src/framework/GuiUnit/TestRunner.cs b/src/framework/GuiUnit/TestRunner.cs index 9078cd5..7b3f01b 100644 --- a/src/framework/GuiUnit/TestRunner.cs +++ b/src/framework/GuiUnit/TestRunner.cs @@ -118,20 +118,30 @@ namespace GuiUnit /// <param name="args">An array of arguments</param> public void Execute(string[] args) { - // NOTE: Execute must be directly called from the - // test assembly in order for the mechanism to work. - Assembly callingAssembly = Assembly.GetCallingAssembly(); - this.commandLineOptions = new CommandLineOptions(); commandLineOptions.Parse(args); if (commandLineOptions.OutFile != null) this.writer = new StreamWriter(commandLineOptions.OutFile); - + + + TcpWriter tcpWriter = null; if (listener == TestListener.NULL && commandLineOptions.Port != -1) { - listener = new XmlTestListener (new TcpWriter (new IPEndPoint (IPAddress.Loopback, commandLineOptions.Port))); + tcpWriter = new TcpWriter (new IPEndPoint (IPAddress.Loopback, commandLineOptions.Port)); + listener = new XmlTestListener (tcpWriter); } + // Ensure we always dispose the socket correctly. + using (tcpWriter) + ExecuteWithListener (args, tcpWriter); + } + + void ExecuteWithListener (string[] args, TcpWriter tcpWriter) + { + // NOTE: Execute must be directly called from the + // test assembly in order for the mechanism to work. + Assembly callingAssembly = Assembly.GetCallingAssembly(); + if (!commandLineOptions.NoHeader) WriteHeader(this.writer); diff --git a/src/framework/Runner/TcpWriter.cs b/src/framework/Runner/TcpWriter.cs index 44cb642..a6472fb 100755 --- a/src/framework/Runner/TcpWriter.cs +++ b/src/framework/Runner/TcpWriter.cs @@ -51,6 +51,15 @@ namespace NUnitLite.Runner writer = new StreamWriter (stream, Encoding); } + protected override void Dispose(bool disposing) + { + if (disposing) { + using (socket) + socket.Shutdown(SocketShutdown.Both); + } + base.Dispose(disposing); + } + public override void Write(char value) { writer.Write(value); |