Skip to content

Commit d1bd9ef

Browse files
author
Michael Pawlik
committed
Add unit tests for FileSystem class and enhance ReadAllTextAsync for concurrent access
1 parent 54b88ef commit d1bd9ef

2 files changed

Lines changed: 118 additions & 1 deletion

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Calinga.NET.Infrastructure;
7+
using FluentAssertions;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace Calinga.NET.Tests
11+
{
12+
[TestClass]
13+
public class FileSystemTests
14+
{
15+
private string _tempDir;
16+
private FileSystem _sut;
17+
18+
[TestInitialize]
19+
public void Init()
20+
{
21+
_tempDir = Path.Combine(Path.GetTempPath(), $"FileSystemTest_{Guid.NewGuid()}");
22+
Directory.CreateDirectory(_tempDir);
23+
_sut = new FileSystem();
24+
}
25+
26+
[TestCleanup]
27+
public void Cleanup()
28+
{
29+
if (Directory.Exists(_tempDir))
30+
Directory.Delete(_tempDir, true);
31+
}
32+
33+
[TestMethod]
34+
public async Task ReadAllTextAsync_ShouldAllowConcurrentReads()
35+
{
36+
// Arrange
37+
var filePath = Path.Combine(_tempDir, "test.json");
38+
var content = "{\"key\": \"value\"}";
39+
await File.WriteAllTextAsync(filePath, content);
40+
41+
// Act - Multiple concurrent reads should not throw
42+
var tasks = Enumerable.Range(0, 10)
43+
.Select(_ => Task.Run(() => _sut.ReadAllTextAsync(filePath)))
44+
.ToList();
45+
46+
Func<Task> act = async () => await Task.WhenAll(tasks);
47+
48+
// Assert
49+
await act.Should().NotThrowAsync("concurrent reads should be allowed");
50+
51+
// Verify all reads returned correct content
52+
var results = await Task.WhenAll(tasks);
53+
foreach (var result in results)
54+
{
55+
result.Should().Be(content);
56+
}
57+
}
58+
59+
[TestMethod]
60+
public async Task WriteAllTextAsync_ShouldCreateFileWithContent()
61+
{
62+
// Arrange
63+
var filePath = Path.Combine(_tempDir, "write_test.json");
64+
var content = "{\"test\": \"data\"}";
65+
66+
// Act
67+
await _sut.WriteAllTextAsync(filePath, content);
68+
69+
// Assert
70+
File.Exists(filePath).Should().BeTrue();
71+
var readContent = await File.ReadAllTextAsync(filePath);
72+
readContent.Should().Be(content);
73+
}
74+
75+
[TestMethod]
76+
public async Task ReadAllTextAsync_ShouldReadFileContent()
77+
{
78+
// Arrange
79+
var filePath = Path.Combine(_tempDir, "read_test.json");
80+
var content = "{\"hello\": \"world\"}";
81+
await File.WriteAllTextAsync(filePath, content);
82+
83+
// Act
84+
var result = await _sut.ReadAllTextAsync(filePath);
85+
86+
// Assert
87+
result.Should().Be(content);
88+
}
89+
90+
[TestMethod]
91+
public void FileExists_ShouldReturnTrue_WhenFileExists()
92+
{
93+
// Arrange
94+
var filePath = Path.Combine(_tempDir, "exists_test.json");
95+
File.WriteAllText(filePath, "test");
96+
97+
// Act
98+
var result = _sut.FileExists(filePath);
99+
100+
// Assert
101+
result.Should().BeTrue();
102+
}
103+
104+
[TestMethod]
105+
public void FileExists_ShouldReturnFalse_WhenFileDoesNotExist()
106+
{
107+
// Arrange
108+
var filePath = Path.Combine(_tempDir, "nonexistent.json");
109+
110+
// Act
111+
var result = _sut.FileExists(filePath);
112+
113+
// Assert
114+
result.Should().BeFalse();
115+
}
116+
}
117+
}

Calinga.NET/Infrastructure/FileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task WriteAllTextAsync(string path, string contents)
2121

2222
public async Task<string> ReadAllTextAsync(string path)
2323
{
24-
using (var tempFileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
24+
using (var tempFileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
2525
using (var tempStreamReader = new StreamReader(tempFileStream, Encoding.UTF8))
2626
{
2727
return await tempStreamReader.ReadToEndAsync().ConfigureAwait(false);

0 commit comments

Comments
 (0)