Browse Source

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
pull/1171/head
FiniteReality 6 years ago
parent
commit
66ce713f38
4 changed files with 27 additions and 6 deletions
  1. +1
    -0
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  2. +18
    -2
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
  3. +1
    -0
      test/Discord.Net.Tests/Discord.Net.Tests.csproj
  4. +7
    -4
      test/Discord.Net.Tests/Net/CachedRestClient.cs

+ 1
- 0
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -715,6 +715,7 @@ namespace Discord.WebSocket
{ {
await GuildUnavailableAsync(guild).ConfigureAwait(false); await GuildUnavailableAsync(guild).ConfigureAwait(false);
await TimedInvokeAsync(_leftGuildEvent, nameof(LeftGuild), guild).ConfigureAwait(false); await TimedInvokeAsync(_leftGuildEvent, nameof(LeftGuild), guild).ConfigureAwait(false);
(guild as IDisposable).Dispose();
} }
else else
{ {


+ 18
- 2
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -25,8 +25,9 @@ namespace Discord.WebSocket
/// Represents a WebSocket-based guild object. /// Represents a WebSocket-based guild object.
/// </summary> /// </summary>
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class SocketGuild : SocketEntity<ulong>, IGuild
public class SocketGuild : SocketEntity<ulong>, IGuild, IDisposable
{ {
#pragma warning disable IDISP002, IDISP006
private readonly SemaphoreSlim _audioLock; private readonly SemaphoreSlim _audioLock;
private TaskCompletionSource<bool> _syncPromise, _downloaderPromise; private TaskCompletionSource<bool> _syncPromise, _downloaderPromise;
private TaskCompletionSource<AudioClient> _audioConnectPromise; private TaskCompletionSource<AudioClient> _audioConnectPromise;
@@ -37,6 +38,7 @@ namespace Discord.WebSocket
private ImmutableArray<GuildEmote> _emotes; private ImmutableArray<GuildEmote> _emotes;
private ImmutableArray<string> _features; private ImmutableArray<string> _features;
private AudioClient _audioClient; private AudioClient _audioClient;
#pragma warning restore IDISP002, IDISP006


/// <inheritdoc /> /// <inheritdoc />
public string Name { get; private set; } 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. /// number here is the most accurate in terms of counting the number of users within this guild.
/// </para> /// </para>
/// <para> /// <para>
/// Use this instead of enumerating the count of the
/// Use this instead of enumerating the count of the
/// <see cref="Discord.WebSocket.SocketGuild.Users" /> collection, as you may see discrepancy /// <see cref="Discord.WebSocket.SocketGuild.Users" /> collection, as you may see discrepancy
/// between that and this property. /// between that and this property.
/// </para> /// </para>
@@ -871,9 +873,11 @@ namespace Discord.WebSocket


if (external) if (external)
{ {
#pragma warning disable IDISP001
var _ = promise.TrySetResultAsync(null); var _ = promise.TrySetResultAsync(null);
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
return null; return null;
#pragma warning restore IDISP001
} }


if (_audioClient == null) if (_audioClient == null)
@@ -896,10 +900,14 @@ namespace Discord.WebSocket
}; };
audioClient.Connected += () => audioClient.Connected += () =>
{ {
#pragma warning disable IDISP001
var _ = promise.TrySetResultAsync(_audioClient); var _ = promise.TrySetResultAsync(_audioClient);
#pragma warning restore IDISP001
return Task.Delay(0); return Task.Delay(0);
}; };
#pragma warning disable IDISP003
_audioClient = audioClient; _audioClient = audioClient;
#pragma warning restore IDISP003
} }


await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false); await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
@@ -947,6 +955,7 @@ namespace Discord.WebSocket
if (_audioClient != null) if (_audioClient != null)
await _audioClient.StopAsync().ConfigureAwait(false); await _audioClient.StopAsync().ConfigureAwait(false);
await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false); await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false);
_audioClient?.Dispose();
_audioClient = null; _audioClient = null;
} }
internal async Task FinishConnectAudio(string url, string token) internal async Task FinishConnectAudio(string url, string token)
@@ -1129,5 +1138,12 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options) async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options).ConfigureAwait(false); => await GetWebhooksAsync(options).ConfigureAwait(false);

void IDisposable.Dispose()
{
DisconnectAudioAsync().GetAwaiter().GetResult();
_audioLock?.Dispose();
_audioClient?.Dispose();
}
} }
} }

+ 1
- 0
test/Discord.Net.Tests/Discord.Net.Tests.csproj View File

@@ -5,6 +5,7 @@
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp1.1</TargetFramework>
<DebugType>portable</DebugType> <DebugType>portable</DebugType>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback> <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback>
<NoWarn>IDISP001,IDISP002,IDISP004,IDISP005</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Include="xunit.runner.json"> <Content Include="xunit.runner.json">


+ 7
- 4
test/Discord.Net.Tests/Net/CachedRestClient.cs View File

@@ -43,7 +43,10 @@ namespace Discord.Net
if (!_isDisposed) if (!_isDisposed)
{ {
if (disposing) if (disposing)
{
_blobCache.Dispose(); _blobCache.Dispose();
_cancelTokenSource?.Dispose();
}
_isDisposed = true; _isDisposed = true;
} }
} }
@@ -70,7 +73,7 @@ namespace Discord.Net
{ {
if (method != "GET") if (method != "GET")
throw new InvalidOperationException("This RestClient only supports GET requests."); throw new InvalidOperationException("This RestClient only supports GET requests.");
string uri = Path.Combine(_baseUrl, endpoint); string uri = Path.Combine(_baseUrl, endpoint);
var bytes = await _blobCache.DownloadUrl(uri, _headers); var bytes = await _blobCache.DownloadUrl(uri, _headers);
return new RestResponse(HttpStatusCode.OK, _headers, new MemoryStream(bytes)); 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."); throw new InvalidOperationException("This RestClient does not support multipart requests.");
} }


public async Task ClearAsync()
public async Task ClearAsync()
{ {
await _blobCache.InvalidateAll(); await _blobCache.InvalidateAll();
} }
@@ -93,7 +96,7 @@ namespace Discord.Net
{ {
if (Info != null) if (Info != null)
return; return;
bool needsReset = false; bool needsReset = false;
try try
{ {
@@ -117,4 +120,4 @@ namespace Discord.Net
await _blobCache.InsertObject<CacheInfo>("info", Info); await _blobCache.InsertObject<CacheInfo>("info", Info);
} }
} }
}
}

Loading…
Cancel
Save