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 TimedInvokeAsync(_leftGuildEvent, nameof(LeftGuild), guild).ConfigureAwait(false);
(guild as IDisposable).Dispose();
}
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.
/// </summary>
[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 TaskCompletionSource<bool> _syncPromise, _downloaderPromise;
private TaskCompletionSource<AudioClient> _audioConnectPromise;
@@ -37,6 +38,7 @@ namespace Discord.WebSocket
private ImmutableArray<GuildEmote> _emotes;
private ImmutableArray<string> _features;
private AudioClient _audioClient;
#pragma warning restore IDISP002, IDISP006

/// <inheritdoc />
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.
/// </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
/// between that and this property.
/// </para>
@@ -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
/// <inheritdoc />
async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
=> 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>
<DebugType>portable</DebugType>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback>
<NoWarn>IDISP001,IDISP002,IDISP004,IDISP005</NoWarn>
</PropertyGroup>
<ItemGroup>
<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 (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<CacheInfo>("info", Info);
}
}
}
}

Loading…
Cancel
Save