-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protocol: add MatchActionException helper class. (#223)
This helps users identify the Observer/Connection disposed cases.
- Loading branch information
Showing
4 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Tmds.DBus.Protocol; | ||
|
||
public static class MatchActionException | ||
{ | ||
// Exception used when the IDisposable returned by AddMatchAsync gets disposed. | ||
public static bool IsObserverDisposed(Exception exception) | ||
=> object.ReferenceEquals(exception, DBusConnection.ObserverDisposedException); | ||
|
||
// Exception used when the Connection gets disposed. | ||
public static bool IsConnectionDisposed(Exception exception) | ||
// note: Connection.DisposedException is only ever used as an InnerException of DisconnectedException, | ||
// so we directly check for that. | ||
=> object.ReferenceEquals(exception?.InnerException, Connection.DisposedException); | ||
|
||
public static bool IsDisposed(Exception exception) | ||
=> IsObserverDisposed(exception) || IsConnectionDisposed(exception); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Tmds.DBus.Protocol.Tests | ||
{ | ||
public class ExceptionTests | ||
{ | ||
[Fact] | ||
public async Task ObserverDisposed() | ||
{ | ||
var connections = PairedConnection.CreatePair(); | ||
using var conn1 = connections.Item1; | ||
using var conn2 = connections.Item2; | ||
|
||
TaskCompletionSource<Exception> tcs = new(); | ||
|
||
var disposable = await conn1.AddMatchAsync( | ||
new MatchRule(), (Message message, object? state) => "", (Exception? ex, string s, object? s1, object? s2) => | ||
{ | ||
tcs.SetResult(ex!); | ||
}); | ||
|
||
disposable.Dispose(); | ||
|
||
Exception ex = await tcs.Task; | ||
Assert.True(MatchActionException.IsObserverDisposed(ex)); | ||
Assert.True(MatchActionException.IsDisposed(ex)); | ||
} | ||
|
||
[Fact] | ||
public async Task ConnectionDisposed() | ||
{ | ||
var connections = PairedConnection.CreatePair(); | ||
using var conn1 = connections.Item1; | ||
using var conn2 = connections.Item2; | ||
|
||
TaskCompletionSource<Exception> tcs = new(); | ||
|
||
var disposable = await conn1.AddMatchAsync( | ||
new MatchRule(), (Message message, object? state) => "", (Exception? ex, string s, object? s1, object? s2) => | ||
{ | ||
tcs.SetResult(ex!); | ||
}); | ||
|
||
conn1.Dispose(); | ||
|
||
Exception ex = await tcs.Task; | ||
Assert.True(MatchActionException.IsConnectionDisposed(ex)); | ||
Assert.True(MatchActionException.IsDisposed(ex)); | ||
} | ||
} | ||
} |