backend: Add integration tests
This commit is contained in:
1 parent
fd37986f5e
commit
def23d306e
9 files changed
+526
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using Helpdesk.Domain.Auth;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
namespace Helpdesk.IntegrationTests;
|
||||
|
||||
internal sealed class TokenSaveGate : SaveChangesInterceptor
|
||||
{
|
||||
private Pause? next;
|
||||
|
||||
internal Pause PauseNextSave()
|
||||
{
|
||||
var pause = new Pause();
|
||||
if (Interlocked.CompareExchange(ref next, pause, null) is not null)
|
||||
throw new InvalidOperationException("A save is already scheduled to pause.");
|
||||
return pause;
|
||||
}
|
||||
|
||||
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(
|
||||
DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (Volatile.Read(ref next) is not null &&
|
||||
eventData.Context!.ChangeTracker.Entries<AuthToken>()
|
||||
.Any(entry => entry.State == EntityState.Added && entry.Entity.TokenType == TokenType.Refresh) &&
|
||||
Interlocked.Exchange(ref next, null) is { } pause)
|
||||
{
|
||||
pause.Entered.TrySetResult();
|
||||
await pause.Released.Task.WaitAsync(cancellationToken);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal sealed class Pause
|
||||
{
|
||||
internal TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
internal TaskCompletionSource Released { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
internal void Release() => Released.TrySetResult();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user