diff --git a/v2rayN/ServiceLib.Tests/CoreConfig/Context/CoreConfigContextBuilderTests.cs b/v2rayN/ServiceLib.Tests/CoreConfig/Context/CoreConfigContextBuilderTests.cs index 73688f67..6e776c40 100644 --- a/v2rayN/ServiceLib.Tests/CoreConfig/Context/CoreConfigContextBuilderTests.cs +++ b/v2rayN/ServiceLib.Tests/CoreConfig/Context/CoreConfigContextBuilderTests.cs @@ -98,7 +98,7 @@ public class CoreConfigContextBuilderTests private static async Task UpsertProfilesAsync(params ProfileItem[] profiles) { - SQLiteHelper.Instance.CreateTable(); + TestDatabase.Initialize(); foreach (var profile in profiles) { await SQLiteHelper.Instance.ReplaceAsync(profile); diff --git a/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs b/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs index b0cb07c6..45d42812 100644 --- a/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs +++ b/v2rayN/ServiceLib.Tests/Handler/ConfigHandlerCustomSubscriptionTests.cs @@ -16,8 +16,7 @@ public class ConfigHandlerCustomSubscriptionTests { var config = CoreConfigTestFactory.CreateConfig(); CoreConfigTestFactory.BindAppManagerConfig(config); - SQLiteHelper.Instance.CreateTable(); - SQLiteHelper.Instance.CreateTable(); + TestDatabase.Initialize(); var subId = $"sub-{Guid.NewGuid():N}"; await SQLiteHelper.Instance.ReplaceAsync(new SubItem @@ -50,8 +49,7 @@ public class ConfigHandlerCustomSubscriptionTests { var config = CoreConfigTestFactory.CreateConfig(); CoreConfigTestFactory.BindAppManagerConfig(config); - SQLiteHelper.Instance.CreateTable(); - SQLiteHelper.Instance.CreateTable(); + TestDatabase.Initialize(); var subId = $"sub-failed-{Guid.NewGuid():N}"; await SQLiteHelper.Instance.ReplaceAsync(new SubItem diff --git a/v2rayN/ServiceLib.Tests/Helper/SQLiteHelperTests.cs b/v2rayN/ServiceLib.Tests/Helper/SQLiteHelperTests.cs index ca0c2b3a..36eacc8e 100644 --- a/v2rayN/ServiceLib.Tests/Helper/SQLiteHelperTests.cs +++ b/v2rayN/ServiceLib.Tests/Helper/SQLiteHelperTests.cs @@ -13,6 +13,33 @@ public class SQLiteHelperTests return database; }); + [Test] + public async Task RepeatedSharedSchemaInitialization_ShouldSucceedDuringAnExclusiveTransaction() + { + TestDatabase.Initialize(); + var database = Database.Value; + var item = new SubItem { Id = Guid.NewGuid().ToString("N"), Remarks = "schema-init", Url = "http://127.0.0.1/sub" }; + await database.InsertAsync(item); + try + { + await database.ExecuteAsync("BEGIN EXCLUSIVE"); + try + { + TestDatabase.Initialize(); + var saved = await database.TableAsync().Where(row => row.Id == item.Id).FirstAsync(); + await saved.Id.Should().BeEqualTo(item.Id); + } + finally + { + await database.ExecuteAsync("ROLLBACK"); + } + } + finally + { + await database.DeleteAsync(item); + } + } + [Test] public async Task SingleWrites_ShouldPreserveValuesAndDeleteByPrimaryKey() { diff --git a/v2rayN/ServiceLib.Tests/Manager/ProfileExManagerPersistenceTests.cs b/v2rayN/ServiceLib.Tests/Manager/ProfileExManagerPersistenceTests.cs index fda14d9b..e45dfa8b 100644 --- a/v2rayN/ServiceLib.Tests/Manager/ProfileExManagerPersistenceTests.cs +++ b/v2rayN/ServiceLib.Tests/Manager/ProfileExManagerPersistenceTests.cs @@ -5,7 +5,7 @@ public class ProfileExManagerPersistenceTests [Test] public async Task SaveTo_NewProfilesWithRepeatedResults_ShouldPersistEveryProfile() { - SQLiteHelper.Instance.CreateTable(); + TestDatabase.Initialize(); var manager = new ProfileExManager(); var ids = Enumerable.Range(0, 3).Select(_ => Guid.NewGuid().ToString("N")).ToArray(); @@ -46,7 +46,7 @@ public class ProfileExManagerPersistenceTests [Test] public async Task SaveTo_MixedNewAndExistingProfiles_ShouldInsertAndUpdateResults() { - SQLiteHelper.Instance.CreateTable(); + TestDatabase.Initialize(); var manager = new ProfileExManager(); var existingId = Guid.NewGuid().ToString("N"); var newId = Guid.NewGuid().ToString("N"); diff --git a/v2rayN/ServiceLib.Tests/TestDatabase.cs b/v2rayN/ServiceLib.Tests/TestDatabase.cs new file mode 100644 index 00000000..befd79cf --- /dev/null +++ b/v2rayN/ServiceLib.Tests/TestDatabase.cs @@ -0,0 +1,15 @@ +namespace ServiceLib.Tests; + +internal static class TestDatabase +{ + private static readonly Lazy Database = new(() => + { + var database = SQLiteHelper.Instance; + database.CreateTable(); + database.CreateTable(); + database.CreateTable(); + return database; + }); + + public static void Initialize() => _ = Database.Value; +}