Skip to content

Commit b337a82

Browse files
committed
Making .NET ICommandExecutor interface extend IDisposable
1 parent 78af26a commit b337a82

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

dotnet/src/webdriver/DriverService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public abstract class DriverService : ICommandServer
3838
private int driverServicePort;
3939
private bool silent;
4040
private bool hideCommandPromptWindow;
41+
private bool isDisposed;
4142
private Process driverServiceProcess;
4243

4344
/// <summary>
@@ -285,9 +286,14 @@ protected static string FindDriverServiceExecutable(string executableName, Uri d
285286
/// <param name="disposing"><see langword="true"/> if the Dispose method was explicitly called; otherwise, <see langword="false"/>.</param>
286287
protected virtual void Dispose(bool disposing)
287288
{
288-
if (disposing)
289+
if (!this.isDisposed)
289290
{
290-
this.Stop();
291+
if (disposing)
292+
{
293+
this.Stop();
294+
}
295+
296+
this.isDisposed = true;
291297
}
292298
}
293299

dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="DriverServiceCommandExecutor.cs" company="WebDriver Committers">
1+
// <copyright file="DriverServiceCommandExecutor.cs" company="WebDriver Committers">
22
// Licensed to the Software Freedom Conservancy (SFC) under one
33
// or more contributor license agreements. See the NOTICE file
44
// distributed with this work for additional information
@@ -27,6 +27,7 @@ internal class DriverServiceCommandExecutor : ICommandExecutor
2727
{
2828
private DriverService service;
2929
private HttpCommandExecutor internalExecutor;
30+
private bool isDisposed;
3031

3132
/// <summary>
3233
/// Initializes a new instance of the <see cref="DriverServiceCommandExecutor"/> class.
@@ -87,11 +88,38 @@ public Response Execute(Command commandToExecute)
8788
{
8889
if (commandToExecute.Name == DriverCommand.Quit)
8990
{
90-
this.service.Dispose();
91+
this.Dispose();
9192
}
9293
}
9394

9495
return toReturn;
9596
}
97+
98+
/// <summary>
99+
/// Releases all resources used by the <see cref="DriverServiceCommandExecutor"/>.
100+
/// </summary>
101+
public void Dispose()
102+
{
103+
this.Dispose(true);
104+
}
105+
106+
/// <summary>
107+
/// Releases the unmanaged resources used by the <see cref="HttpCommandExecutor"/> and
108+
/// optionally releases the managed resources.
109+
/// </summary>
110+
/// <param name="disposing"><see langword="true"/> to release managed and resources;
111+
/// <see langword="false"/> to only release unmanaged resources.</param>
112+
protected virtual void Dispose(bool disposing)
113+
{
114+
if (!this.isDisposed)
115+
{
116+
if (disposing)
117+
{
118+
this.service.Dispose();
119+
}
120+
121+
this.isDisposed = true;
122+
}
123+
}
96124
}
97125
}

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class HttpCommandExecutor : ICommandExecutor
4040
private Uri remoteServerUri;
4141
private TimeSpan serverResponseTimeout;
4242
private bool enableKeepAlive;
43+
private bool isDisposed;
4344
private CommandInfoRepository commandInfoRepository = new WebDriverWireProtocolCommandInfoRepository();
4445

4546
/// <summary>
@@ -292,6 +293,28 @@ private Response CreateResponse(HttpResponseInfo stuff)
292293
return commandResponse;
293294
}
294295

296+
/// <summary>
297+
/// Releases all resources used by the <see cref="HttpCommandExecutor"/>.
298+
/// </summary>
299+
public void Dispose()
300+
{
301+
this.Dispose(true);
302+
}
303+
304+
/// <summary>
305+
/// Releases the unmanaged resources used by the <see cref="HttpCommandExecutor"/> and
306+
/// optionally releases the managed resources.
307+
/// </summary>
308+
/// <param name="disposing"><see langword="true"/> to release managed and resources;
309+
/// <see langword="false"/> to only release unmanaged resources.</param>
310+
protected virtual void Dispose(bool disposing)
311+
{
312+
if (!this.isDisposed)
313+
{
314+
this.isDisposed = true;
315+
}
316+
}
317+
295318
private class HttpRequestInfo
296319
{
297320
public HttpRequestInfo(Uri serverUri, Command commandToExecute, CommandInfo commandInfo)

dotnet/src/webdriver/Remote/ICommandExecutor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="ICommandExecutor.cs" company="WebDriver Committers">
1+
// <copyright file="ICommandExecutor.cs" company="WebDriver Committers">
22
// Licensed to the Software Freedom Conservancy (SFC) under one
33
// or more contributor license agreements. See the NOTICE file
44
// distributed with this work for additional information
@@ -16,12 +16,14 @@
1616
// limitations under the License.
1717
// </copyright>
1818

19+
using System;
20+
1921
namespace OpenQA.Selenium.Remote
2022
{
2123
/// <summary>
2224
/// Provides a way to send commands to the remote server
2325
/// </summary>
24-
public interface ICommandExecutor
26+
public interface ICommandExecutor : IDisposable
2527
{
2628
/// <summary>
2729
/// Gets the repository of objects containin information about commands.

dotnet/src/webdriver/Remote/RemoteWebDriver.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,11 @@ protected virtual void Dispose(bool disposing)
10911091
}
10921092
finally
10931093
{
1094+
if (this.CommandExecutor != null)
1095+
{
1096+
this.CommandExecutor.Dispose();
1097+
}
1098+
10941099
this.StopClient();
10951100
this.sessionId = null;
10961101
}

0 commit comments

Comments
 (0)