Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0adf1a2
dispose of client should only flush
SimonCropp Nov 30, 2021
d734a6b
.
SimonCropp Nov 30, 2021
4298934
Update ApiApprovalTests.Run.Core3_1.verified.txt
SimonCropp Nov 30, 2021
20e56b9
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 2, 2021
d7c8ce0
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 2, 2021
424f7e9
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 5, 2021
4a1120f
Update test/Sentry.Tests/SentryClientTests.cs
SimonCropp Dec 8, 2021
07e9898
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 8, 2021
d572445
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 14, 2021
f836955
Update CHANGELOG.md
SimonCropp Dec 14, 2021
06154c2
Merge branch 'dispose-of-client-should-only-flush' of https://github.…
SimonCropp Dec 14, 2021
3d75150
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 14, 2021
6730109
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Dec 22, 2021
46798f2
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Jan 4, 2022
b02e240
Update Hub.cs
SimonCropp Jan 4, 2022
b098685
Update HubTests.cs
SimonCropp Jan 4, 2022
7854e30
Update Hub.cs
SimonCropp Jan 4, 2022
dfe9c0d
Update Hub.cs
SimonCropp Jan 4, 2022
54b15b1
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Jan 5, 2022
d040f2a
.
SimonCropp Jan 5, 2022
a27fcec
Update CHANGELOG.md
SimonCropp Jan 5, 2022
3c3968b
Merge branch 'main' into dispose-of-client-should-only-flush
SimonCropp Jan 5, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

Dispose of client should only flush ([#1354](https://github.com/getsentry/sentry-dotnet/pull/1354))
### Fixes

- Operation cancel while flushing cache no longer logs an errors ([#1352](https://github.com/getsentry/sentry-dotnet/pull/1352))
Expand Down
33 changes: 3 additions & 30 deletions src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Sentry
/// <inheritdoc cref="IDisposable" />
public class SentryClient : ISentryClient, IDisposable
{
private volatile bool _disposed;
private readonly SentryOptions _options;

// Internal for testing.
Expand Down Expand Up @@ -62,11 +61,6 @@ internal SentryClient(
/// <inheritdoc />
public SentryId CaptureEvent(SentryEvent? @event, Scope? scope = null)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(SentryClient));
}

if (@event == null)
{
return SentryId.Empty;
Expand All @@ -86,11 +80,6 @@ public SentryId CaptureEvent(SentryEvent? @event, Scope? scope = null)
/// <inheritdoc />
public void CaptureUserFeedback(UserFeedback userFeedback)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(SentryClient));
}

if (userFeedback.EventId.Equals(SentryId.Empty))
{
// Ignore the user feedback if EventId is empty
Expand All @@ -104,11 +93,6 @@ public void CaptureUserFeedback(UserFeedback userFeedback)
/// <inheritdoc />
public void CaptureTransaction(Transaction transaction)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(SentryClient));
}

if (transaction.SpanId.Equals(SpanId.Empty))
{
_options.LogWarning(
Expand Down Expand Up @@ -156,11 +140,6 @@ public void CaptureTransaction(Transaction transaction)
/// <inheritdoc />
public void CaptureSession(SessionUpdate sessionUpdate)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(SentryClient));
}

CaptureEnvelope(Envelope.FromSession(sessionUpdate));
}

Expand Down Expand Up @@ -299,19 +278,13 @@ private bool CaptureEnvelope(Envelope envelope)
/// Disposes this client
/// </summary>
/// <inheritdoc />
[Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in version 4.")]
public void Dispose()
{
_options.LogDebug("Disposing SentryClient.");

if (_disposed)
{
return;
}

_disposed = true;
_options.LogDebug("Flushing SentryClient.");

// Worker should empty it's queue until SentryOptions.ShutdownTimeout
(Worker as IDisposable)?.Dispose();
Worker.FlushAsync(_options.ShutdownTimeout).GetAwaiter().GetResult();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Core2_1.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Core3_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ namespace Sentry
public void CaptureSession(Sentry.SessionUpdate sessionUpdate) { }
public void CaptureTransaction(Sentry.Transaction transaction) { }
public void CaptureUserFeedback(Sentry.UserFeedback userFeedback) { }
[System.Obsolete("Sentry client should no be explicitly disposed of. This method will be removed in" +
" version 4.")]
public void Dispose() { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
}
Expand Down
54 changes: 36 additions & 18 deletions test/Sentry.Tests/SentryClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
#pragma warning disable CS0618

namespace Sentry.Tests;

Expand Down Expand Up @@ -372,11 +373,12 @@ public void CaptureEvent_Release_SetFromOptions()
}

[Fact]
public void CaptureEvent_DisposedClient_ThrowsObjectDisposedException()
public void CaptureEvent_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
_ = Assert.Throws<ObjectDisposedException>(() => sut.CaptureEvent(null));
var @event = new SentryEvent();
sut.CaptureEvent(@event);
}

[Fact]
Expand Down Expand Up @@ -419,13 +421,28 @@ public void CaptureUserFeedback_EventIdEmpty_FeedbackIgnored()
//Assert
_ = sut.Worker.DidNotReceive().EnqueueEnvelope(Arg.Any<Envelope>());
}
[Fact]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Fact]
[Fact]

public void Dispose_should_only_flush()
{
// Arrange
var client = new SentryClient(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret,
});

// Act
client.Dispose();

//Assert is still usable
client.CaptureEvent(new SentryEvent { Message = "Test" });
}

[Fact]
public void CaptureUserFeedback_DisposedClient_ThrowsObjectDisposedException()
public void CaptureUserFeedback_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
_ = Assert.Throws<ObjectDisposedException>(() => sut.CaptureUserFeedback(null));
sut.CaptureUserFeedback(new UserFeedback(SentryId.Empty, "name", "email", "comment"));
}

[Fact]
Expand Down Expand Up @@ -557,34 +574,35 @@ public void CaptureTransaction_NotFinished_Sent()
}

[Fact]
public void CaptureTransaction_DisposedClient_ThrowsObjectDisposedException()
public void CaptureTransaction_DisposedClient_DoesNotThrow()
{
var sut = _fixture.GetSut();
sut.Dispose();
_ = Assert.Throws<ObjectDisposedException>(() => sut.CaptureTransaction(null));
sut.CaptureTransaction(
new Transaction(
"test name",
"test operation")
{
IsSampled = true,
EndTimestamp = null // not finished
});
}

[Fact]
public void Dispose_Worker_DisposeCalled()
public void Dispose_Worker_FlushCalled()
{
_fixture.GetSut().Dispose();
(_fixture.BackgroundWorker as IDisposable)?.Received(1).Dispose();
var client = _fixture.GetSut();
client.Dispose();
_fixture.BackgroundWorker?.Received(1).FlushAsync(_fixture.SentryOptions.ShutdownTimeout);
}

[Fact]
public void Dispose_MultipleCalls_WorkerDisposedOnce()
public void Dispose_MultipleCalls_WorkerFlushedTwice()
{
var sut = _fixture.GetSut();
sut.Dispose();
sut.Dispose();
(_fixture.BackgroundWorker as IDisposable).Received(1).Dispose();
}

[Fact]
public void Dispose_WorkerDoesNotImplementDispose_DoesntThrow()
{
_fixture.BackgroundWorker = Substitute.For<IBackgroundWorker>();
_fixture.GetSut().Dispose();
_fixture.BackgroundWorker?.Received(2).FlushAsync(_fixture.SentryOptions.ShutdownTimeout);
}

[Fact]
Expand Down