Skip to content

Commit

Permalink
fix: Do not throw on read stream Flush
Browse files Browse the repository at this point in the history
- Fixes unoplatform#5751 by making Flush a no-op for some of the Storage read-only streams
  • Loading branch information
MartinZikmund committed Apr 16, 2021
1 parent f952c3e commit 3b67dd4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,72 @@ public async Task When_GetParent()
}
}

[TestMethod]
public async Task When_ReadStreamFlush()
{
var rootFolder = await GetRootFolderAsync();
var fileName = GetRandomTextFileName();
StorageFile? createdFile = null;
try
{
createdFile = await rootFolder.CreateFileAsync(fileName);
await FileIO.WriteTextAsync(createdFile, "Hello world!");

var buffer = new byte[1024];

using var stream = await createdFile.OpenStreamForReadAsync();

while (await stream.ReadAsync(buffer, 0, 1024) > 0)
{
}

try
{
stream.Flush();
}
catch (Exception ex)
{
Assert.Fail($"Read stream flush threw {ex}");
}
}
finally
{
await DeleteIfNotNullAsync(createdFile);
await CleanupRootFolderAsync();
}
}

[TestMethod]
public async Task When_InputStreamFlush()
{
var rootFolder = await GetRootFolderAsync();
var fileName = GetRandomTextFileName();
StorageFile? createdFile = null;
try
{
createdFile = await rootFolder.CreateFileAsync(fileName);
await FileIO.WriteTextAsync(createdFile, "Hello world!");

var buffer = new byte[1024];

using var stream = await createdFile.OpenReadAsync();

try
{
stream.FlushAsync();
}
catch (Exception ex)
{
Assert.Fail($"Input stream flush threw {ex}");
}
}
finally
{
await DeleteIfNotNullAsync(createdFile);
await CleanupRootFolderAsync();
}
}

private string GetRandomFolderName() => Guid.NewGuid().ToString();

private string GetRandomTextFileName() => Guid.NewGuid().ToString() + ".txt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public async Task When_FromUri()
Assert.AreEqual(_unoStaticTestFileContent, actual);
}

[TestMethod]
public async Task When_FlushReadOnly()
{
var sut = RandomAccessStreamReference.CreateFromUri(new Uri("https://nv-assets.azurewebsites.net/uno-unit-tests.txt"));
using var readStream = await sut.OpenReadAsync();

try
{
await readStream.FlushAsync();
}
catch (Exception)
{
// UWP throws NotImplementedException
// Uno throws InvalidOperationException with a description
}
}

#if !NETFX_CORE
[TestMethod]
public async Task When_FromFile()
Expand Down Expand Up @@ -54,7 +71,7 @@ public async Task When_FromStream()
var tempContentBytes = Encoding.UTF8.GetBytes(tempContent);
temp.Write(tempContentBytes, 0, tempContentBytes.Length);
temp.Position = 0;

var actual = await ReadToEnd(sut);

Assert.AreEqual(tempContent, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public RentedStream Rent()

public override void SetLength(long value) => throw new NotSupportedException("This stream is read-only");

public override void Flush() => throw new NotSupportedException("This stream is read-only");
public override void Flush() { }

public static async Task<NativeReadStream> CreateAsync(Guid fileId)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Uno.UWP/Storage/Streams/StreamOverInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
=> throw new NotSupportedException("Cannot Write an input stream.");

/// <inheritdoc />
public override void Flush()
=> FlushAsync(CancellationToken.None).Wait();
public override void Flush() { }

/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
=> throw new NotSupportedException("Cannot Flush an input stream.");
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}

0 comments on commit 3b67dd4

Please sign in to comment.