Skip to content

Commit c0aa1fb

Browse files
committed
Fix tests
1 parent 402878f commit c0aa1fb

5 files changed

Lines changed: 121 additions & 9 deletions

File tree

src/Storage.Tests/BucketShould.cs

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

33
public sealed class BucketShould(StorageFixture fixture) : IClassFixture<StorageFixture>
44
{
5-
private readonly CancellationToken _cancellation = CancellationToken.None;
5+
private readonly CancellationToken _ct = CancellationToken.None;
66
private readonly S3Client _client = fixture.S3Client;
77

88
[Fact]
@@ -11,7 +11,7 @@ public async Task CreateBucket()
1111
// don't dispose it
1212
var client = TestHelper.CloneClient(fixture);
1313

14-
var bucketCreateResult = await client.CreateBucket(_cancellation);
14+
var bucketCreateResult = await client.CreateBucket(_ct);
1515

1616
bucketCreateResult
1717
.Should().BeTrue();
@@ -22,7 +22,7 @@ public async Task CreateBucket()
2222
[Fact]
2323
public async Task BeExists()
2424
{
25-
var bucketExistsResult = await _client.IsBucketExists(_cancellation);
25+
var bucketExistsResult = await _client.IsBucketExists(_ct);
2626

2727
bucketExistsResult
2828
.Should().BeTrue();
@@ -34,7 +34,7 @@ public async Task BeNotExists()
3434
// don't dispose it
3535
var client = TestHelper.CloneClient(fixture);
3636

37-
var bucketExistsResult = await client.IsBucketExists(_cancellation);
37+
var bucketExistsResult = await client.IsBucketExists(_ct);
3838

3939
bucketExistsResult
4040
.Should().BeFalse();
@@ -44,7 +44,7 @@ public async Task BeNotExists()
4444
public Task NotThrowIfCreateBucketAlreadyExists()
4545
{
4646
return _client
47-
.Invoking(client => client.CreateBucket(_cancellation))
47+
.Invoking(client => client.CreateBucket(_ct))
4848
.Should().NotThrowAsync();
4949
}
5050

@@ -55,13 +55,13 @@ public Task NotThrowIfDeleteNotExistsBucket()
5555
var client = TestHelper.CloneClient(fixture);
5656

5757
return client
58-
.Invoking(c => c.DeleteBucket(_cancellation))
58+
.Invoking(c => c.DeleteBucket(_ct))
5959
.Should().NotThrowAsync();
6060
}
6161

6262
private async Task DeleteTestBucket(S3Client client)
6363
{
64-
var bucketDeleteResult = await client.DeleteBucket(_cancellation);
64+
var bucketDeleteResult = await client.DeleteBucket(_ct);
6565

6666
bucketDeleteResult
6767
.Should().BeTrue();

src/Storage.Tests/ClientShould.cs

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

33
public sealed class ClientShould(StorageFixture fixture) : IClassFixture<StorageFixture>
44
{
5-
private readonly CancellationToken _cancellation = CancellationToken.None;
5+
private readonly CancellationToken _ct = CancellationToken.None;
66
private readonly S3Client _client = fixture.S3Client;
77

88
[Fact]
@@ -32,7 +32,7 @@ public Task ThrowIfDisposed()
3232
client.Dispose();
3333

3434
return client
35-
.Invoking(c => c.CreateBucket(_cancellation))
35+
.Invoking(c => c.CreateBucket(_ct))
3636
.Should().ThrowExactlyAsync<ObjectDisposedException>();
3737
}
3838
}

src/Storage.Tests/ObjectShould.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,35 @@ public async Task GetFileUrlWithCyrillicName()
194194
.Should().BeTrue(response.ReasonPhrase);
195195
}
196196

197+
[Fact]
198+
public async Task HasValidUploadInformation()
199+
{
200+
var fileName = _fixture.Create<string>();
201+
202+
using var uploader = await _client.UploadFile(fileName, StreamContentType, _ct);
203+
204+
uploader
205+
.FileName
206+
.Should().Be(fileName);
207+
208+
uploader
209+
.UploadId
210+
.Should().NotBeEmpty();
211+
212+
uploader
213+
.Written
214+
.Should().Be(0);
215+
216+
var partData = new byte[] { 1, 2, 3 };
217+
await uploader.AddPart(partData, _ct);
218+
219+
uploader
220+
.Written
221+
.Should().Be(partData.Length);
222+
223+
await uploader.Abort(_ct);
224+
}
225+
197226
[Fact]
198227
public async Task HasValidInformation()
199228
{
@@ -334,6 +363,30 @@ public async Task PutBigStream()
334363
await DeleteTestFile(fileName);
335364
}
336365

366+
[Fact]
367+
public async Task ReadFileStream()
368+
{
369+
var fileName = await CreateTestFile();
370+
371+
var buffer = new byte[1024];
372+
var file = await _client.GetFile(fileName, _ct);
373+
var fileStream = await file.GetStream(_ct);
374+
375+
#pragma warning disable CA1835
376+
var read = await fileStream.ReadAsync(buffer, _ct);
377+
read.Should().BeGreaterThan(0);
378+
379+
read = await fileStream.ReadAsync(buffer, 10, 20, _ct);
380+
read.Should().BeGreaterThan(0);
381+
382+
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
383+
read = fileStream.Read(buffer, 10, 20);
384+
read.Should().BeGreaterThan(0);
385+
#pragma warning restore CA1835
386+
387+
await DeleteTestFile(fileName);
388+
}
389+
337390
[Fact]
338391
public async Task Upload()
339392
{
@@ -425,6 +478,15 @@ public Task NotThrowIfDeleteFileNotExists()
425478
.Should().NotThrowAsync();
426479
}
427480

481+
[Fact]
482+
public async Task NotThrowIfGetFileStreamNotFound()
483+
{
484+
var fileName = _fixture.Create<string>();
485+
await _client
486+
.Invoking(client => client.GetFileStream(fileName, _ct))
487+
.Should().NotThrowAsync();
488+
}
489+
428490
[Fact]
429491
public async Task ThrowIfBucketNotExists()
430492
{
@@ -445,6 +507,27 @@ await _notExistsBucketClient
445507
.Should().ThrowAsync<HttpRequestException>();
446508
}
447509

510+
[Fact]
511+
public async Task ThrowIfUploadDisposed()
512+
{
513+
var fileName = _fixture.Create<string>();
514+
515+
var uploader = await _client.UploadFile(fileName, StreamContentType, _ct);
516+
uploader.Dispose();
517+
518+
await uploader
519+
.Invoking(u => u.Abort(_ct))
520+
.Should().ThrowExactlyAsync<ObjectDisposedException>();
521+
522+
await uploader
523+
.Invoking(u => u.AddPart([], 0, _ct))
524+
.Should().ThrowExactlyAsync<ObjectDisposedException>();
525+
526+
await uploader
527+
.Invoking(u => u.Complete(_ct))
528+
.Should().ThrowExactlyAsync<ObjectDisposedException>();
529+
}
530+
448531
private async Task<string> CreateTestFile(
449532
string? fileName = null,
450533
string contentType = StreamContentType,

src/Storage.Tests/Utils/ValueStringBuilderShould.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@ public void Grow()
1818

1919
builder.Length.Should().Be(stringLength);
2020
}
21+
22+
[Fact]
23+
public void NotCreateEmptyString()
24+
{
25+
var builder = new ValueStringBuilder(stackalloc char[64]);
26+
builder
27+
.ToString()
28+
.Should().BeEmpty();
29+
}
30+
31+
[Fact]
32+
public void RemoveLastCorrectly()
33+
{
34+
var builder = new ValueStringBuilder(stackalloc char[64]);
35+
builder.RemoveLast();
36+
37+
builder.Length
38+
.Should().BeGreaterThan(-1);
39+
}
2140
}

src/Storage/S3Upload.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public long Written
6363
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6464
public Task<bool> Abort(CancellationToken ct)
6565
{
66+
if (_disposed)
67+
{
68+
Errors.Disposed();
69+
}
70+
6671
return _client.MultipartAbort(_encodedFileName, UploadId, ct);
6772
}
6873

@@ -174,6 +179,11 @@ public async Task<bool> AddParts(byte[] data, CancellationToken ct)
174179
/// <returns>Возвращает результат завершения загрузки</returns>
175180
public Task<bool> Complete(CancellationToken ct)
176181
{
182+
if (_disposed)
183+
{
184+
Errors.Disposed();
185+
}
186+
177187
return _partCount == 0
178188
? Task.FromResult(false)
179189
: _client.MultipartComplete(_encodedFileName, UploadId, _parts, _partCount, ct);

0 commit comments

Comments
 (0)