-
Notifications
You must be signed in to change notification settings - Fork 47
Add IDataStreamReceiver.SaveToStreamAsync and lower in-memory threshold to 128MB #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b53fde7
27263a1
918618e
b7c1598
a1d6939
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,4 +122,6 @@ UpgradeLog*.XML | |
|
|
||
| tools/ | ||
| built-packages/** | ||
| artifacts/ | ||
| artifacts/ | ||
|
|
||
| docs/superpowers/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Halibut.Queue.Redis.MessageStorage; | ||
| using Halibut.Tests.Support; | ||
| using Halibut.Transport.Protocol; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Halibut.Tests.Transport.Protocol | ||
| { | ||
| public class DataStreamReceiverSaveToStreamAsyncFixture : BaseTest | ||
| { | ||
| static byte[] SomeBytes() => Encoding.UTF8.GetBytes("Hello from SaveToStreamAsync!"); | ||
|
|
||
| [Test] | ||
| public async Task InMemoryDataStreamReceiver_SaveToStreamAsync_WritesTheWritersData() | ||
| { | ||
| var data = SomeBytes(); | ||
| var sut = new InMemoryDataStreamReceiver((stream, ct) => stream.WriteAsync(data, 0, data.Length, ct)); | ||
|
|
||
| using var destination = new MemoryStream(); | ||
| await sut.SaveToStreamAsync(destination, CancellationToken); | ||
|
|
||
| destination.ToArray().Should().BeEquivalentTo(data); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task TemporaryFileDataStreamReceiver_SaveToStreamAsync_WritesTheWritersData() | ||
| { | ||
| var data = SomeBytes(); | ||
| var sut = new TemporaryFileDataStreamReceiver((stream, ct) => stream.WriteAsync(data, 0, data.Length, ct)); | ||
|
|
||
| using var destination = new MemoryStream(); | ||
| await sut.SaveToStreamAsync(destination, CancellationToken); | ||
|
|
||
| destination.ToArray().Should().BeEquivalentTo(data); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task TemporaryFileStream_SaveToStreamAsync_WritesTheFilesDataAndDeletesTheSourceFile() | ||
| { | ||
| var data = SomeBytes(); | ||
| var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
| File.WriteAllBytes(path, data); | ||
|
|
||
| var sut = new TemporaryFileStream(path, HalibutLog); | ||
|
|
||
| using var destination = new MemoryStream(); | ||
| await sut.SaveToStreamAsync(destination, CancellationToken); | ||
|
|
||
| destination.ToArray().Should().BeEquivalentTo(data); | ||
| File.Exists(path).Should().BeFalse("the source temp file should be deleted once consumed"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task TemporaryFileStream_SaveToStreamAsync_CannotBeCalledTwice() | ||
| { | ||
| var data = SomeBytes(); | ||
| var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
| File.WriteAllBytes(path, data); | ||
|
|
||
| var sut = new TemporaryFileStream(path, HalibutLog); | ||
|
|
||
| using (var destination = new MemoryStream()) | ||
| { | ||
| await sut.SaveToStreamAsync(destination, CancellationToken); | ||
| } | ||
|
|
||
| using var secondDestination = new MemoryStream(); | ||
| await AssertException.Throws<InvalidOperationException>(async () => await sut.SaveToStreamAsync(secondDestination, CancellationToken)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task DataStreamRehydrationDataDataStreamReceiver_SaveToStreamAsync_WritesTheSuppliedData() | ||
| { | ||
| var data = SomeBytes(); | ||
| var sourceStream = new MemoryStream(data); | ||
| var sut = new DataStreamRehydrationDataDataStreamReceiver(() => new DataStreamRehydrationData(sourceStream)); | ||
|
|
||
| using var destination = new MemoryStream(); | ||
| await sut.SaveToStreamAsync(destination, CancellationToken); | ||
|
|
||
| destination.ToArray().Should().BeEquivalentTo(data); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,23 @@ void SetFilePermissionsToInheritFromParent(string filePath) | |
| } | ||
| } | ||
|
|
||
| public async Task SaveToStreamAsync(Stream destinationStream, CancellationToken cancellationToken) | ||
| { | ||
| if (moved) throw new InvalidOperationException("This stream has already been received once, and it cannot be read again."); | ||
|
|
||
| using (var file = new FileStream(path, FileMode.Open, FileAccess.Read)) | ||
| { | ||
| #if NET8_0_OR_GREATER | ||
| await file.CopyToAsync(destinationStream, cancellationToken); | ||
| #else | ||
| await file.CopyToAsync(destinationStream); | ||
| #endif | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep |
||
| await AttemptToDeleteAsync(path); | ||
| moved = true; | ||
| GC.SuppressFinalize(this); | ||
| } | ||
|
|
||
| public async Task ReadAsync(Func<Stream, CancellationToken, Task> readerAsync, CancellationToken cancellationToken) | ||
| { | ||
| if (moved) throw new InvalidOperationException("This stream has already been received once, and it cannot be read again."); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.