Skip to content

Commit df7fbbb

Browse files
committed
Fix tests
1 parent efd326e commit df7fbbb

16 files changed

Lines changed: 752 additions & 731 deletions

src/Storage.Tests/BucketShould.cs

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Storage.Tests.Utils;
2-
3-
namespace Storage.Tests;
1+
namespace Storage.Tests;
42

53
public sealed class BucketShould(StorageFixture fixture) : IClassFixture<StorageFixture>
64
{
@@ -10,21 +8,8 @@ public sealed class BucketShould(StorageFixture fixture) : IClassFixture<Storage
108
[Fact]
119
public async Task CreateBucket()
1210
{
13-
var settings = fixture.Settings;
14-
15-
// don't use using here
16-
var client =
17-
new S3Client(
18-
new S3Settings
19-
{
20-
AccessKey = settings.AccessKey,
21-
Bucket = fixture.Create<string>(),
22-
EndPoint = settings.EndPoint,
23-
Port = settings.Port,
24-
SecretKey = settings.SecretKey,
25-
UseHttps = settings.UseHttps,
26-
},
27-
fixture.HttpClient);
11+
// don't dispose it
12+
var client = TestHelper.CloneClient(fixture);
2813

2914
var bucketCreateResult = await client.CreateBucket(_cancellation);
3015

@@ -46,21 +31,8 @@ public async Task BeExists()
4631
[Fact]
4732
public async Task BeNotExists()
4833
{
49-
var settings = fixture.Settings;
50-
5134
// don't dispose it
52-
var client =
53-
new S3Client(
54-
new S3Settings
55-
{
56-
AccessKey = settings.AccessKey,
57-
Bucket = fixture.Create<string>(),
58-
EndPoint = settings.EndPoint,
59-
Port = settings.Port,
60-
SecretKey = settings.SecretKey,
61-
UseHttps = settings.UseHttps,
62-
},
63-
fixture.HttpClient);
35+
var client = TestHelper.CloneClient(fixture);
6436

6537
var bucketExistsResult = await client.IsBucketExists(_cancellation);
6638

@@ -79,21 +51,8 @@ public Task NotThrowIfCreateBucketAlreadyExists()
7951
[Fact]
8052
public Task NotThrowIfDeleteNotExistsBucket()
8153
{
82-
var settings = fixture.Settings;
83-
84-
// don't use using here
85-
var client =
86-
new S3Client(
87-
new S3Settings
88-
{
89-
AccessKey = settings.AccessKey,
90-
Bucket = fixture.Create<string>(),
91-
EndPoint = settings.EndPoint,
92-
Port = settings.Port,
93-
SecretKey = settings.SecretKey,
94-
UseHttps = settings.UseHttps,
95-
},
96-
fixture.HttpClient);
54+
// don't dispose it
55+
var client = TestHelper.CloneClient(fixture);
9756

9857
return client
9958
.Invoking(c => c.DeleteBucket(_cancellation))

src/Storage.Tests/ClientShould.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using Storage.Tests.Utils;
2-
3-
namespace Storage.Tests;
1+
namespace Storage.Tests;
42

53
public sealed class ClientShould(StorageFixture fixture) : IClassFixture<StorageFixture>
64
{
5+
private readonly CancellationToken _cancellation = CancellationToken.None;
76
private readonly S3Client _client = fixture.S3Client;
87

98
[Fact]
@@ -24,4 +23,16 @@ public void HasValidInfo()
2423
.Bucket
2524
.Should().Be(fixture.Settings.Bucket);
2625
}
26+
27+
[Fact]
28+
public Task ThrowIfDisposed()
29+
{
30+
var client = TestHelper.CloneClient(fixture, null, new HttpClient());
31+
32+
client.Dispose();
33+
34+
return client
35+
.Invoking(c => c.CreateBucket(_cancellation))
36+
.Should().ThrowExactlyAsync<ObjectDisposedException>();
37+
}
2738
}

src/Storage.Tests/ObjectShould.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net;
2-
using Storage.Tests.Utils;
3-
using static Storage.Tests.Utils.StorageFixture;
2+
using static Storage.Tests.StorageFixture;
43

54
namespace Storage.Tests;
65

@@ -17,18 +16,7 @@ public ObjectShould(StorageFixture fixture)
1716
_client = fixture.S3Client;
1817
_fixture = fixture;
1918

20-
var settings = _fixture.Settings;
21-
_notExistsBucketClient = new S3Client(
22-
new S3Settings
23-
{
24-
AccessKey = settings.AccessKey,
25-
Bucket = _fixture.Create<string>(),
26-
EndPoint = settings.EndPoint,
27-
Port = settings.Port,
28-
SecretKey = settings.SecretKey,
29-
UseHttps = settings.UseHttps,
30-
},
31-
_fixture.HttpClient);
19+
_notExistsBucketClient = TestHelper.CloneClient(_fixture);
3220
}
3321

3422
[Fact]
@@ -44,16 +32,16 @@ public async Task AllowParallelUploadMultipleFiles()
4432
var fileName = $"{file}-{i}";
4533
tasks[i] = Task.Run(
4634
async () =>
47-
{
48-
await _client.UploadFile(fileName, StreamContentType, fileData, _ct);
49-
if (!await _client.IsFileExists(fileName, _ct))
5035
{
51-
return false;
52-
}
53-
54-
await _client.DeleteFile(fileName, _ct);
55-
return true;
56-
},
36+
await _client.UploadFile(fileName, StreamContentType, fileData, _ct);
37+
if (!await _client.IsFileExists(fileName, _ct))
38+
{
39+
return false;
40+
}
41+
42+
await _client.DeleteFile(fileName, _ct);
43+
return true;
44+
},
5745
_ct);
5846
}
5947

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using AutoFixture;
22
using DotNet.Testcontainers.Containers;
33

4-
namespace Storage.Tests.Utils;
4+
namespace Storage.Tests;
55

66
public sealed class StorageFixture : IDisposable, IAsyncDisposable
77
{
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,30 @@
22
using DotNet.Testcontainers.Builders;
33
using DotNet.Testcontainers.Containers;
44

5-
namespace Storage.Tests.Utils;
5+
namespace Storage.Tests;
66

77
public static class TestHelper
88
{
99
public const int MinioInternalPort = 9000;
1010
public const string SecretKey = "ChangeMe123";
1111
public const string Username = "ROOTUSER";
1212

13+
public static S3Client CloneClient(StorageFixture fixture, string? bucket = null, HttpClient? client = null)
14+
{
15+
var settings = fixture.Settings;
16+
var clonedSettings = new S3Settings
17+
{
18+
AccessKey = settings.AccessKey,
19+
Bucket = bucket ?? fixture.Create<string>(),
20+
EndPoint = settings.EndPoint,
21+
Port = settings.Port,
22+
SecretKey = settings.SecretKey,
23+
UseHttps = settings.UseHttps,
24+
};
25+
26+
return new S3Client(clonedSettings, client ?? fixture.HttpClient);
27+
}
28+
1329
public static S3Settings CreateSettings(IContainer? container = null)
1430
{
1531
var environmentPort = Environment.GetEnvironmentVariable("STORAGE_PORT");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Buffers;
2+
using Storage.Utils;
3+
4+
namespace Storage.Tests.Utils;
5+
6+
public class CollectionUtilsShould
7+
{
8+
private static readonly ArrayPool<int> Pool = ArrayPool<int>.Shared;
9+
10+
[Fact]
11+
public void ResizeArray()
12+
{
13+
var array = Pool.Rent(5);
14+
15+
var newLength = array.Length * 2;
16+
CollectionUtils.Resize(ref array, Pool, newLength);
17+
18+
array.Length
19+
.Should().BeGreaterOrEqualTo(newLength);
20+
}
21+
22+
[Fact]
23+
public void ResizeEmptyArray()
24+
{
25+
const int newLength = 5;
26+
27+
var emptyArray = Array.Empty<int>();
28+
CollectionUtils.Resize(ref emptyArray, Pool, newLength);
29+
30+
emptyArray.Length
31+
.Should().BeGreaterOrEqualTo(newLength);
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Storage.Utils;
2+
3+
namespace Storage.Tests.Utils;
4+
5+
public class ValueStringBuilderShould
6+
{
7+
[Fact]
8+
public void Grow()
9+
{
10+
const int stringLength = 256;
11+
var chars = Enumerable.Range(0, stringLength).Select(i => (char)i);
12+
13+
var builder = new ValueStringBuilder(stackalloc char[64]);
14+
foreach (var c in chars)
15+
{
16+
builder.Append(c);
17+
}
18+
19+
builder.Length.Should().Be(stringLength);
20+
}
21+
}

src/Storage/Storage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<ItemGroup>
3434
<InternalsVisibleTo Include="Storage.Benchmark" />
35+
<InternalsVisibleTo Include="Storage.Tests" />
3536
</ItemGroup>
3637

3738
<ItemGroup>

src/Storage/Utils/CollectionUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Storage.Utils;
44

5-
public static class CollectionUtils
5+
internal static class CollectionUtils
66
{
77
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88
public static void Resize<T>(ref T[] array, ArrayPool<T> pool, int newLength, bool clear = false)
@@ -17,4 +17,4 @@ public static void Resize<T>(ref T[] array, ArrayPool<T> pool, int newLength, bo
1717

1818
array = newArray;
1919
}
20-
}
20+
}

src/Storage/Utils/Errors.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,28 @@
22

33
internal static class Errors
44
{
5-
[MethodImpl(MethodImplOptions.NoInlining)]
6-
public static void CantFormatToString<T>(T value)
7-
where T : struct
8-
{
9-
throw new FormatException($"Can't format '{value}' to string");
10-
}
5+
[MethodImpl(MethodImplOptions.NoInlining)]
6+
public static void CantFormatToString<T>(T value)
7+
where T : struct
8+
{
9+
throw new FormatException($"Can't format '{value}' to string");
10+
}
1111

12-
[MethodImpl(MethodImplOptions.NoInlining)]
13-
public static void Disposed()
14-
{
15-
throw new ObjectDisposedException(nameof(S3Client));
16-
}
12+
[MethodImpl(MethodImplOptions.NoInlining)]
13+
public static void Disposed()
14+
{
15+
throw new ObjectDisposedException(nameof(S3Client));
16+
}
1717

18-
[MethodImpl(MethodImplOptions.NoInlining)]
19-
public static void UnexpectedResult(HttpResponseMessage response)
20-
{
21-
var reason = response.ReasonPhrase ?? response.ToString();
22-
var exception = new HttpRequestException($"Storage has returned an unexpected result: {response.StatusCode} ({reason})");
18+
[MethodImpl(MethodImplOptions.NoInlining)]
19+
public static void UnexpectedResult(HttpResponseMessage response)
20+
{
21+
var reason = response.ReasonPhrase ?? response.ToString();
22+
var exception =
23+
new HttpRequestException($"Storage has returned an unexpected result: {response.StatusCode} ({reason})");
2324

24-
response.Dispose();
25+
response.Dispose();
2526

26-
throw exception;
27-
}
27+
throw exception;
28+
}
2829
}

0 commit comments

Comments
 (0)