Repository navigation
Expand file tree
/
Copy pathILCacheTests.cs
More file actions
379 lines (308 loc) · 15.3 KB
/
Copy pathILCacheTests.cs
File metadata and controls
379 lines (308 loc) · 15.3 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using FolderDiffIL4DotNet.Services;
using FolderDiffIL4DotNet.Services.Caching;
using FolderDiffIL4DotNet.Tests.Helpers;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Services.Caching
{
public partial class ILCacheTests : IDisposable
{
private readonly string _tempDir;
private readonly string _cacheDir;
public ILCacheTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), $"ILCacheTests_{Guid.NewGuid():N}");
_cacheDir = Path.Combine(_tempDir, "cache");
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
try { Directory.Delete(_tempDir, recursive: true); } catch { }
}
private string CreateTestFile(string name, string content = "test content")
{
var path = Path.Combine(_tempDir, name);
File.WriteAllText(path, content);
return path;
}
[Fact]
public async Task SetAndGet_MemoryOnly_ReturnsStoredValue()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file = CreateTestFile("test.dll");
var toolLabel = "dotnet-ildasm (version: 1.0.0)";
var ilText = ".assembly Test {}";
await cache.SetILAsync(file, toolLabel, ilText);
var result = await cache.TryGetILAsync(file, toolLabel);
Assert.Equal(ilText, result);
}
[Fact]
public async Task TryGet_NotStored_ReturnsNull()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file = CreateTestFile("test.dll");
var result = await cache.TryGetILAsync(file, "some-tool");
Assert.Null(result);
}
[Fact]
public async Task Set_NullOrEmptyIL_DoesNotStore()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file = CreateTestFile("test.dll");
var toolLabel = "tool";
await cache.SetILAsync(file, toolLabel, null);
Assert.Null(await cache.TryGetILAsync(file, toolLabel));
await cache.SetILAsync(file, toolLabel, string.Empty);
Assert.Null(await cache.TryGetILAsync(file, toolLabel));
}
[Fact]
public async Task DifferentToolLabels_IndependentEntries()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file = CreateTestFile("test.dll");
await cache.SetILAsync(file, "tool-a", "IL from A");
await cache.SetILAsync(file, "tool-b", "IL from B");
Assert.Equal("IL from A", await cache.TryGetILAsync(file, "tool-a"));
Assert.Equal("IL from B", await cache.TryGetILAsync(file, "tool-b"));
}
[Fact]
public async Task DifferentFiles_IndependentEntries()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file1 = CreateTestFile("a.dll", "content-a");
var file2 = CreateTestFile("b.dll", "content-b");
var tool = "tool";
await cache.SetILAsync(file1, tool, "IL-A");
await cache.SetILAsync(file2, tool, "IL-B");
Assert.Equal("IL-A", await cache.TryGetILAsync(file1, tool));
Assert.Equal("IL-B", await cache.TryGetILAsync(file2, tool));
}
[Fact]
public async Task LRU_ExceedsMaxEntries_EvictsOldest()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, ilCacheMaxMemoryEntries: 2);
var tool = "tool";
var file1 = CreateTestFile("1.dll", "c1");
var file2 = CreateTestFile("2.dll", "c2");
var file3 = CreateTestFile("3.dll", "c3");
await cache.SetILAsync(file1, tool, "IL-1");
await cache.SetILAsync(file2, tool, "IL-2");
// file1 is the oldest entry and should be evicted when file3 fills the capacity
// file1 は最も古いエントリであり、file3 追加時に容量超過で退去される
await cache.SetILAsync(file3, tool, "IL-3");
Assert.Null(await cache.TryGetILAsync(file1, tool));
Assert.NotNull(await cache.TryGetILAsync(file2, tool));
Assert.NotNull(await cache.TryGetILAsync(file3, tool));
}
[Fact]
public async Task LRU_AccessRefreshesEntry()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, ilCacheMaxMemoryEntries: 2);
var tool = "tool";
var file1 = CreateTestFile("1.dll", "c1");
var file2 = CreateTestFile("2.dll", "c2");
var file3 = CreateTestFile("3.dll", "c3");
await cache.SetILAsync(file1, tool, "IL-1");
await cache.SetILAsync(file2, tool, "IL-2");
// Access file1 to refresh its LRU position; file2 becomes the oldest
// file1 にアクセスして LRU 位置を更新し、file2 を最古にする
await cache.TryGetILAsync(file1, tool);
// Adding file3 evicts file2 (the oldest) instead of file1
// file3 追加時に file1 ではなく file2(最古)が退去される
await cache.SetILAsync(file3, tool, "IL-3");
Assert.NotNull(await cache.TryGetILAsync(file1, tool));
Assert.Null(await cache.TryGetILAsync(file2, tool));
Assert.NotNull(await cache.TryGetILAsync(file3, tool));
}
[Fact]
public async Task Set_SameKeyAtCapacity_DoesNotEvictOtherEntries()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, ilCacheMaxMemoryEntries: 2);
var tool = "tool";
var file1 = CreateTestFile("1.dll", "c1");
var file2 = CreateTestFile("2.dll", "c2");
await cache.SetILAsync(file1, tool, "IL-1");
await cache.SetILAsync(file2, tool, "IL-2");
await cache.SetILAsync(file1, tool, "IL-1-updated");
Assert.Equal("IL-1-updated", await cache.TryGetILAsync(file1, tool));
Assert.Equal("IL-2", await cache.TryGetILAsync(file2, tool));
Assert.Equal(0, cache.Stats.Evicted);
}
[Fact]
public async Task Stats_TracksEvictions()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, ilCacheMaxMemoryEntries: 1);
var tool = "tool";
var file1 = CreateTestFile("1.dll", "c1");
var file2 = CreateTestFile("2.dll", "c2");
await cache.SetILAsync(file1, tool, "IL-1");
await cache.SetILAsync(file2, tool, "IL-2");
Assert.True(cache.Stats.Evicted >= 1);
}
[Fact]
public async Task TTL_ExpiredEntry_ReturnsNull()
{
// TTL of 1ms ensures the entry expires almost immediately
// TTL 1ms でエントリがほぼ即座に期限切れになることを保証する
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, timeToLive: TimeSpan.FromMilliseconds(1));
var file = CreateTestFile("test.dll");
var tool = "tool";
await cache.SetILAsync(file, tool, "IL text");
// Wait long enough for the 1ms TTL to expire
// 1ms の TTL が確実に満了するまで待機
await Task.Delay(50);
var result = await cache.TryGetILAsync(file, tool);
Assert.Null(result);
}
[Fact]
public async Task TTL_NotExpired_ReturnsValue()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null, timeToLive: TimeSpan.FromMinutes(10));
var file = CreateTestFile("test.dll");
var tool = "tool";
await cache.SetILAsync(file, tool, "IL text");
var result = await cache.TryGetILAsync(file, tool);
Assert.Equal("IL text", result);
}
[Fact]
public async Task DiskCache_SetAndGet_PersistsToFile()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir);
var file = CreateTestFile("test.dll");
var tool = "tool";
await cache.SetILAsync(file, tool, "IL from disk");
// Verify at least one cache file was persisted to disk
// ディスクにキャッシュファイルが永続化されたことを確認
var cacheFiles = Directory.GetFiles(_cacheDir, "*.ilcache");
Assert.NotEmpty(cacheFiles);
}
[Fact]
public async Task DiskCache_NewInstance_ReadsPreviouslyWrittenCache()
{
var file = CreateTestFile("test.dll");
var tool = "tool";
var ilText = "IL persisted across instances";
// Write with the first ILCache instance
// 最初の ILCache インスタンスで書き込み
var cache1 = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir);
await cache1.SetILAsync(file, tool, ilText);
// A fresh instance has an empty memory cache but can read from disk
// 新しいインスタンスはメモリキャッシュが空だがディスクから読み取れる
var cache2 = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir);
var result = await cache2.TryGetILAsync(file, tool);
Assert.Equal(ilText, result);
}
[Fact]
public async Task DiskCache_EmptyCacheFile_TreatsAsMissLogsWarningAndRemovesFile()
{
var logger = new TestLogger();
var file = CreateTestFile("empty-cache.dll");
var tool = "tool";
var writer = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, logger: logger);
await writer.SetILAsync(file, tool, "non-empty IL");
var cacheFile = Assert.Single(Directory.GetFiles(_cacheDir, "*.ilcache"));
await File.WriteAllTextAsync(cacheFile, string.Empty);
var reader = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, logger: logger);
var result = await reader.TryGetILAsync(file, tool);
Assert.Null(result);
Assert.False(File.Exists(cacheFile));
var warning = Assert.Single(logger.Entries, entry => entry.LogLevel == AppLogLevel.Warning);
Assert.Contains("Ignored empty IL cache disk hit", warning.Message, StringComparison.Ordinal);
Assert.Contains("cacheKeyLength=", warning.Message, StringComparison.Ordinal);
Assert.Contains("treated as a miss", warning.Message, StringComparison.Ordinal);
}
[Fact]
public async Task DiskCache_InvalidDirectory_FallsBackToMemoryOnly()
{
// Provide an excessively long path that cannot be created as a directory
// ディレクトリとして作成できない過度に長いパスを指定する
var invalidPath = Path.Combine(_tempDir, new string('x', 300), "cache");
var cache = new ILCache(ilCacheDirectoryAbsolutePath: invalidPath);
var file = CreateTestFile("test.dll");
var tool = "tool";
// Should still work via in-memory fallback when disk init fails
// ディスク初期化失敗時もメモリキャッシュへのフォールバックで動作する
await cache.SetILAsync(file, tool, "memory only");
var result = await cache.TryGetILAsync(file, tool);
Assert.Equal("memory only", result);
}
[Fact]
public async Task DiskQuota_MaxFileCount_RemovesOldFiles()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, ilCacheMaxDiskFileCount: 2);
var tool = "tool";
// Create 3 files sequentially with unique content to produce distinct SHA256 cache keys
// ユニークなコンテンツで 3 ファイルを順次作成し、異なる SHA256 キャッシュキーを生成する
for (int i = 0; i < 3; i++)
{
var file = CreateTestFile($"f{i}.dll", $"unique-content-{i}");
await cache.SetILAsync(file, tool, $"IL-{i}");
// Small delay so file timestamps differ for quota enforcement ordering
// クォータ適用時の順序付けのためファイルのタイムスタンプを異ならせる
await Task.Delay(50);
}
var cacheFiles = Directory.GetFiles(_cacheDir, "*.ilcache");
Assert.True(cacheFiles.Length <= 2, $"Expected <= 2 cache files, got {cacheFiles.Length}");
}
[Fact]
public async Task LRU_WithDiskCache_RemovesEvictedDiskEntry()
{
var tool = "tool";
var file1 = CreateTestFile("disk-1.dll", "disk-c1");
var file2 = CreateTestFile("disk-2.dll", "disk-c2");
var cache = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, ilCacheMaxMemoryEntries: 1);
await cache.SetILAsync(file1, tool, "IL-1");
await Task.Delay(50);
await cache.SetILAsync(file2, tool, "IL-2");
var cacheFiles = Directory.GetFiles(_cacheDir, "*.ilcache");
Assert.Single(cacheFiles);
var freshCache = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, ilCacheMaxMemoryEntries: 1);
Assert.Null(await freshCache.TryGetILAsync(file1, tool));
Assert.Equal("IL-2", await freshCache.TryGetILAsync(file2, tool));
}
[Fact]
public async Task DiskQuota_MaxMegabytes_RemovesOldFiles()
{
// 0 MB limit means unlimited; verify file is still written
// 0 MB 制限は無制限を意味し、ファイルが書き込まれることを確認
var cache = new ILCache(ilCacheDirectoryAbsolutePath: _cacheDir, ilCacheMaxDiskMegabytes: 0);
var file = CreateTestFile("test.dll");
var tool = "tool";
var largeIL = new string('X', 2048);
await cache.SetILAsync(file, tool, largeIL);
var cacheFiles = Directory.GetFiles(_cacheDir, "*.ilcache");
Assert.NotEmpty(cacheFiles);
}
[Fact]
public async Task Precompute_ValidFiles_DoesNotThrow()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file1 = CreateTestFile("a.dll", "content-a");
var file2 = CreateTestFile("b.dll", "content-b");
await cache.PrecomputeAsync(new[] { file1, file2 }, maxParallel: 2);
}
[Fact]
public async Task Precompute_NullEnumerable_DoesNotThrow()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
await cache.PrecomputeAsync(null, maxParallel: 1);
}
[Fact]
public async Task Precompute_EmptyEnumerable_DoesNotThrow()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
await cache.PrecomputeAsync(Array.Empty<string>(), maxParallel: 1);
}
[Fact]
public async Task Precompute_ZeroParallel_ThrowsArgumentOutOfRange()
{
var cache = new ILCache(ilCacheDirectoryAbsolutePath: null);
var file = CreateTestFile("test.dll");
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(
() => cache.PrecomputeAsync(new[] { file }, maxParallel: 0));
}
}
}