Skip to content

Commit 6f58d0f

Browse files
committed
缓存命中前保留后端校验取消
- BackendAssetService 在读取后端版本缓存前检查调用方取消令牌,避免缓存命中绕过取消。 - BackendAssetServiceTests 覆盖已有版本缓存时取消仍会传播的回归场景。
1 parent 07b18f4 commit 6f58d0f

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/CodexCliPlus.Infrastructure/Backend/BackendAssetService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ private async Task<bool> IsExecutableVersionCurrentAsync(
118118
CancellationToken cancellationToken
119119
)
120120
{
121+
cancellationToken.ThrowIfCancellationRequested();
122+
121123
if (TryReadCachedExecutableVersion(executablePath, out var cachedVersion))
122124
{
123125
return IsExpectedBackendVersion(cachedVersion);

tests/CodexCliPlus.Tests/Backend/BackendAssetServiceTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ public async Task IsExecutableVersionCurrentAsyncPropagatesCallerCancellation()
173173
Assert.IsAssignableFrom<OperationCanceledException>(exception);
174174
}
175175

176+
[Fact]
177+
public async Task IsExecutableVersionCurrentAsyncPropagatesCallerCancellationBeforeCacheHit()
178+
{
179+
var pathService = new TestPathService(_rootDirectory);
180+
await pathService.EnsureCreatedAsync();
181+
var executablePath = Path.Combine(
182+
pathService.Directories.BackendDirectory,
183+
BackendExecutableNames.ManagedExecutableFileName
184+
);
185+
await File.WriteAllTextAsync(executablePath, "managed executable placeholder");
186+
var service = new BackendAssetService(
187+
new HttpClient(),
188+
pathService,
189+
new NullAppLogger(_rootDirectory)
190+
);
191+
await InvokeWriteVersionCacheAsync(service, executablePath, BackendReleaseMetadata.Version);
192+
using var cancellation = new CancellationTokenSource();
193+
cancellation.Cancel();
194+
195+
var exception = await Record.ExceptionAsync(() =>
196+
InvokeIsExecutableVersionCurrentAsync(service, executablePath, cancellation.Token)
197+
);
198+
199+
Assert.IsAssignableFrom<OperationCanceledException>(exception);
200+
}
201+
176202
public void Dispose()
177203
{
178204
if (Directory.Exists(_rootDirectory))

0 commit comments

Comments
 (0)