Browse Source

Update OpenIddictMongoDbContext to dispose of the private semaphore

pull/711/head
Kévin Chalet 7 years ago
parent
commit
d7de292d93
  1. 12
      src/OpenIddict.MongoDb/OpenIddictMongoDbContext.cs
  2. 2
      src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs
  3. 21
      test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbContextTests.cs

12
src/OpenIddict.MongoDb/OpenIddictMongoDbContext.cs

@ -19,7 +19,7 @@ namespace OpenIddict.MongoDb
/// <summary>
/// Exposes the MongoDB database used by the OpenIddict stores.
/// </summary>
public class OpenIddictMongoDbContext : IOpenIddictMongoDbContext
public class OpenIddictMongoDbContext : IOpenIddictMongoDbContext, IDisposable
{
private readonly IOptionsMonitor<OpenIddictMongoDbOptions> _options;
private readonly IServiceProvider _provider;
@ -35,6 +35,11 @@ namespace OpenIddict.MongoDb
_semaphore = new SemaphoreSlim(1);
}
/// <summary>
/// Disposes the semaphore held by this instance.
/// </summary>
public void Dispose() => _semaphore.Dispose();
/// <summary>
/// Gets the <see cref="IMongoDatabase"/>.
/// </summary>
@ -49,6 +54,11 @@ namespace OpenIddict.MongoDb
return new ValueTask<IMongoDatabase>(_database);
}
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<IMongoDatabase>(Task.FromCanceled<IMongoDatabase>(cancellationToken));
}
async Task<IMongoDatabase> ExecuteAsync()
{
var options = _options.CurrentValue;

2
src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs

@ -299,6 +299,8 @@ namespace OpenIddict.MongoDb
var database = await Context.GetDatabaseAsync(cancellationToken);
var collection = database.GetCollection<TAuthorization>(Options.CurrentValue.AuthorizationsCollectionName);
// Note: Enumerable.All() is deliberately used without the extension method syntax to ensure
// ImmutableArrayExtensions.All() (which is not supported by MongoDB) is not used instead.
return ImmutableArray.CreateRange(await collection.Find(authorization =>
authorization.Subject == subject &&
authorization.ApplicationId == ObjectId.Parse(client) &&

21
test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbContextTests.cs

@ -20,6 +20,27 @@ namespace OpenIddict.MongoDb.Tests
{
public class OpenIddictMongoDbContextTests
{
[Fact]
public async Task GetDatabaseAsync_ThrowsAnExceptionForCanceledToken()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var options = Mock.Of<IOptionsMonitor<OpenIddictMongoDbOptions>>();
var token = new CancellationToken(canceled: true);
var context = new OpenIddictMongoDbContext(options, provider);
// Act and assert
var exception = await Assert.ThrowsAsync<TaskCanceledException>(async delegate
{
await context.GetDatabaseAsync(token);
});
Assert.Equal(token, exception.CancellationToken);
}
[Fact]
public async Task GetDatabaseAsync_ThrowsAnExceptionForNullOptions()
{

Loading…
Cancel
Save