From 66ce713f38abf3dafcc3ac50016ec43f53ba0a39 Mon Sep 17 00:00:00 2001 From: FiniteReality Date: Mon, 22 Oct 2018 21:04:46 +0100 Subject: [PATCH] Finish implementing IDisposable where appropriate I opted to use NoWarn in the Tests project as it wasn't really necessary considering that our tests only run once --- .../DiscordSocketClient.cs | 1 + .../Entities/Guilds/SocketGuild.cs | 20 +++++++++++++++++-- .../Discord.Net.Tests.csproj | 1 + .../Discord.Net.Tests/Net/CachedRestClient.cs | 11 ++++++---- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 531e6a730..6b720645e 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -715,6 +715,7 @@ namespace Discord.WebSocket { await GuildUnavailableAsync(guild).ConfigureAwait(false); await TimedInvokeAsync(_leftGuildEvent, nameof(LeftGuild), guild).ConfigureAwait(false); + (guild as IDisposable).Dispose(); } else { diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index 412f3acff..137923003 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -25,8 +25,9 @@ namespace Discord.WebSocket /// Represents a WebSocket-based guild object. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] - public class SocketGuild : SocketEntity, IGuild + public class SocketGuild : SocketEntity, IGuild, IDisposable { +#pragma warning disable IDISP002, IDISP006 private readonly SemaphoreSlim _audioLock; private TaskCompletionSource _syncPromise, _downloaderPromise; private TaskCompletionSource _audioConnectPromise; @@ -37,6 +38,7 @@ namespace Discord.WebSocket private ImmutableArray _emotes; private ImmutableArray _features; private AudioClient _audioClient; +#pragma warning restore IDISP002, IDISP006 /// public string Name { get; private set; } @@ -63,7 +65,7 @@ namespace Discord.WebSocket /// number here is the most accurate in terms of counting the number of users within this guild. /// /// - /// Use this instead of enumerating the count of the + /// Use this instead of enumerating the count of the /// collection, as you may see discrepancy /// between that and this property. /// @@ -871,9 +873,11 @@ namespace Discord.WebSocket if (external) { +#pragma warning disable IDISP001 var _ = promise.TrySetResultAsync(null); await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); return null; +#pragma warning restore IDISP001 } if (_audioClient == null) @@ -896,10 +900,14 @@ namespace Discord.WebSocket }; audioClient.Connected += () => { +#pragma warning disable IDISP001 var _ = promise.TrySetResultAsync(_audioClient); +#pragma warning restore IDISP001 return Task.Delay(0); }; +#pragma warning disable IDISP003 _audioClient = audioClient; +#pragma warning restore IDISP003 } await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); @@ -947,6 +955,7 @@ namespace Discord.WebSocket if (_audioClient != null) await _audioClient.StopAsync().ConfigureAwait(false); await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false); + _audioClient?.Dispose(); _audioClient = null; } internal async Task FinishConnectAudio(string url, string token) @@ -1129,5 +1138,12 @@ namespace Discord.WebSocket /// async Task> IGuild.GetWebhooksAsync(RequestOptions options) => await GetWebhooksAsync(options).ConfigureAwait(false); + + void IDisposable.Dispose() + { + DisconnectAudioAsync().GetAwaiter().GetResult(); + _audioLock?.Dispose(); + _audioClient?.Dispose(); + } } } diff --git a/test/Discord.Net.Tests/Discord.Net.Tests.csproj b/test/Discord.Net.Tests/Discord.Net.Tests.csproj index 0ee6f7e59..aa6f86a34 100644 --- a/test/Discord.Net.Tests/Discord.Net.Tests.csproj +++ b/test/Discord.Net.Tests/Discord.Net.Tests.csproj @@ -5,6 +5,7 @@ netcoreapp1.1 portable $(PackageTargetFallback);portable-net45+win8+wp8+wpa81 + IDISP001,IDISP002,IDISP004,IDISP005 diff --git a/test/Discord.Net.Tests/Net/CachedRestClient.cs b/test/Discord.Net.Tests/Net/CachedRestClient.cs index 4bc8a386a..c465eaa01 100644 --- a/test/Discord.Net.Tests/Net/CachedRestClient.cs +++ b/test/Discord.Net.Tests/Net/CachedRestClient.cs @@ -43,7 +43,10 @@ namespace Discord.Net if (!_isDisposed) { if (disposing) + { _blobCache.Dispose(); + _cancelTokenSource?.Dispose(); + } _isDisposed = true; } } @@ -70,7 +73,7 @@ namespace Discord.Net { if (method != "GET") throw new InvalidOperationException("This RestClient only supports GET requests."); - + string uri = Path.Combine(_baseUrl, endpoint); var bytes = await _blobCache.DownloadUrl(uri, _headers); return new RestResponse(HttpStatusCode.OK, _headers, new MemoryStream(bytes)); @@ -84,7 +87,7 @@ namespace Discord.Net throw new InvalidOperationException("This RestClient does not support multipart requests."); } - public async Task ClearAsync() + public async Task ClearAsync() { await _blobCache.InvalidateAll(); } @@ -93,7 +96,7 @@ namespace Discord.Net { if (Info != null) return; - + bool needsReset = false; try { @@ -117,4 +120,4 @@ namespace Discord.Net await _blobCache.InsertObject("info", Info); } } -} \ No newline at end of file +}