Skip to content

[dotnet] [bidi] Encapsulate transport inside Broker #15423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions dotnet/src/webdriver/BiDi/BiDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
using System;
using System.Threading.Tasks;
using OpenQA.Selenium.BiDi.Communication;
using OpenQA.Selenium.BiDi.Communication.Transport;

namespace OpenQA.Selenium.BiDi;

public class BiDi : IAsyncDisposable
{
private readonly ITransport _transport;
private readonly Broker _broker;

private readonly Lazy<Modules.Session.SessionModule> _sessionModule;
Expand All @@ -42,8 +40,7 @@ internal BiDi(string url)
{
var uri = new Uri(url);

_transport = new WebSocketTransport(new Uri(url));
_broker = new Broker(this, _transport);
_broker = new Broker(this, uri);

_sessionModule = new Lazy<Modules.Session.SessionModule>(() => new Modules.Session.SessionModule(_broker));
_browsingContextModule = new Lazy<Modules.BrowsingContext.BrowsingContextModule>(() => new Modules.BrowsingContext.BrowsingContextModule(_broker));
Expand Down Expand Up @@ -83,10 +80,14 @@ public Task EndAsync(Modules.Session.EndOptions? options = null)
return SessionModule.EndAsync(options);
}

public async ValueTask DisposeAsync()
public virtual async ValueTask DisposeAsyncCore()
{
await _broker.DisposeAsync().ConfigureAwait(false);
}

_transport?.Dispose();
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore();
GC.SuppressFinalize(this);
}
}
14 changes: 11 additions & 3 deletions dotnet/src/webdriver/BiDi/Communication/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public class Broker : IAsyncDisposable

private readonly BiDiJsonSerializerContext _jsonSerializerContext;

internal Broker(BiDi bidi, ITransport transport)
internal Broker(BiDi bidi, Uri url)
{
_bidi = bidi;
_transport = transport;
_transport = new WebSocketTransport(url);

var jsonSerializerOptions = new JsonSerializerOptions
{
Expand Down Expand Up @@ -298,7 +298,7 @@ public async Task UnsubscribeAsync(Modules.Session.Subscription subscription, Ev
}
}

public async ValueTask DisposeAsync()
public virtual async ValueTask DisposeAsyncCore()
{
_pendingEvents.CompleteAdding();

Expand All @@ -308,5 +308,13 @@ public async ValueTask DisposeAsync()
{
await _eventEmitterTask.ConfigureAwait(false);
}

_transport.Dispose();
}

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore();
GC.SuppressFinalize(this);
}
}
Loading