/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/VisualStudio/CSharp/Test/PersistentStorage/SQLiteV2PersistentStorageTests.cs
97 строк
4 KB
Jan Jones
Use SQLite3MC (#84140)
24 июн 2026, 14:20
Не верифицирован
24 июн 2026, 14:20
0288255
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Host; using Xunit; namespace Microsoft.CodeAnalysis.UnitTests.WorkspaceServices; /// <remarks> /// Tests are inherited from <see cref="AbstractPersistentStorageTests"/>. That way we can /// write tests once and have them run against all <see cref="IPersistentStorageService"/> /// implementations. /// </remarks> public sealed class SQLiteV2PersistentStorageTests : AbstractPersistentStorageTests { static SQLiteV2PersistentStorageTests() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var runtimeIdentifier = Environment.Is64BitProcess ? "win-x64" : "win-x86"; var sqlite3mcPath = Path.Combine(AppContext.BaseDirectory, "runtimes", runtimeIdentifier, "native", "sqlite3mc.dll"); if (File.Exists(sqlite3mcPath) && LoadLibrary(sqlite3mcPath) == IntPtr.Zero) throw new Win32Exception(Marshal.GetLastWin32Error(), $"Failed to load '{sqlite3mcPath}'."); } } [Fact] public async Task TestCrashInNewConnection() { var solution = CreateOrOpenSolution(nullPaths: true); var hitInjector = false; var faultInjector = new PersistentStorageFaultInjector( onNewConnection: () => { hitInjector = true; throw new Exception(); }, onFatalError: e => throw e); // Because instantiating the connection will fail, we will not get back // a working persistent storage. We are testing a fault recovery code path. var storage = await GetStorageAsync(solution, faultInjector: faultInjector, throwOnFailure: false); using (var memStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memStream)) { streamWriter.WriteLine("contents"); streamWriter.Flush(); memStream.Position = 0; await storage.WriteStreamAsync("temp", memStream); var readStream = await storage.ReadStreamAsync("temp"); // Because we don't have a real storage service, we should get back // null even when trying to read something we just wrote. Assert.Null(readStream); } Assert.True(hitInjector); // Ensure we don't get a crash due to SqlConnection's finalizer running. for (var i = 0; i < 10; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); } } private sealed class PersistentStorageFaultInjector : IPersistentStorageFaultInjector { private readonly Action? _onNewConnection; private readonly Action<Exception>? _onFatalError; public PersistentStorageFaultInjector( Action? onNewConnection = null, Action<Exception>? onFatalError = null) { _onNewConnection = onNewConnection; _onFatalError = onFatalError; } public void OnNewConnection() => _onNewConnection?.Invoke(); public void OnFatalError(Exception ex) => _onFatalError?.Invoke(ex); } [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibrary(string lpFileName); }