Skip to content

Commit 465a492

Browse files
authored
Merge pull request #59 from rameel/cleanup
Clean up and formatting
2 parents 7a529c5 + c04c499 commit 465a492

4 files changed

Lines changed: 40 additions & 39 deletions

File tree

src/Ramstack.FileSystem.Amazon/S3UploadStream.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ public override void Write(ReadOnlySpan<byte> buffer)
100100
try
101101
{
102102
_stream.Write(buffer);
103-
104103
if (_stream.Length >= MinPartSize)
105104
UploadPart();
106105
}
107-
catch (Exception exception)
106+
catch
108107
{
109108
Abort();
110-
ExceptionDispatchInfo.Throw(exception);
109+
throw;
111110
}
112111
}
113112

@@ -124,10 +123,10 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella
124123
if (_stream.Length >= MinPartSize)
125124
await UploadPartAsync(cancellationToken).ConfigureAwait(false);
126125
}
127-
catch (Exception exception)
126+
catch
128127
{
129128
await AbortAsync(cancellationToken).ConfigureAwait(false);
130-
ExceptionDispatchInfo.Throw(exception);
129+
throw;
131130
}
132131
}
133132

@@ -254,10 +253,10 @@ private async ValueTask UploadPartAsync(CancellationToken cancellationToken)
254253
_stream.Position = 0;
255254
_stream.SetLength(0);
256255
}
257-
catch (Exception exception)
256+
catch
258257
{
259258
await AbortAsync(cancellationToken).ConfigureAwait(false);
260-
ExceptionDispatchInfo.Throw(exception);
259+
throw;
261260
}
262261
}
263262
}

src/Ramstack.FileSystem.Google/GcsWriteStream.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Ramstack.FileSystem.Google;
44

55
/// <summary>
6-
/// Represents a temporary write-only stream for Google Cloud Storage operations, redirecting all write operations to a temporary file.
7-
/// Upon disposing or closing the stream, the data is transferred to the Azure Blob storage.
6+
/// Represents a temporary write-only stream that buffers data to a temporary file before uploading it to Google Cloud Storage.
7+
/// Data is committed to the storage bucket when the stream is disposed or closed.
88
/// </summary>
99
internal sealed class GcsWriteStream : Stream
1010
{
1111
private readonly GoogleFileSystem _fs;
1212
private readonly string _objectName;
13-
private readonly FileStream _stream = CreateTempFileStream();
13+
private readonly FileStream _stream;
1414
private bool _disposed;
1515

1616
/// <inheritdoc />
@@ -53,6 +53,16 @@ public GcsWriteStream(GoogleFileSystem fs, string objectName)
5353
{
5454
_fs = fs;
5555
_objectName = objectName;
56+
_stream = new FileStream(
57+
Path.Combine(
58+
Path.GetTempPath(),
59+
Path.GetRandomFileName()),
60+
FileMode.CreateNew,
61+
FileAccess.ReadWrite,
62+
FileShare.None,
63+
bufferSize: 4096,
64+
FileOptions.DeleteOnClose
65+
| FileOptions.Asynchronous);
5666
}
5767

5868
/// <inheritdoc />
@@ -84,7 +94,6 @@ public override void Write(ReadOnlySpan<byte> buffer)
8494
{
8595
_disposed = true;
8696
_stream.Close();
87-
8897
throw;
8998
}
9099
}
@@ -104,7 +113,6 @@ public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, Cancella
104113
{
105114
_disposed = true;
106115
_stream.Close();
107-
108116
throw;
109117
}
110118
}
@@ -176,15 +184,6 @@ await _fs.StorageClient
176184
}
177185
}
178186

179-
private static FileStream CreateTempFileStream()
180-
{
181-
const int BufferSize = 4096;
182-
const FileOptions Options = FileOptions.DeleteOnClose | FileOptions.Asynchronous;
183-
184-
var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
185-
return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, BufferSize, Options);
186-
}
187-
188187
[DoesNotReturn]
189188
private static void Error_NotSupported() =>
190189
throw new NotSupportedException();

tests/Ramstack.FileSystem.Amazon.Tests/WritableAmazonFileSystemTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Ramstack.FileSystem.Amazon;
1414
[Category("Cloud:Amazon")]
1515
public class WritableAmazonFileSystemTests : VirtualFileSystemSpecificationTests
1616
{
17-
private readonly HashSet<string> _list = [];
17+
private readonly HashSet<string> _buckets = [];
1818
private readonly TempFileStorage _storage = new TempFileStorage();
1919

2020
[OneTimeSetUp]
@@ -35,17 +35,17 @@ public async Task Cleanup()
3535
{
3636
_storage.Dispose();
3737

38-
foreach (var name in _list.ToArray())
38+
foreach (var name in _buckets.ToArray())
3939
{
4040
using var fs = CreateFileSystem(name);
4141

4242
try
4343
{
4444
await fs.DeleteDirectoryAsync("/");
4545
}
46-
catch (Exception exception)
46+
catch (Exception e)
4747
{
48-
Console.WriteLine(exception);
48+
Console.WriteLine(e);
4949
}
5050
}
5151
}
@@ -61,14 +61,15 @@ public async Task File_OpenWrite_InternalBufferWriteError_DoesNotCreateFile()
6161
Assert.That(underlying, Is.Not.Null);
6262

6363
// Write enough data to trigger automatic part upload (>= 5 MiB).
64-
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[6 * 1024 * 1024]));
64+
await stream.WriteAsync(new byte[6 * 1024 * 1024]);
6565

6666
// Simulates an internal buffer write error.
6767
await underlying.DisposeAsync();
6868

6969
try
7070
{
71-
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[1024]));
71+
await stream.WriteAsync(new byte[1024]);
72+
Assert.Fail();
7273
}
7374
catch
7475
{
@@ -239,7 +240,6 @@ public async Task File_OpenWrite_FlushWithMultipartUpload()
239240
for (var i = 0; i < Count; i++)
240241
await stream.WriteAsync(chunk);
241242
}
242-
243243
{
244244
var file = fs.GetFile(FileName);
245245

@@ -314,7 +314,7 @@ protected override DirectoryInfo GetDirectoryInfo() =>
314314

315315
private AmazonS3FileSystem CreateFileSystem(string storageName)
316316
{
317-
_list.Add(storageName);
317+
_buckets.Add(storageName);
318318

319319
return new AmazonS3FileSystem(
320320
new BasicAWSCredentials("rustfsadmin", "rustfsadmin"),

tests/Ramstack.FileSystem.Google.Tests/WritableGoogleFileSystemTests.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public async Task Cleanup()
4343
{
4444
await fs.DeleteDirectoryAsync("/");
4545
}
46-
catch (Exception exception)
46+
catch (Exception e)
4747
{
48-
Console.WriteLine(exception);
48+
Console.WriteLine(e);
4949
}
5050
}
5151
}
@@ -60,22 +60,19 @@ public async Task File_OpenWrite_InternalBufferWriteError_DoesNotCreateFile()
6060
var underlying = (FileStream)stream.GetType().GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(stream)!;
6161
Assert.That(underlying, Is.Not.Null);
6262

63-
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[1024]));
64-
65-
// Forces to upload buffer.
66-
await stream.FlushAsync();
63+
await stream.WriteAsync(new byte[1024]);
6764

6865
// Simulates an internal buffer write error.
6966
await underlying.DisposeAsync();
7067

7168
try
7269
{
73-
await stream.WriteAsync(new ReadOnlyMemory<byte>(new byte[1024]));
70+
await stream.WriteAsync(new byte[1024]);
71+
Assert.Fail();
7472
}
75-
catch (Exception exception)
73+
catch
7674
{
77-
Console.WriteLine("Exception expected!");
78-
Console.WriteLine(exception);
75+
// Ignore
7976
}
8077
}
8178

@@ -160,6 +157,9 @@ public async Task File_CopyTo_File_DifferentFileSystems()
160157
Assert.That(
161158
await reader.ReadToEndAsync(),
162159
Is.EqualTo(content));
160+
161+
await source.DeleteAsync();
162+
await destination.DeleteAsync();
163163
}
164164

165165
[Test]
@@ -189,6 +189,9 @@ public async Task File_CopyTo_File_DifferentStorages()
189189
Assert.That(
190190
await reader.ReadToEndAsync(),
191191
Is.EqualTo(content));
192+
193+
await source.DeleteAsync();
194+
await destination.DeleteAsync();
192195
}
193196

194197
protected override GoogleFileSystem GetFileSystem() =>

0 commit comments

Comments
 (0)