Skip to content

Commit f251bee

Browse files
committed
Add tests and improve disconnection handling
- Added `TestingJoiningMultipleTimes` to verify application launch behavior and exception handling in `ComplicatedCasesTester.cs`. - Renamed constructor parameter from `log` to `logger` in `ConnectionChannel.cs` for clarity. - Enhanced `OnMessageReceivedAsync` to log debug messages before disconnecting the client. - Updated `DisconnectAsync` in `ChromeCastClient.cs` to set exceptions for waiting tasks on disconnection.
1 parent 77d307e commit f251bee

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Sharpcaster.Test/ComplicatedCasesTester.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ public async Task TestingWithSwitchingRunningApplication()
181181
Assert.Equal(0.8, status.Volume.Level.Value, precision: 1);
182182

183183

184+
}
185+
186+
[Fact]
187+
public async Task TestingJoiningMultipleTimes()
188+
{
189+
var TestHelper = new TestHelper();
190+
var client = await TestHelper.CreateConnectAndLoadAppClient(outputHelper, fixture);
191+
192+
193+
//This should cause issues
194+
try {
195+
await client.LaunchApplicationAsync("CC1AD845");
196+
} catch (Exception ex)
197+
{
198+
Assert.IsType<TaskCanceledException>(ex);
199+
Assert.Contains("Client disconnected before receiving response.", ex.Message);
200+
}
201+
202+
184203
}
185204
}
186205
}

Sharpcaster/Channels/ConnectionChannel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ConnectionChannel : ChromecastChannel
1515
/// <summary>
1616
/// Initializes a new instance of ConnectionChannel class
1717
/// </summary>
18-
public ConnectionChannel(ILogger? log = null) : base("tp.connection", log)
18+
public ConnectionChannel(ILogger? logger = null) : base("tp.connection", logger)
1919
{
2020
}
2121

@@ -45,8 +45,8 @@ public async override Task OnMessageReceivedAsync(string messagePayload, string
4545
{
4646
if (type == "CLOSE")
4747
{
48-
// In order to avoid usage deadlocks we need to spawn a new Task here!?
49-
_ = Task.Run(async () => await Client.DisconnectAsync().ConfigureAwait(false));
48+
Logger?.LogDebug("Connection closed by Chromecast, message: {messagePayload}", messagePayload);
49+
await Client.DisconnectAsync().ConfigureAwait(false);
5050
}
5151
await base.OnMessageReceivedAsync(messagePayload, type).ConfigureAwait(false);
5252
}

Sharpcaster/ChromeCastClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ public async Task<string> WaitResponseAsync(int messageRequestId)
331331

332332
public async Task DisconnectAsync()
333333
{
334+
foreach (var task in WaitingTasks)
335+
{
336+
task.Value?.SetException(new TaskCanceledException("Client disconnected before receiving response."));
337+
}
338+
WaitingTasks.Clear();
339+
334340
if (HeartbeatChannel != null)
335341
{
336342
HeartbeatChannel.StopTimeoutTimer();

0 commit comments

Comments
 (0)