Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Token cleanup isn't performing very well with large amounts of data #7

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 1 deletion src/TokenCleanup/TokenCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public async Task RemoveExpiredGrantsAsync()
// TODO: Device Flow cleanup
var expired = context.PersistedGrants
.Where(x => x.Expiration < DateTime.UtcNow)
.OrderBy(x => x.Key)
.Take(_options.TokenCleanupBatchSize)
.ToArray();

Expand Down
14 changes: 14 additions & 0 deletions test/IntegrationTests/FakeOperationalStoreNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using IdentityServer4.EntityFramework.Entities;

namespace IdentityServer4.EntityFramework.IntegrationTests
{
public class FakeOperationalStoreNotification : IOperationalStoreNotification
{
public Task PersistedGrantsRemovedAsync(IEnumerable<PersistedGrant> persistedGrants)
{
return Task.CompletedTask;
}
}
}
79 changes: 79 additions & 0 deletions test/IntegrationTests/TokenCleanup/TokenCleanupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Entities;
using IdentityServer4.EntityFramework.Interfaces;
using IdentityServer4.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace IdentityServer4.EntityFramework.IntegrationTests.TokenCleanup
{
public class TokenCleanupTests : IntegrationTest<TokenCleanupTests, PersistedGrantDbContext, OperationalStoreOptions>
{
private readonly DateTime _now;

public TokenCleanupTests(DatabaseProviderFixture<PersistedGrantDbContext> fixture) : base(fixture)
{
_now = DateTime.UtcNow;
foreach (var options in TestDatabaseProviders.SelectMany(x => x.Select(y => (DbContextOptions<PersistedGrantDbContext>)y)).ToList())
{
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
context.Database.EnsureCreated();
}
}
}

[Theory, MemberData(nameof(TestDatabaseProviders))]
public async Task RemoveExpiredGrantsAsync_ExpiredTokensCleanedUp(DbContextOptions<PersistedGrantDbContext> options)
{
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
var grants = new PersistedGrant[100];
for (var i = 0; i < grants.Length; i++)
{
grants[i] = new PersistedGrant
{
Key = Guid.NewGuid().ToString("N"),
Type = i % 4 == 0 ? "authorization_code" : "refresh_token",
SubjectId = i.ToString(),
ClientId = "test-client",
CreationTime = _now.AddDays(-60),
// 34 tokens should be cleaned up.
Expiration = _now.AddDays(i % 3 == 0 ? -1 : 1),
Data = "grant data"
};
}
context.PersistedGrants.AddRange(grants);
await context.SaveChangesAsync();
}

var customStoreOptions = new OperationalStoreOptions
{
TokenCleanupBatchSize = 5
};
using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IPersistedGrantDbContext>(context);
serviceCollection.AddSingleton<IOperationalStoreNotification, FakeOperationalStoreNotification>();

var cleanup = new EntityFramework.TokenCleanup(
serviceCollection.BuildServiceProvider(),
new FakeLogger<EntityFramework.TokenCleanup>(),
customStoreOptions);

await cleanup.RemoveExpiredGrantsAsync();
}

using (var context = new PersistedGrantDbContext(options, StoreOptions))
{
var grants = await context.PersistedGrants.ToListAsync();
Assert.Equal(100 - 34, grants.Count);
}
}
}
}