Repository navigation
Expand file tree
/
Copy pathLargeFileComparisonTests.cs
More file actions
291 lines (252 loc) · 13.7 KB
/
Copy pathLargeFileComparisonTests.cs
File metadata and controls
291 lines (252 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FolderDiffIL4DotNet.Core.Diagnostics;
using FolderDiffIL4DotNet.Models;
using FolderDiffIL4DotNet.Services;
using FolderDiffIL4DotNet.Tests.Helpers;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Services.EdgeCases
{
/// <summary>
/// Tests for ultra-large file comparison scenarios.
/// Verifies that multi-MiB files are handled correctly via both sequential and chunk-parallel paths.
/// 超大規模ファイル比較のシナリオテスト。
/// 数 MiB のファイルが逐次およびチャンク並列パスの両方で正しく処理されることを確認する。
/// </summary>
[Trait("Category", "Unit")]
public sealed class LargeFileComparisonTests
{
[Fact]
public async Task FilesAreEqualAsync_LargeIdenticalTextFiles_ComparedViaChunkParallel_ReturnsTrue()
{
// 4 MiB identical text files compared via parallel chunk comparison
// 4 MiB の同一テキストファイルを並列チャンク比較で処理
const string relativePath = "large-identical.txt";
const int fileSize = 4 * 1024 * 1024; // 4 MiB
var content = new string('A', fileSize);
var fileComparisonService = new FakeFileComparisonService
{
HashResult = false,
DotNetDetectionResult = new DotNetExecutableDetectionResult(DotNetExecutableDetectionStatus.NotDotNetExecutable)
};
var oldPath = Path.Combine("/virtual/old", relativePath);
var newPath = Path.Combine("/virtual/new", relativePath);
fileComparisonService.SetFileContent(oldPath, content);
fileComparisonService.SetFileContent(newPath, content);
var resultLists = new FileDiffResultLists();
var logger = new TestLogger();
var service = CreateService(fileComparisonService, resultLists, logger,
config =>
{
config.TextDiffParallelThresholdKilobytes = 64;
config.TextDiffChunkSizeKilobytes = 256;
});
var areEqual = await service.FilesAreEqualAsync(relativePath, maxParallel: 4);
Assert.True(areEqual);
Assert.Equal(FileDiffResultLists.DiffDetailResult.TextMatch,
resultLists.FileRelativePathToDiffDetailDictionary[relativePath]);
Assert.NotEmpty(fileComparisonService.ReadChunkCalls);
}
[Fact]
public async Task FilesAreEqualAsync_LargeDifferingTextFiles_DiffDetectedViaChunkParallel()
{
// 4 MiB text files with a single byte difference at the end
// ファイル末尾に 1 バイトの違いがある 4 MiB テキストファイル
const string relativePath = "large-differ-tail.txt";
const int fileSize = 4 * 1024 * 1024;
var contentA = new string('X', fileSize);
var contentB = new string('X', fileSize - 1) + "Y";
var fileComparisonService = new FakeFileComparisonService
{
HashResult = false,
DotNetDetectionResult = new DotNetExecutableDetectionResult(DotNetExecutableDetectionStatus.NotDotNetExecutable)
};
var oldPath = Path.Combine("/virtual/old", relativePath);
var newPath = Path.Combine("/virtual/new", relativePath);
fileComparisonService.SetFileContent(oldPath, contentA);
fileComparisonService.SetFileContent(newPath, contentB);
var resultLists = new FileDiffResultLists();
var logger = new TestLogger();
var service = CreateService(fileComparisonService, resultLists, logger,
config =>
{
config.TextDiffParallelThresholdKilobytes = 64;
config.TextDiffChunkSizeKilobytes = 256;
});
var areEqual = await service.FilesAreEqualAsync(relativePath, maxParallel: 4);
Assert.False(areEqual);
Assert.Equal(FileDiffResultLists.DiffDetailResult.TextMismatch,
resultLists.FileRelativePathToDiffDetailDictionary[relativePath]);
}
[Fact]
public async Task FilesAreEqualAsync_LargeFilesWithDifferentSizes_ReturnsNotEqual()
{
// Files of different sizes should be detected as different
// 異なるサイズのファイルは異なるとして検出される
const string relativePath = "size-mismatch.txt";
var contentA = new string('Z', 2 * 1024 * 1024);
var contentB = new string('Z', 3 * 1024 * 1024);
var fileComparisonService = new FakeFileComparisonService
{
HashResult = false,
DotNetDetectionResult = new DotNetExecutableDetectionResult(DotNetExecutableDetectionStatus.NotDotNetExecutable)
};
var oldPath = Path.Combine("/virtual/old", relativePath);
var newPath = Path.Combine("/virtual/new", relativePath);
fileComparisonService.SetFileContent(oldPath, contentA);
fileComparisonService.SetFileContent(newPath, contentB);
var resultLists = new FileDiffResultLists();
var logger = new TestLogger();
var service = CreateService(fileComparisonService, resultLists, logger,
config =>
{
config.TextDiffParallelThresholdKilobytes = 64;
config.TextDiffChunkSizeKilobytes = 256;
});
var areEqual = await service.FilesAreEqualAsync(relativePath, maxParallel: 4);
Assert.False(areEqual);
}
[Fact]
public async Task FilesAreEqualAsync_EmptyFiles_ReturnsTrueViaSHA256()
{
// Empty files should match via SHA256 hash comparison
// 空ファイルは SHA256 ハッシュ比較で一致する
const string relativePath = "empty.txt";
var fileComparisonService = new FakeFileComparisonService
{
HashResult = true
};
var resultLists = new FileDiffResultLists();
var logger = new TestLogger();
var service = CreateService(fileComparisonService, resultLists, logger);
var areEqual = await service.FilesAreEqualAsync(relativePath, maxParallel: 1);
Assert.True(areEqual);
Assert.Equal(FileDiffResultLists.DiffDetailResult.SHA256Match,
resultLists.FileRelativePathToDiffDetailDictionary[relativePath]);
}
[Fact]
public async Task FilesAreEqualAsync_VerySmallChunkSize_ManyChunksStillCompareCorrectly()
{
// 64 KB files with 1 KB chunk size = 64 chunks; tests chunk boundary correctness
// 64 KB ファイルを 1 KB チャンクで分割 = 64 チャンク; チャンク境界の正確性をテスト
const string relativePath = "many-chunks.txt";
const int fileSize = 64 * 1024;
var content = new string('M', fileSize);
var fileComparisonService = new FakeFileComparisonService
{
HashResult = false,
DotNetDetectionResult = new DotNetExecutableDetectionResult(DotNetExecutableDetectionStatus.NotDotNetExecutable)
};
var oldPath = Path.Combine("/virtual/old", relativePath);
var newPath = Path.Combine("/virtual/new", relativePath);
fileComparisonService.SetFileContent(oldPath, content);
fileComparisonService.SetFileContent(newPath, content);
var resultLists = new FileDiffResultLists();
var logger = new TestLogger();
var service = CreateService(fileComparisonService, resultLists, logger,
config =>
{
config.TextDiffParallelThresholdKilobytes = 1;
config.TextDiffChunkSizeKilobytes = 1;
});
var areEqual = await service.FilesAreEqualAsync(relativePath, maxParallel: 8);
Assert.True(areEqual);
Assert.True(fileComparisonService.ReadChunkCalls.Count >= 64,
$"Expected >= 64 chunk reads, got {fileComparisonService.ReadChunkCalls.Count}");
}
private static FileDiffService CreateService(
FakeFileComparisonService fileComparisonService,
FileDiffResultLists resultLists,
TestLogger logger,
Action<ConfigSettingsBuilder>? configure = null)
{
var builder = new ConfigSettingsBuilder
{
TextFileExtensions = new List<string> { ".txt" },
IgnoredExtensions = new List<string>(),
ShouldOutputILText = false,
EnableILCache = false,
OptimizeForNetworkShares = false,
TextDiffParallelThresholdKilobytes = ConfigSettings.DefaultTextDiffParallelThresholdKilobytes,
TextDiffChunkSizeKilobytes = ConfigSettings.DefaultTextDiffChunkSizeKilobytes,
TextDiffParallelMemoryLimitMegabytes = 0,
ShouldTreatTextByteDifferencesAsMismatch = false
};
configure?.Invoke(builder);
var config = builder.Build();
var executionContext = new DiffExecutionContext(
"/virtual/old", "/virtual/new", "/virtual/report",
optimizeForNetworkShares: false, detectedNetworkOld: false, detectedNetworkNew: false);
var ilOutputService = new FakeILOutputService();
return new FileDiffService(config, ilOutputService, executionContext, resultLists, logger, fileComparisonService);
}
private sealed class FakeFileComparisonService : IFileComparisonService
{
private readonly Dictionary<string, byte[]> _contentsByPath = new(StringComparer.OrdinalIgnoreCase);
public bool HashResult { get; set; }
public Exception HashException { get; set; }
public bool TextDiffResult { get; set; }
public Exception TextDiffException { get; set; }
public DotNetExecutableDetectionResult DotNetDetectionResult { get; set; } =
new(DotNetExecutableDetectionStatus.NotDotNetExecutable);
public ConcurrentBag<(string, string)> HashCalls { get; } = new();
public ConcurrentBag<string> DotNetDetectionCalls { get; } = new();
public ConcurrentBag<(string, string)> TextDiffCalls { get; } = new();
public ConcurrentBag<(string Path, long Offset, int Length)> ReadChunkCalls { get; } = new();
public void SetFileContent(string path, string content)
=> _contentsByPath[path] = System.Text.Encoding.UTF8.GetBytes(content);
public Task<bool> DiffFilesByHashAsync(string file1, string file2)
{
HashCalls.Add((file1, file2));
return HashException != null ? throw HashException : Task.FromResult(HashResult);
}
public Task<(bool AreEqual, string? Hash1Hex, string? Hash2Hex)> DiffFilesByHashWithHexAsync(
string file1, string file2)
{
HashCalls.Add((file1, file2));
if (HashException != null) throw HashException;
string? hash1 = HashResult ? "a".PadRight(64, '0') : "a".PadRight(64, '0');
string? hash2 = HashResult ? "a".PadRight(64, '0') : "b".PadRight(64, '0');
return Task.FromResult((HashResult, hash1, hash2));
}
public Task<bool> DiffTextFilesAsync(string file1, string file2)
{
TextDiffCalls.Add((file1, file2));
return TextDiffException != null ? throw TextDiffException : Task.FromResult(TextDiffResult);
}
public DotNetExecutableDetectionResult DetectDotNetExecutable(string path)
{
DotNetDetectionCalls.Add(path);
return DotNetDetectionResult;
}
public bool FileExists(string path) => _contentsByPath.ContainsKey(path);
public long GetFileLength(string path)
{
if (_contentsByPath.TryGetValue(path, out var c)) return c.LongLength;
throw new FileNotFoundException($"Not found: {path}", path);
}
public Task<int> ReadChunkAsync(string path, long offset, Memory<byte> buffer, System.Threading.CancellationToken ct)
{
ReadChunkCalls.Add((path, offset, buffer.Length));
if (!_contentsByPath.TryGetValue(path, out var content))
throw new FileNotFoundException($"Not found: {path}", path);
int start = checked((int)offset);
if (start >= content.Length) return Task.FromResult(0);
int count = Math.Min(buffer.Length, content.Length - start);
content.AsMemory(start, count).CopyTo(buffer);
return Task.FromResult(count);
}
}
private sealed class FakeILOutputService : IILOutputService
{
public Task PrecomputeAsync(System.Collections.Generic.IEnumerable<string> filesAbsolutePaths, int maxParallel, System.Threading.CancellationToken ct = default)
=> Task.CompletedTask;
public void PreSeedFileHash(string fileAbsolutePath, string sha256Hex) { }
public Task<(bool AreEqual, string? DisassemblerLabel)> DiffDotNetAssembliesAsync(string fileRelativePath, string oldFolder, string newFolder, bool shouldOutput, System.Threading.CancellationToken ct = default)
=> Task.FromResult((false, (string?)null));
}
}
}