|
1 | 1 | using System; |
2 | | -using System.Diagnostics; |
3 | | -using System.Runtime.CompilerServices; |
4 | | -using System.Threading; |
| 2 | +using NeoSmart.AsyncLock; |
5 | 3 |
|
6 | 4 | namespace NGitLab.Mock.Clients; |
7 | 5 |
|
8 | 6 | internal sealed class ClientContext(GitLabServer server, User user) |
9 | 7 | { |
10 | | - private readonly SemaphoreSlim _operationLock = new(1, 1); |
| 8 | + private readonly AsyncLock _operationLock = new(); |
11 | 9 |
|
12 | 10 | public GitLabServer Server { get; } = server; |
13 | 11 |
|
14 | 12 | public User User { get; } = user; |
15 | 13 |
|
16 | | - public bool IsAuthenticated => User != null; |
| 14 | + public bool IsAuthenticated => User is not null; |
17 | 15 |
|
18 | | - public IDisposable BeginOperationScope( |
19 | | - [CallerMemberName] string callingMethod = null, |
20 | | - [CallerFilePath] string callingFilePath = null, |
21 | | - [CallerLineNumber] int callingLineNumber = -1) |
| 16 | + public IDisposable BeginOperationScope() |
22 | 17 | { |
23 | 18 | Server.RaiseOnClientOperation(); |
24 | | - var releaser = new Releaser(_operationLock); |
25 | | - |
26 | | - // Store caller info for debugging purposes |
27 | | - Releaser.MethodWhereLockWasTaken = callingMethod; |
28 | | - Releaser.FilePathWhereLockWasTaken = callingFilePath; |
29 | | - Releaser.LineNumberWhereLockWasTaken = callingLineNumber; |
30 | | - |
31 | | - return releaser; |
32 | | - } |
33 | | - |
34 | | - private sealed class Releaser : IDisposable |
35 | | - { |
36 | | - private readonly SemaphoreSlim _operationLock; |
37 | | - |
38 | | - public Releaser(SemaphoreSlim operationLock) |
39 | | - { |
40 | | - _operationLock = operationLock; |
41 | | - if (_operationLock.CurrentCount == 0 && Debugger.IsAttached) |
42 | | - Debugger.Break(); |
43 | | - _operationLock.Wait(); |
44 | | - } |
45 | | - |
46 | | - // The following is for debugging purposes only. It stores info about where the active lock was taken. |
47 | | - public static string MethodWhereLockWasTaken { get; set; } |
48 | | - |
49 | | - public static string FilePathWhereLockWasTaken { get; set; } |
50 | | - |
51 | | - public static int LineNumberWhereLockWasTaken { get; set; } |
52 | | - |
53 | | - public void Dispose() |
54 | | - { |
55 | | - _operationLock.Release(); |
56 | | - } |
| 19 | + return _operationLock.Lock(); |
57 | 20 | } |
58 | 21 | } |
0 commit comments