Skip to content

Commit b687f55

Browse files
ap0llolouis-z
authored andcommitted
Remove lock mechanism from NGitLab.Mock.Clients.ClientContext
That lock mechanism was not needed and caused issues when using the ClientContext in an async context.
1 parent 55065f5 commit b687f55

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

NGitLab.Mock.Tests/FileTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using NGitLab.Models;
4+
using NUnit.Framework;
5+
6+
namespace NGitLab.Mock.Tests;
7+
8+
public class FileTests
9+
{
10+
[TestCase("README.md", true)]
11+
[TestCase("does-not-exist.md", false)]
12+
public async Task Test_get_raw_file_async(string fileToLookUp, bool expectSuccess)
13+
{
14+
// Arrange
15+
using var server = new GitLabServer();
16+
var user = server.Users.AddNew();
17+
var project = user.Namespace.Projects.AddNew(project => project.Visibility = VisibilityLevel.Internal);
18+
var initCommit = project.Repository.Commit(user, "Initial commit",
19+
[
20+
File.CreateFromText("README.md", "This is the initial content"),
21+
]);
22+
23+
var client = server.CreateClient(user);
24+
var filesClient = client.GetRepository(project.Id).Files;
25+
26+
string downloadedContent = null;
27+
28+
// Act/Assert
29+
if (expectSuccess)
30+
{
31+
await filesClient.GetRawAsync(fileToLookUp, async stream =>
32+
{
33+
using var streamReader = new StreamReader(stream);
34+
downloadedContent = await streamReader.ReadToEndAsync().ConfigureAwait(false);
35+
});
36+
Assert.That(downloadedContent, Is.EqualTo("This is the initial content"));
37+
}
38+
else
39+
{
40+
Assert.ThrowsAsync<GitLabException>(async () => await filesClient.GetRawAsync(fileToLookUp, _ => Task.CompletedTask).ConfigureAwait(false));
41+
}
42+
}
43+
}

NGitLab.Mock/Clients/ClientContext.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using System;
2-
using System.Threading;
32

43
namespace NGitLab.Mock.Clients;
54

65
internal sealed class ClientContext
76
{
8-
private readonly object _operationLock = new();
9-
107
public ClientContext(GitLabServer server, User user)
118
{
129
Server = server;
@@ -22,22 +19,17 @@ public ClientContext(GitLabServer server, User user)
2219
public IDisposable BeginOperationScope()
2320
{
2421
Server.RaiseOnClientOperation();
25-
Monitor.Enter(_operationLock);
26-
return new Releaser(_operationLock);
22+
return new Releaser();
2723
}
2824

2925
private sealed class Releaser : IDisposable
3026
{
31-
private readonly object _operationLock;
32-
33-
public Releaser(object operationLock)
27+
public Releaser()
3428
{
35-
_operationLock = operationLock;
3629
}
3730

3831
public void Dispose()
3932
{
40-
Monitor.Exit(_operationLock);
4133
}
4234
}
4335
}

0 commit comments

Comments
 (0)