Skip to content

Commit

Permalink
handle disposed cancellation token (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Mar 15, 2024
1 parent f3f64ea commit e3072df
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var settings = new JsonSerializerSettings
DefaultValueHandling = DefaultValueHandling.Ignore
};
```
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L81-L90' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L82-L91' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters</NoWarn>
<Version>23.5.0</Version>
<Version>23.5.1</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"IsCancellationRequested": true,
"Token": true,
"WaitHandle": {
"SafeWaitHandle": {
"IsInvalid": false,
"IsClosed": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"IsCancellationRequested": true,
"Token": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
IsCancellationRequested: true,
Token: true,
WaitHandle: {
SafeWaitHandle: {
IsInvalid: false,
IsClosed: false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
IsCancellationRequested: true,
Token: true
}
18 changes: 18 additions & 0 deletions src/Verify.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3894,4 +3894,22 @@ public class Child
{
public Parent Parent { get; set; }
}

[Fact]
public async Task CancellationToken_Cancelled()
{
var cancelSource = new CancelSource();
cancelSource.Cancel();
await Verify(cancelSource.Token);
}

[Fact]
public async Task CancellationToken_CancelledDisposed()
{
var cancelSource = new CancelSource();
cancelSource.Cancel();
var token = cancelSource.Token;
cancelSource.Dispose();
await Verify(token);
}
}
20 changes: 20 additions & 0 deletions src/Verify/Serialization/Converters/CancelConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class CancelConverter :
WriteOnlyJsonConverter<Cancel>
{
public override void Write(VerifyJsonWriter writer, Cancel value)
{
writer.WriteStartObject();

writer.WriteMember(value, value.IsCancellationRequested, "IsCancellationRequested");
writer.WriteMember(value, value.CanBeCanceled, "Token");
try
{
writer.WriteMember(value, value.WaitHandle, "WaitHandle");
}
catch (ObjectDisposedException)
{
}

writer.WriteEndObject();
}
}
2 changes: 2 additions & 0 deletions src/Verify/Serialization/SerializationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ partial class SerializationSettings
static MethodInfoConverter methodInfoConverter = new();
static FieldInfoConverter fieldInfoConverter = new();
static ConstructorInfoConverter constructorInfoConverter = new();
static CancelConverter cancelConverter = new();
static ParameterInfoConverter parameterInfoConverter = new();
static PropertyInfoConverter propertyInfoConverter = new();
static ClaimConverter claimConverter = new();
Expand Down Expand Up @@ -108,6 +109,7 @@ JsonSerializerSettings BuildSettings()
converters.Add(methodInfoConverter);
converters.Add(fieldInfoConverter);
converters.Add(constructorInfoConverter);
converters.Add(cancelConverter);
converters.Add(propertyInfoConverter);
converters.Add(parameterInfoConverter);
converters.Add(claimConverter);
Expand Down

0 comments on commit e3072df

Please sign in to comment.