Skip to content
Merged
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
47 changes: 5 additions & 42 deletions NGitLab.Mock/Clients/ClientContext.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using NeoSmart.AsyncLock;

namespace NGitLab.Mock.Clients;

internal sealed class ClientContext(GitLabServer server, User user)
{
private readonly SemaphoreSlim _operationLock = new(1, 1);
private readonly AsyncLock _operationLock = new();

public GitLabServer Server { get; } = server;

public User User { get; } = user;

public bool IsAuthenticated => User != null;
public bool IsAuthenticated => User is not null;

public IDisposable BeginOperationScope(
[CallerMemberName] string callingMethod = null,
[CallerFilePath] string callingFilePath = null,
[CallerLineNumber] int callingLineNumber = -1)
public IDisposable BeginOperationScope()
{
Server.RaiseOnClientOperation();
var releaser = new Releaser(_operationLock);

// Store caller info for debugging purposes
Releaser.MethodWhereLockWasTaken = callingMethod;
Releaser.FilePathWhereLockWasTaken = callingFilePath;
Releaser.LineNumberWhereLockWasTaken = callingLineNumber;

return releaser;
}

private sealed class Releaser : IDisposable
{
private readonly SemaphoreSlim _operationLock;

public Releaser(SemaphoreSlim operationLock)
{
_operationLock = operationLock;
if (_operationLock.CurrentCount == 0 && Debugger.IsAttached)
Debugger.Break();
_operationLock.Wait();
}

// The following is for debugging purposes only. It stores info about where the active lock was taken.
public static string MethodWhereLockWasTaken { get; set; }

public static string FilePathWhereLockWasTaken { get; set; }

public static int LineNumberWhereLockWasTaken { get; set; }

public void Dispose()
{
_operationLock.Release();
}
return _operationLock.Lock();
}
}
1 change: 1 addition & 0 deletions NGitLab.Mock/NGitLab.Mock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="YamlDotNet" Version="16.3.0" />
</ItemGroup>

Expand Down
Loading