Skip to content

Commit 7d6293f

Browse files
authored
Support of Linux and MacOS environments (#14)
* Adapt behavior to linux FileShare limitations * Update azure-pipelines.yml for linux support * Fix check timeout * Update azure-pipelines.yml for publishSymbols on WIndows only * Update azure-pipelines.yml for ReportGenerator on MacOS * Rollback latest update of azure-pipelines.yml * Rollback manually
1 parent d95e04d commit 7d6293f

3 files changed

Lines changed: 47 additions & 10 deletions

File tree

FollowingFileStream.Tests/FollowingFileStreamTest.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public FollowingFileStreamTest()
1515
{
1616
using (var sw = File.CreateText(inputFilePath))
1717
{
18-
sw.WriteLine("coucou");
18+
sw.Write("coucou");
1919
}
2020
}
2121

@@ -66,6 +66,7 @@ public void FFS_Caps()
6666
Assert.IsTrue(ffs.CanRead);
6767
Assert.IsFalse(ffs.CanWrite);
6868
Assert.IsTrue(ffs.CanSeek);
69+
Assert.IsTrue(ffs.CanTimeout);
6970
}
7071
}
7172

@@ -104,11 +105,11 @@ public void FFS_Read(bool async)
104105
{
105106
using (var ffs = new FollowingFileStream(inputFilePath, 4*1096, async))
106107
{
108+
var expected = "coucou";
107109
Assert.AreEqual(0, ffs.Position);
108-
Assert.AreEqual(8, ffs.Length);
110+
Assert.AreEqual(expected.Length, ffs.Length);
109111

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

@@ -134,6 +135,7 @@ public void FFS_CopyTo(bool async)
134135
using (var ffs = new FollowingFileStream(inputFilePath, 4*1096, async))
135136
using (var destination = File.CreateText(outputFilePath))
136137
{
138+
ffs.ReadTimeout = 0;
137139
ffs.CopyTo(destination.BaseStream);
138140
}
139141
Assert.IsTrue(FileCompare(inputFilePath, outputFilePath));
@@ -148,16 +150,18 @@ public void FFS_FollowingRead(bool async)
148150
using (var ffs = new FollowingFileStream(inputFilePath, 4*1024, async))
149151
using (var destination = File.CreateText(outputFilePath))
150152
{
153+
ffs.ReadTimeout = 400;
151154
destination.AutoFlush = true;
152155
var os = destination.BaseStream;
153156
var copy = ffs.CopyToAsync(os);
154157
Assert.AreEqual(0, os.Length);
155-
Thread.Sleep(200);
158+
Thread.Sleep(ffs.ReadTimeout/2);
156159
Assert.IsFalse(copy.IsCompleted);
157160
input.WriteLine("coucou2");
158161
input.Close();
159-
Thread.Sleep(200);
160-
Assert.IsTrue(copy.IsCompletedSuccessfully);
162+
//Thread.Sleep(200);
163+
//Assert.IsTrue(copy.IsCompletedSuccessfully);
164+
Assert.IsTrue(copy.Wait(3*ffs.ReadTimeout));
161165
}
162166
Assert.IsTrue(FileCompare(inputFilePath, outputFilePath));
163167
}

FollowingFileStream/FollowingFileStream.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Runtime.InteropServices;
34
using System.Threading;
45
using System.Threading.Tasks;
56

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

247+
TotalTime = 0;
248+
246249
return read;
247250
}
248251

@@ -257,12 +260,20 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
257260
/// </returns>
258261
private async Task<bool> RetryNeededAsync()
259262
{
260-
bool retry = IsFileLockedForWriting();
263+
bool retry = true;
264+
// File locking for read/write operations is only supported on Windows
265+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
266+
{
267+
retry = IsFileLockedForWriting();
268+
}
261269
if (retry)
262270
{
263271
try
264272
{
265-
await Task.Delay(MillisecondsRetryTimeout, cts.Token).ConfigureAwait(false);
273+
var duration = MillisecondsRetryTimeout;
274+
await Task.Delay(duration, cts.Token).ConfigureAwait(false);
275+
TotalTime += duration;
276+
retry = ReadTimeout == Timeout.Infinite || TotalTime < ReadTimeout;
266277
}
267278
catch (TaskCanceledException)
268279
{
@@ -272,6 +283,18 @@ private async Task<bool> RetryNeededAsync()
272283
return retry;
273284
}
274285

286+
private long TotalTime;
287+
288+
/// <summary>
289+
///
290+
/// </summary>
291+
public override bool CanTimeout => true;
292+
293+
/// <summary>
294+
///
295+
/// </summary>
296+
public override int ReadTimeout { get; set; } = Timeout.Infinite;
297+
275298
/// <summary>
276299
/// Synchronously checks whether the file is locked for writing
277300
/// </summary>

azure-pipelines.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
trigger:
77
- master
88

9+
strategy:
10+
matrix:
11+
linux:
12+
imageName: 'ubuntu-latest'
13+
mac:
14+
imageName: 'macos-latest'
15+
windows:
16+
imageName: 'windows-latest'
17+
918
pool:
10-
vmImage: 'windows-latest'
19+
vmImage: $(imageName)
1120

1221
variables:
1322
buildConfiguration: 'Release'
@@ -89,6 +98,7 @@ steps:
8998
versionEnvVar: 'GitVersion.NuGetVersion'
9099

91100
- task: PublishSymbols@2
101+
condition: eq( variables['Agent.OS'], 'Windows_NT' )
92102
inputs:
93103
SearchPattern: |
94104
**/bin/**/FollowingFileStream.pdb

0 commit comments

Comments
 (0)