Skip to content

Commit

Permalink
Making .NET ICommandExecutor interface extend IDisposable
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Apr 2, 2018
1 parent 78af26a commit b337a82
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
10 changes: 8 additions & 2 deletions dotnet/src/webdriver/DriverService.cs
Expand Up @@ -38,6 +38,7 @@ public abstract class DriverService : ICommandServer
private int driverServicePort;
private bool silent;
private bool hideCommandPromptWindow;
private bool isDisposed;
private Process driverServiceProcess;

/// <summary>
Expand Down Expand Up @@ -285,9 +286,14 @@ protected static string FindDriverServiceExecutable(string executableName, Uri d
/// <param name="disposing"><see langword="true"/> if the Dispose method was explicitly called; otherwise, <see langword="false"/>.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
if (!this.isDisposed)
{
this.Stop();
if (disposing)
{
this.Stop();
}

this.isDisposed = true;
}
}

Expand Down
32 changes: 30 additions & 2 deletions dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs
@@ -1,4 +1,4 @@
// <copyright file="DriverServiceCommandExecutor.cs" company="WebDriver Committers">
// <copyright file="DriverServiceCommandExecutor.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand Down Expand Up @@ -27,6 +27,7 @@ internal class DriverServiceCommandExecutor : ICommandExecutor
{
private DriverService service;
private HttpCommandExecutor internalExecutor;
private bool isDisposed;

/// <summary>
/// Initializes a new instance of the <see cref="DriverServiceCommandExecutor"/> class.
Expand Down Expand Up @@ -87,11 +88,38 @@ public Response Execute(Command commandToExecute)
{
if (commandToExecute.Name == DriverCommand.Quit)
{
this.service.Dispose();
this.Dispose();
}
}

return toReturn;
}

/// <summary>
/// Releases all resources used by the <see cref="DriverServiceCommandExecutor"/>.
/// </summary>
public void Dispose()
{
this.Dispose(true);
}

/// <summary>
/// Releases the unmanaged resources used by the <see cref="HttpCommandExecutor"/> and
/// optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release managed and resources;
/// <see langword="false"/> to only release unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
this.service.Dispose();
}

this.isDisposed = true;
}
}
}
}
23 changes: 23 additions & 0 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Expand Up @@ -40,6 +40,7 @@ public class HttpCommandExecutor : ICommandExecutor
private Uri remoteServerUri;
private TimeSpan serverResponseTimeout;
private bool enableKeepAlive;
private bool isDisposed;
private CommandInfoRepository commandInfoRepository = new WebDriverWireProtocolCommandInfoRepository();

/// <summary>
Expand Down Expand Up @@ -292,6 +293,28 @@ private Response CreateResponse(HttpResponseInfo stuff)
return commandResponse;
}

/// <summary>
/// Releases all resources used by the <see cref="HttpCommandExecutor"/>.
/// </summary>
public void Dispose()
{
this.Dispose(true);
}

/// <summary>
/// Releases the unmanaged resources used by the <see cref="HttpCommandExecutor"/> and
/// optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release managed and resources;
/// <see langword="false"/> to only release unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
this.isDisposed = true;
}
}

private class HttpRequestInfo
{
public HttpRequestInfo(Uri serverUri, Command commandToExecute, CommandInfo commandInfo)
Expand Down
6 changes: 4 additions & 2 deletions dotnet/src/webdriver/Remote/ICommandExecutor.cs
@@ -1,4 +1,4 @@
// <copyright file="ICommandExecutor.cs" company="WebDriver Committers">
// <copyright file="ICommandExecutor.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
Expand All @@ -16,12 +16,14 @@
// limitations under the License.
// </copyright>

using System;

namespace OpenQA.Selenium.Remote
{
/// <summary>
/// Provides a way to send commands to the remote server
/// </summary>
public interface ICommandExecutor
public interface ICommandExecutor : IDisposable
{
/// <summary>
/// Gets the repository of objects containin information about commands.
Expand Down
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Expand Up @@ -1091,6 +1091,11 @@ protected virtual void Dispose(bool disposing)
}
finally
{
if (this.CommandExecutor != null)
{
this.CommandExecutor.Dispose();
}

this.StopClient();
this.sessionId = null;
}
Expand Down

0 comments on commit b337a82

Please sign in to comment.