Skip to content

Commit 1dc2da2

Browse files
committed
Put back lock
1 parent b687f55 commit 1dc2da2

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

NGitLab.Mock/Clients/ClientContext.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
using System;
2+
using System.Diagnostics;
3+
using System.Threading;
24

35
namespace NGitLab.Mock.Clients;
46

5-
internal sealed class ClientContext
7+
internal sealed class ClientContext(GitLabServer server, User user)
68
{
7-
public ClientContext(GitLabServer server, User user)
8-
{
9-
Server = server;
10-
User = user;
11-
}
9+
private readonly SemaphoreSlim _operationLock = new(1, 1);
1210

13-
public GitLabServer Server { get; }
11+
public GitLabServer Server { get; } = server;
1412

15-
public User User { get; }
13+
public User User { get; } = user;
1614

1715
public bool IsAuthenticated => User != null;
1816

1917
public IDisposable BeginOperationScope()
2018
{
2119
Server.RaiseOnClientOperation();
22-
return new Releaser();
20+
return new Releaser(_operationLock);
2321
}
2422

2523
private sealed class Releaser : IDisposable
2624
{
27-
public Releaser()
25+
private readonly SemaphoreSlim _operationLock;
26+
27+
public Releaser(SemaphoreSlim operationLock)
2828
{
29+
_operationLock = operationLock;
30+
if (Debugger.IsAttached && _operationLock.CurrentCount == 0)
31+
Debugger.Break();
32+
_operationLock.Wait();
2933
}
3034

3135
public void Dispose()
3236
{
37+
_operationLock.Release();
3338
}
3439
}
3540
}

NGitLab.Mock/Clients/GroupClient.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,18 @@ public Models.Group GetGroup(GroupId id)
156156
{
157157
using (Context.BeginOperationScope())
158158
{
159-
var group = Server.AllGroups.FirstOrDefault(g => id.Equals(g.PathWithNameSpace, g.Id));
159+
return GetGroupLockless(id);
160+
}
161+
}
160162

161-
if (group == null || !group.CanUserViewGroup(Context.User))
162-
throw GitLabException.NotFound();
163+
private Models.Group GetGroupLockless(GroupId id)
164+
{
165+
var group = Server.AllGroups.FirstOrDefault(g => id.Equals(g.PathWithNameSpace, g.Id));
163166

164-
return group.ToClientGroup(Context.User);
165-
}
167+
if (group == null || !group.CanUserViewGroup(Context.User))
168+
throw GitLabException.NotFound();
169+
170+
return group.ToClientGroup(Context.User);
166171
}
167172

168173
public Task<Models.Group> GetByFullPathAsync(string fullPath, CancellationToken cancellationToken = default) => GetGroupAsync(fullPath, cancellationToken);
@@ -333,7 +338,7 @@ public Models.Group Update(long id, GroupUpdate groupUpdate)
333338
{
334339
using (Context.BeginOperationScope())
335340
{
336-
var parentGroup = GetGroup(groupId);
341+
var parentGroup = GetGroupLockless(groupId);
337342
var groups = Server.AllGroups.Where(group => group.CanUserViewGroup(Context.User));
338343

339344
if (query is not null)

NGitLab.Mock/Clients/GroupHooksClient.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@ public IEnumerable<Models.GroupHook> All
2020
{
2121
using (Context.BeginOperationScope())
2222
{
23-
var hooks = GetGroup(_groupId, GroupPermission.Edit).Hooks;
24-
return ToClientGroupHooks(hooks).ToList();
23+
return GetAllLockless();
2524
}
2625
}
2726
}
2827

28+
private List<Models.GroupHook> GetAllLockless()
29+
{
30+
var hooks = GetGroup(_groupId, GroupPermission.Edit).Hooks;
31+
return ToClientGroupHooks(hooks).ToList();
32+
}
33+
2934
public Models.GroupHook this[long hookId]
3035
{
3136
get
3237
{
3338
using (Context.BeginOperationScope())
3439
{
35-
var hook = All.FirstOrDefault(h => h.Id == hookId) ?? throw GitLabException.NotFound();
40+
var hook = GetAllLockless().FirstOrDefault(h => h.Id == hookId) ?? throw GitLabException.NotFound();
3641
return hook;
3742
}
3843
}

0 commit comments

Comments
 (0)