|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using Serilog.Sinks.File.Tests.Support; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Serilog.Sinks.File.Tests |
| 7 | +{ |
| 8 | + public class WriteCountingStreamTests |
| 9 | + { |
| 10 | + [Fact] |
| 11 | + public void CountedLengthIsResetToStreamLengthIfNewSizeIsSmaller() |
| 12 | + { |
| 13 | + // If we counted 10 bytes written and SetLength was called with a smaller length (e.g. 5) |
| 14 | + // we adjust the counter to the new byte count of the file to reflect reality |
| 15 | + |
| 16 | + using (var tmp = TempFolder.ForCaller()) |
| 17 | + { |
| 18 | + var path = tmp.AllocateFilename("txt"); |
| 19 | + |
| 20 | + long streamLengthAfterSetLength; |
| 21 | + long countedLengthAfterSetLength; |
| 22 | + |
| 23 | + using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)) |
| 24 | + using (var countingStream = new WriteCountingStream(fileStream)) |
| 25 | + using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false))) |
| 26 | + { |
| 27 | + writer.WriteLine("Hello, world!"); |
| 28 | + writer.Flush(); |
| 29 | + |
| 30 | + countingStream.SetLength(5); |
| 31 | + streamLengthAfterSetLength = countingStream.Length; |
| 32 | + countedLengthAfterSetLength = countingStream.CountedLength; |
| 33 | + } |
| 34 | + |
| 35 | + Assert.Equal(5, streamLengthAfterSetLength); |
| 36 | + Assert.Equal(5, countedLengthAfterSetLength); |
| 37 | + |
| 38 | + var lines = System.IO.File.ReadAllLines(path); |
| 39 | + |
| 40 | + Assert.Single(lines); |
| 41 | + Assert.Equal("Hello", lines[0]); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void CountedLengthRemainsTheSameIfNewSizeIsLarger() |
| 47 | + { |
| 48 | + // If we counted 10 bytes written and SetLength was called with a larger length (e.g. 100) |
| 49 | + // we leave the counter intact because our position on the stream remains the same... The |
| 50 | + // file just grew larger in size |
| 51 | + |
| 52 | + using (var tmp = TempFolder.ForCaller()) |
| 53 | + { |
| 54 | + var path = tmp.AllocateFilename("txt"); |
| 55 | + |
| 56 | + long streamLengthBeforeSetLength; |
| 57 | + long streamLengthAfterSetLength; |
| 58 | + long countedLengthAfterSetLength; |
| 59 | + |
| 60 | + using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)) |
| 61 | + using (var countingStream = new WriteCountingStream(fileStream)) |
| 62 | + using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false))) |
| 63 | + { |
| 64 | + writer.WriteLine("Hello, world!"); |
| 65 | + writer.Flush(); |
| 66 | + |
| 67 | + streamLengthBeforeSetLength = countingStream.CountedLength; |
| 68 | + countingStream.SetLength(100); |
| 69 | + streamLengthAfterSetLength = countingStream.Length; |
| 70 | + countedLengthAfterSetLength = countingStream.CountedLength; |
| 71 | + } |
| 72 | + |
| 73 | + Assert.Equal(100, streamLengthAfterSetLength); |
| 74 | + Assert.Equal(streamLengthBeforeSetLength, countedLengthAfterSetLength); |
| 75 | + |
| 76 | + var lines = System.IO.File.ReadAllLines(path); |
| 77 | + |
| 78 | + Assert.Equal(2, lines.Length); |
| 79 | + Assert.Equal("Hello, world!", lines[0]); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments