Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions FollowingFileStream.Tests/FollowingFileStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FollowingFileStreamTest()
{
using (var sw = File.CreateText(inputFilePath))
{
sw.WriteLine("coucou");
sw.Write("coucou");
}
}

Expand Down Expand Up @@ -66,6 +66,7 @@ public void FFS_Caps()
Assert.IsTrue(ffs.CanRead);
Assert.IsFalse(ffs.CanWrite);
Assert.IsTrue(ffs.CanSeek);
Assert.IsTrue(ffs.CanTimeout);
}
}

Expand Down Expand Up @@ -104,11 +105,11 @@ public void FFS_Read(bool async)
{
using (var ffs = new FollowingFileStream(inputFilePath, 4*1096, async))
{
var expected = "coucou";
Assert.AreEqual(0, ffs.Position);
Assert.AreEqual(8, ffs.Length);
Assert.AreEqual(expected.Length, ffs.Length);

var expected = "coucou" + Environment.NewLine;
var bytes = new byte[8];
var bytes = new byte[expected.Length];
Assert.AreEqual(expected.Length, ffs.Read(bytes, 0, bytes.Length));
Assert.AreEqual(expected, System.Text.Encoding.Default.GetString(bytes));

Expand All @@ -134,6 +135,7 @@ public void FFS_CopyTo(bool async)
using (var ffs = new FollowingFileStream(inputFilePath, 4*1096, async))
using (var destination = File.CreateText(outputFilePath))
{
ffs.ReadTimeout = 0;
ffs.CopyTo(destination.BaseStream);
}
Assert.IsTrue(FileCompare(inputFilePath, outputFilePath));
Expand All @@ -148,16 +150,18 @@ public void FFS_FollowingRead(bool async)
using (var ffs = new FollowingFileStream(inputFilePath, 4*1024, async))
using (var destination = File.CreateText(outputFilePath))
{
ffs.ReadTimeout = 400;
destination.AutoFlush = true;
var os = destination.BaseStream;
var copy = ffs.CopyToAsync(os);
Assert.AreEqual(0, os.Length);
Thread.Sleep(200);
Thread.Sleep(ffs.ReadTimeout/2);
Assert.IsFalse(copy.IsCompleted);
input.WriteLine("coucou2");
input.Close();
Thread.Sleep(200);
Assert.IsTrue(copy.IsCompletedSuccessfully);
//Thread.Sleep(200);
//Assert.IsTrue(copy.IsCompletedSuccessfully);
Assert.IsTrue(copy.Wait(3*ffs.ReadTimeout));
}
Assert.IsTrue(FileCompare(inputFilePath, outputFilePath));
}
Expand Down
27 changes: 25 additions & 2 deletions FollowingFileStream/FollowingFileStream.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -243,6 +244,8 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
read = await fileStream.ReadAsync(buffer, offset, count, cancellationToken);
}

TotalTime = 0;

return read;
}

Expand All @@ -257,12 +260,20 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
/// </returns>
private async Task<bool> RetryNeededAsync()
{
bool retry = IsFileLockedForWriting();
bool retry = true;
// File locking for read/write operations is only supported on Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
retry = IsFileLockedForWriting();
}
if (retry)
{
try
{
await Task.Delay(MillisecondsRetryTimeout, cts.Token).ConfigureAwait(false);
var duration = MillisecondsRetryTimeout;
await Task.Delay(duration, cts.Token).ConfigureAwait(false);
TotalTime += duration;
retry = ReadTimeout == Timeout.Infinite || TotalTime < ReadTimeout;
}
catch (TaskCanceledException)
{
Expand All @@ -272,6 +283,18 @@ private async Task<bool> RetryNeededAsync()
return retry;
}

private long TotalTime;

/// <summary>
///
/// </summary>
public override bool CanTimeout => true;

/// <summary>
///
/// </summary>
public override int ReadTimeout { get; set; } = Timeout.Infinite;

/// <summary>
/// Synchronously checks whether the file is locked for writing
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
trigger:
- master

strategy:
matrix:
linux:
imageName: 'ubuntu-latest'
mac:
imageName: 'macos-latest'
windows:
imageName: 'windows-latest'

pool:
vmImage: 'windows-latest'
vmImage: $(imageName)

variables:
buildConfiguration: 'Release'
Expand Down Expand Up @@ -89,6 +98,7 @@ steps:
versionEnvVar: 'GitVersion.NuGetVersion'

- task: PublishSymbols@2
condition: eq( variables['Agent.OS'], 'Windows_NT' )
inputs:
SearchPattern: |
**/bin/**/FollowingFileStream.pdb
Expand Down