|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace Testably.Abstractions.Tests.FileSystem.FileStream; |
| 5 | + |
| 6 | +[FileSystemTests] |
| 7 | +public partial class ParallelTests |
| 8 | +{ |
| 9 | + [Theory] |
| 10 | + [AutoData] |
| 11 | + public async Task MultipleFlush_ShouldKeepLatestChanges(string path) |
| 12 | + { |
| 13 | + using (FileSystemStream stream1 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 14 | + FileAccess.Write, FileShare.ReadWrite)) |
| 15 | + { |
| 16 | + using FileSystemStream stream2 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 17 | + FileAccess.Write, FileShare.ReadWrite); |
| 18 | + |
| 19 | + stream2.Write(Encoding.UTF8.GetBytes("foo"), 0, 3); |
| 20 | + stream1.Write(Encoding.UTF8.GetBytes("bar"), 0, 3); |
| 21 | + |
| 22 | + stream1.Flush(); |
| 23 | + stream2.Flush(); |
| 24 | + } |
| 25 | + |
| 26 | + await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foo"); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [AutoData] |
| 31 | + public async Task MultipleFlush_DifferentLength_ShouldKeepAdditionalBytes(string path) |
| 32 | + { |
| 33 | + using (FileSystemStream stream1 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 34 | + FileAccess.Write, FileShare.ReadWrite)) |
| 35 | + { |
| 36 | + using FileSystemStream stream2 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 37 | + FileAccess.Write, FileShare.ReadWrite); |
| 38 | + |
| 39 | + stream2.Write(Encoding.UTF8.GetBytes("foo"), 0, 3); |
| 40 | + stream1.Write(Encoding.UTF8.GetBytes("barfoo"), 0, 6); |
| 41 | + |
| 42 | + await That(stream1).HasLength().EqualTo(6); |
| 43 | + await That(stream2).HasLength().EqualTo(3); |
| 44 | + |
| 45 | + stream1.Flush(); |
| 46 | + |
| 47 | + await That(stream1).HasLength().EqualTo(6); |
| 48 | + await That(stream2).HasLength().EqualTo(6); |
| 49 | + |
| 50 | + stream2.Flush(); |
| 51 | + |
| 52 | + await That(stream1).HasLength().EqualTo(6); |
| 53 | + await That(stream2).HasLength().EqualTo(6); |
| 54 | + } |
| 55 | + |
| 56 | + await That(FileSystem.File.ReadAllText(path)).IsEqualTo("foofoo"); |
| 57 | + } |
| 58 | + |
| 59 | + [Theory] |
| 60 | + [AutoData] |
| 61 | + public async Task MultipleFlush_DifferentPosition_ShouldKeepAdditionalBytes(string path) |
| 62 | + { |
| 63 | + FileSystem.File.WriteAllText(path, "AAAAAAAAAAAA"); |
| 64 | + using (FileSystemStream stream1 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 65 | + FileAccess.Write, FileShare.ReadWrite)) |
| 66 | + { |
| 67 | + using FileSystemStream stream2 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 68 | + FileAccess.Write, FileShare.ReadWrite); |
| 69 | + stream2.Position = 3; |
| 70 | + stream1.Position = 2; |
| 71 | + |
| 72 | + stream2.Write(Encoding.UTF8.GetBytes("CCC"), 0, 3); |
| 73 | + stream1.Write(Encoding.UTF8.GetBytes("bbbbbb"), 0, 6); |
| 74 | + |
| 75 | + stream1.Flush(); |
| 76 | + stream2.Flush(); |
| 77 | + } |
| 78 | + |
| 79 | + await That(FileSystem.File.ReadAllText(path)).IsEqualTo("AAbCCCbbAAAA"); |
| 80 | + } |
| 81 | + |
| 82 | + [Theory] |
| 83 | + [AutoData] |
| 84 | + public async Task MultipleFlush_DifferentPositionWithGaps_ShouldKeepAdditionalBytes(string path) |
| 85 | + { |
| 86 | + FileSystem.File.WriteAllText(path, "AAAAAAAAAAAA"); |
| 87 | + using (FileSystemStream stream1 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 88 | + FileAccess.Write, FileShare.ReadWrite)) |
| 89 | + { |
| 90 | + using FileSystemStream stream2 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 91 | + FileAccess.Write, FileShare.ReadWrite); |
| 92 | + stream1.Position = 2; |
| 93 | + |
| 94 | + stream2.Position = 3; |
| 95 | + stream2.Write(Encoding.UTF8.GetBytes("C"), 0, 1); |
| 96 | + stream2.Position = 5; |
| 97 | + stream2.Write(Encoding.UTF8.GetBytes("C"), 0, 1); |
| 98 | + stream1.Write(Encoding.UTF8.GetBytes("bbbbbb"), 0, 6); |
| 99 | + |
| 100 | + stream1.Flush(); |
| 101 | + stream2.Flush(); |
| 102 | + } |
| 103 | + |
| 104 | + await That(FileSystem.File.ReadAllText(path)).IsEqualTo("AAbbbCbbAAAA"); |
| 105 | + } |
| 106 | + |
| 107 | + [Theory] |
| 108 | + [AutoData] |
| 109 | + public async Task WriteEmpty_ShouldNotOverwrite(string path) |
| 110 | + { |
| 111 | + using (FileSystemStream stream1 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 112 | + FileAccess.Write, FileShare.ReadWrite)) |
| 113 | + { |
| 114 | + using FileSystemStream stream2 = FileSystem.File.Open(path, FileMode.OpenOrCreate, |
| 115 | + FileAccess.Write, FileShare.ReadWrite); |
| 116 | + |
| 117 | + stream2.Write(Encoding.UTF8.GetBytes(""), 0, 0); |
| 118 | + stream1.Write(Encoding.UTF8.GetBytes("barfoo"), 0, 6); |
| 119 | + |
| 120 | + stream1.Flush(); |
| 121 | + stream2.Flush(); |
| 122 | + } |
| 123 | + |
| 124 | + await That(FileSystem.File.ReadAllText(path)).IsEqualTo("barfoo"); |
| 125 | + } |
| 126 | +} |
0 commit comments