From ae8b0e04d41ced1e3527008f3bc36c514dd5d1e0 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Wed, 6 Nov 2024 16:15:44 -0800 Subject: [PATCH 01/12] Added end2end test for changes in datasource --- .../HotReload/ConfigurationHotReloadTests.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index de199a31e6..050ae763c0 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -299,4 +299,36 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() // Assert Assert.AreEqual(HttpStatusCode.NotFound, gQLResult.StatusCode); } + + /// + /// Hot reload the configuration file by saving a new database type and connection string. + /// Validate that the response from the server is correct when making a new request after + /// the change in database type. + /// + [DataTestMethod] + [DataRow(DatabaseType.MySQL, TestCategory.MYSQL, DisplayName = "Hot-reload change to MySQL Datasource")] + [DataRow(DatabaseType.PostgreSQL, TestCategory.POSTGRESQL, DisplayName = "Hot-reload change to PostgreSQL Datasource")] + public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseType expectedDatabaseType, string expectedTestCategory) + { + // Arrange + string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(expectedTestCategory).Replace("\\", "\\\\")}"; + + GenerateConfigFile( + databaseType: expectedDatabaseType, + connectionString: expectedConnectionString); + System.Threading.Thread.Sleep(3000); + + // Act + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; + JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( + _testClient, + _configProvider, + GQL_QUERY_NAME, + GQL_QUERY); + + // Assert + Assert.AreEqual(expectedDatabaseType, actualDatabaseType); + SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); + } } From a15484588ed5c261f2d627a052bf09a3090981f8 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Wed, 6 Nov 2024 17:30:00 -0800 Subject: [PATCH 02/12] Changes from comments --- .../HotReload/ConfigurationHotReloadTests.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 050ae763c0..f9206d9bcb 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -311,23 +311,28 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseType expectedDatabaseType, string expectedTestCategory) { // Arrange + RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); + DatabaseType previousDatabaseType = previousRuntimeConfig.DataSource.DatabaseType; string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(expectedTestCategory).Replace("\\", "\\\\")}"; + // Act GenerateConfigFile( databaseType: expectedDatabaseType, connectionString: expectedConnectionString); System.Threading.Thread.Sleep(3000); - // Act - RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; + // Await allows the server to hot-reload before sending the message since it has to wait + // until the hot-reload is finished before being able to handle any requests. JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( _testClient, _configProvider, GQL_QUERY_NAME, GQL_QUERY); + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; // Assert + Assert.AreNotEqual(previousDatabaseType, actualDatabaseType); Assert.AreEqual(expectedDatabaseType, actualDatabaseType); SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); } From f0fb7124971a363c2bf9f30bce38e74bc74b86ab Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Tue, 12 Nov 2024 14:25:25 -0800 Subject: [PATCH 03/12] Comment changes --- src/Config/FileSystemRuntimeConfigLoader.cs | 18 +++++++ .../HotReload/ConfigurationHotReloadTests.cs | 50 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/Config/FileSystemRuntimeConfigLoader.cs b/src/Config/FileSystemRuntimeConfigLoader.cs index 7528bf0e52..c6829f8698 100644 --- a/src/Config/FileSystemRuntimeConfigLoader.cs +++ b/src/Config/FileSystemRuntimeConfigLoader.cs @@ -75,6 +75,11 @@ public class FileSystemRuntimeConfigLoader : RuntimeConfigLoader /// public string ConfigFilePath { get; internal set; } + /// + /// Orchestrates sending HotReloadCompleted events to all its subscribers + /// + public event EventHandler? HotReloadCompleted; + public FileSystemRuntimeConfigLoader( IFileSystem fileSystem, HotReloadEventHandler? handler = null, @@ -154,6 +159,16 @@ private bool TrySetupConfigFileWatcher() return false; } + /// + /// Raises the HotReloadCompleted event, which signals to listeners + /// that hot-reload has finished. It does not matter if hot-reload + /// succeeded or failed, the event will still be raised. + /// + protected virtual void OnHotReloadCompleted() + { + HotReloadCompleted?.Invoke(this, EventArgs.Empty); + } + /// /// When a change is detected in the Config file being watched this trigger /// function is called and handles the hot reload logic when appropriate, @@ -174,6 +189,9 @@ private void OnNewFileContentsDetected(object? sender, EventArgs e) // before we can have an ILogger here. Console.WriteLine("Unable to hot reload configuration file due to " + ex.Message); } + + // Event + OnHotReloadCompleted(); } /// diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index f9206d9bcb..112f16facc 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -1,12 +1,14 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Threading.Tasks; +using Azure.DataApiBuilder.Config; using Azure.DataApiBuilder.Config.ObjectModel; using Azure.DataApiBuilder.Core.Configurations; using Azure.DataApiBuilder.Service.Tests.SqlTests; @@ -23,8 +25,11 @@ public class ConfigurationHotReloadTests private static TestServer _testServer; private static HttpClient _testClient; private static RuntimeConfigProvider _configProvider; + private static FileSystemRuntimeConfigLoader _configLoader; internal const string CONFIG_FILE_NAME = "hot-reload.dab-config.json"; internal const string GQL_QUERY_NAME = "books"; + private static TaskCompletionSource _tsc; + private static Task _task; internal const string GQL_QUERY = @"{ books(first: 100) { @@ -162,6 +167,9 @@ public static async Task ClassInitializeAsync(TestContext context) _testServer = new(Program.CreateWebHostBuilder(new string[] { "--ConfigFileName", CONFIG_FILE_NAME })); _testClient = _testServer.CreateClient(); _configProvider = _testServer.Services.GetService(); + _configLoader = _testServer.Services.GetService(); + + _configLoader.HotReloadCompleted += OnHotReloadCompleted; string query = GQL_QUERY; object payload = @@ -311,6 +319,7 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseType expectedDatabaseType, string expectedTestCategory) { // Arrange + StartTaskCompletionOperation(); RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); DatabaseType previousDatabaseType = previousRuntimeConfig.DataSource.DatabaseType; string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(expectedTestCategory).Replace("\\", "\\\\")}"; @@ -319,21 +328,52 @@ public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseTyp GenerateConfigFile( databaseType: expectedDatabaseType, connectionString: expectedConnectionString); - System.Threading.Thread.Sleep(3000); + await _task; - // Await allows the server to hot-reload before sending the message since it has to wait - // until the hot-reload is finished before being able to handle any requests. + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( _testClient, _configProvider, GQL_QUERY_NAME, GQL_QUERY); - RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; // Assert Assert.AreNotEqual(previousDatabaseType, actualDatabaseType); Assert.AreEqual(expectedDatabaseType, actualDatabaseType); SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); } + + /// + /// + /// + public async Task HotReloadConfigConnectionStringEndToEndTest() + { + // Arrange + StartTaskCompletionOperation(); + + // Act + + // Assert + } + + /// + /// Helper function that sets up an empty Task property + /// + public static void StartTaskCompletionOperation() + { + _tsc = new TaskCompletionSource(); + _task = _tsc.Task; + } + + /// + /// Helper function that is used when event HotReloadCompleted is triggered. + /// It sets the empty Task property as if the task it is doing is finished, + /// this mimics an async task so that we can wait for the hot-reload to finish + /// before doing anything else. + /// + private static void OnHotReloadCompleted(object sender, EventArgs e) + { + _tsc.SetResult(true); + } } From e95037bfa2e044dcf7371c5be2cef6af60e7acf8 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Tue, 12 Nov 2024 14:35:02 -0800 Subject: [PATCH 04/12] Changes --- .../HotReload/ConfigurationHotReloadTests.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 112f16facc..15b4912674 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -344,19 +344,6 @@ public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseTyp SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); } - /// - /// - /// - public async Task HotReloadConfigConnectionStringEndToEndTest() - { - // Arrange - StartTaskCompletionOperation(); - - // Act - - // Assert - } - /// /// Helper function that sets up an empty Task property /// From 573ffa78d90b8e089dc58a761d0d958adcccde09 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 18:38:29 -0800 Subject: [PATCH 05/12] Changes --- src/Config/FileSystemRuntimeConfigLoader.cs | 18 ------ .../HotReload/ConfigurationHotReloadTests.cs | 57 ++++++------------- 2 files changed, 16 insertions(+), 59 deletions(-) diff --git a/src/Config/FileSystemRuntimeConfigLoader.cs b/src/Config/FileSystemRuntimeConfigLoader.cs index c6829f8698..7528bf0e52 100644 --- a/src/Config/FileSystemRuntimeConfigLoader.cs +++ b/src/Config/FileSystemRuntimeConfigLoader.cs @@ -75,11 +75,6 @@ public class FileSystemRuntimeConfigLoader : RuntimeConfigLoader /// public string ConfigFilePath { get; internal set; } - /// - /// Orchestrates sending HotReloadCompleted events to all its subscribers - /// - public event EventHandler? HotReloadCompleted; - public FileSystemRuntimeConfigLoader( IFileSystem fileSystem, HotReloadEventHandler? handler = null, @@ -159,16 +154,6 @@ private bool TrySetupConfigFileWatcher() return false; } - /// - /// Raises the HotReloadCompleted event, which signals to listeners - /// that hot-reload has finished. It does not matter if hot-reload - /// succeeded or failed, the event will still be raised. - /// - protected virtual void OnHotReloadCompleted() - { - HotReloadCompleted?.Invoke(this, EventArgs.Empty); - } - /// /// When a change is detected in the Config file being watched this trigger /// function is called and handles the hot reload logic when appropriate, @@ -189,9 +174,6 @@ private void OnNewFileContentsDetected(object? sender, EventArgs e) // before we can have an ILogger here. Console.WriteLine("Unable to hot reload configuration file due to " + ex.Message); } - - // Event - OnHotReloadCompleted(); } /// diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 15b4912674..df2a0bcbb5 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -25,12 +25,9 @@ public class ConfigurationHotReloadTests private static TestServer _testServer; private static HttpClient _testClient; private static RuntimeConfigProvider _configProvider; - private static FileSystemRuntimeConfigLoader _configLoader; internal const string CONFIG_FILE_NAME = "hot-reload.dab-config.json"; internal const string GQL_QUERY_NAME = "books"; - private static TaskCompletionSource _tsc; - private static Task _task; - + internal const string GQL_QUERY = @"{ books(first: 100) { items { @@ -45,6 +42,7 @@ public class ConfigurationHotReloadTests private static void GenerateConfigFile( DatabaseType databaseType = DatabaseType.MSSQL, + string sessionContext = "true", string connectionString = "", string restPath = "rest", string restEnabled = "true", @@ -66,7 +64,7 @@ private static void GenerateConfigFile( ""data-source"": { ""database-type"": """ + databaseType + @""", ""options"": { - ""set-session-context"": true + ""set-session-context"": """ + sessionContext + @""" }, ""connection-string"": """ + connectionString + @""" }, @@ -167,10 +165,7 @@ public static async Task ClassInitializeAsync(TestContext context) _testServer = new(Program.CreateWebHostBuilder(new string[] { "--ConfigFileName", CONFIG_FILE_NAME })); _testClient = _testServer.CreateClient(); _configProvider = _testServer.Services.GetService(); - _configLoader = _testServer.Services.GetService(); - - _configLoader.HotReloadCompleted += OnHotReloadCompleted; - + string query = GQL_QUERY; object payload = new { query }; @@ -313,25 +308,24 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() /// Validate that the response from the server is correct when making a new request after /// the change in database type. /// - [DataTestMethod] - [DataRow(DatabaseType.MySQL, TestCategory.MYSQL, DisplayName = "Hot-reload change to MySQL Datasource")] - [DataRow(DatabaseType.PostgreSQL, TestCategory.POSTGRESQL, DisplayName = "Hot-reload change to PostgreSQL Datasource")] - public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseType expectedDatabaseType, string expectedTestCategory) + [TestMethod] + public async Task HotReloadConfigDataSourceEndToEndTest() { // Arrange - StartTaskCompletionOperation(); RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); - DatabaseType previousDatabaseType = previousRuntimeConfig.DataSource.DatabaseType; - string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(expectedTestCategory).Replace("\\", "\\\\")}"; + MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); + + // String has additions that are not in original connection string + string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; // Act GenerateConfigFile( - databaseType: expectedDatabaseType, + sessionContext: "false", connectionString: expectedConnectionString); - await _task; + System.Threading.Thread.Sleep(3000); RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - DatabaseType actualDatabaseType = updatedRuntimeConfig.DataSource.DatabaseType; + MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( _testClient, _configProvider, @@ -339,28 +333,9 @@ public async Task HotReloadConfigDatasourceDatabaseTypesEndToEndTest(DatabaseTyp GQL_QUERY); // Assert - Assert.AreNotEqual(previousDatabaseType, actualDatabaseType); - Assert.AreEqual(expectedDatabaseType, actualDatabaseType); + // If the assert succeeds it implies that the connection string is hot-reloadable + Assert.AreNotEqual(previousSessionContext, actualSessionContext); + Assert.AreEqual(false, actualSessionContext.SetSessionContext); SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); } - - /// - /// Helper function that sets up an empty Task property - /// - public static void StartTaskCompletionOperation() - { - _tsc = new TaskCompletionSource(); - _task = _tsc.Task; - } - - /// - /// Helper function that is used when event HotReloadCompleted is triggered. - /// It sets the empty Task property as if the task it is doing is finished, - /// this mimics an async task so that we can wait for the hot-reload to finish - /// before doing anything else. - /// - private static void OnHotReloadCompleted(object sender, EventArgs e) - { - _tsc.SetResult(true); - } } From a85f8cd8f33c6116e83e4c75c1250e0c9d066842 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 19:27:16 -0800 Subject: [PATCH 06/12] Changes --- .../Configuration/HotReload/ConfigurationHotReloadTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index df2a0bcbb5..494bb52943 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -1,14 +1,12 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Threading.Tasks; -using Azure.DataApiBuilder.Config; using Azure.DataApiBuilder.Config.ObjectModel; using Azure.DataApiBuilder.Core.Configurations; using Azure.DataApiBuilder.Service.Tests.SqlTests; From 8dfaa5cfb9e77edda94477ac2da00b01d81d050a Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 20:39:00 -0800 Subject: [PATCH 07/12] Changes --- .../Configuration/HotReload/ConfigurationHotReloadTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 494bb52943..2a7a8c5f7a 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -40,7 +40,6 @@ public class ConfigurationHotReloadTests private static void GenerateConfigFile( DatabaseType databaseType = DatabaseType.MSSQL, - string sessionContext = "true", string connectionString = "", string restPath = "rest", string restEnabled = "true", @@ -62,7 +61,7 @@ private static void GenerateConfigFile( ""data-source"": { ""database-type"": """ + databaseType + @""", ""options"": { - ""set-session-context"": """ + sessionContext + @""" + ""set-session-context"": true }, ""connection-string"": """ + connectionString + @""" }, From 995762b3afd5f74e8a762167a3dbbbcb88bb2634 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 20:52:06 -0800 Subject: [PATCH 08/12] Fixed Errors --- .../HotReload/ConfigurationHotReloadTests.cs | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 2a7a8c5f7a..e74079c624 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -25,7 +25,7 @@ public class ConfigurationHotReloadTests private static RuntimeConfigProvider _configProvider; internal const string CONFIG_FILE_NAME = "hot-reload.dab-config.json"; internal const string GQL_QUERY_NAME = "books"; - + internal const string GQL_QUERY = @"{ books(first: 100) { items { @@ -40,6 +40,7 @@ public class ConfigurationHotReloadTests private static void GenerateConfigFile( DatabaseType databaseType = DatabaseType.MSSQL, + string sessionContext = "true", string connectionString = "", string restPath = "rest", string restEnabled = "true", @@ -61,7 +62,7 @@ private static void GenerateConfigFile( ""data-source"": { ""database-type"": """ + databaseType + @""", ""options"": { - ""set-session-context"": true + ""set-session-context"": " + sessionContext + @" }, ""connection-string"": """ + connectionString + @""" }, @@ -162,7 +163,7 @@ public static async Task ClassInitializeAsync(TestContext context) _testServer = new(Program.CreateWebHostBuilder(new string[] { "--ConfigFileName", CONFIG_FILE_NAME })); _testClient = _testServer.CreateClient(); _configProvider = _testServer.Services.GetService(); - + string query = GQL_QUERY; object payload = new { query }; @@ -305,34 +306,34 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() /// Validate that the response from the server is correct when making a new request after /// the change in database type. /// - [TestMethod] - public async Task HotReloadConfigDataSourceEndToEndTest() - { - // Arrange - RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); - - // String has additions that are not in original connection string - string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; + [TestMethod] + public async Task HotReloadConfigDataSourceEndToEndTest() + { + // Arrange + RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); - // Act - GenerateConfigFile( - sessionContext: "false", - connectionString: expectedConnectionString); - System.Threading.Thread.Sleep(3000); + // String has additions that are not in original connection string + string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; - RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); - JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( - _testClient, - _configProvider, - GQL_QUERY_NAME, - GQL_QUERY); + // Act + GenerateConfigFile( + sessionContext: "false", + connectionString: expectedConnectionString); + System.Threading.Thread.Sleep(3000); - // Assert - // If the assert succeeds it implies that the connection string is hot-reloadable - Assert.AreNotEqual(previousSessionContext, actualSessionContext); - Assert.AreEqual(false, actualSessionContext.SetSessionContext); - SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); - } + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); + JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( + _testClient, + _configProvider, + GQL_QUERY_NAME, + GQL_QUERY); + + // Assert + // If the assert succeeds it implies that the connection string is hot-reloadable + Assert.AreNotEqual(previousSessionContext, actualSessionContext); + Assert.AreEqual(false, actualSessionContext.SetSessionContext); + SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); + } } From 62642edee88a6d3611fcf02138b40311a33e6de2 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 21:00:27 -0800 Subject: [PATCH 09/12] Changes --- .../HotReload/ConfigurationHotReloadTests.cs | 39 +------------------ 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index e74079c624..44d3265a34 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -40,7 +40,6 @@ public class ConfigurationHotReloadTests private static void GenerateConfigFile( DatabaseType databaseType = DatabaseType.MSSQL, - string sessionContext = "true", string connectionString = "", string restPath = "rest", string restEnabled = "true", @@ -62,7 +61,7 @@ private static void GenerateConfigFile( ""data-source"": { ""database-type"": """ + databaseType + @""", ""options"": { - ""set-session-context"": " + sessionContext + @" + ""set-session-context"": true }, ""connection-string"": """ + connectionString + @""" }, @@ -301,39 +300,5 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() Assert.AreEqual(HttpStatusCode.NotFound, gQLResult.StatusCode); } - /// - /// Hot reload the configuration file by saving a new database type and connection string. - /// Validate that the response from the server is correct when making a new request after - /// the change in database type. - /// - [TestMethod] - public async Task HotReloadConfigDataSourceEndToEndTest() - { - // Arrange - RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); - - // String has additions that are not in original connection string - string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; - - // Act - GenerateConfigFile( - sessionContext: "false", - connectionString: expectedConnectionString); - System.Threading.Thread.Sleep(3000); - - RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); - JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( - _testClient, - _configProvider, - GQL_QUERY_NAME, - GQL_QUERY); - - // Assert - // If the assert succeeds it implies that the connection string is hot-reloadable - Assert.AreNotEqual(previousSessionContext, actualSessionContext); - Assert.AreEqual(false, actualSessionContext.SetSessionContext); - SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); - } + // Future Test Category } From 9098cd5872d6435d1de018d405ae73b7cf575b07 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 21:08:14 -0800 Subject: [PATCH 10/12] Fix Errors --- .../HotReload/ConfigurationHotReloadTests.cs | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index 44d3265a34..e74079c624 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -40,6 +40,7 @@ public class ConfigurationHotReloadTests private static void GenerateConfigFile( DatabaseType databaseType = DatabaseType.MSSQL, + string sessionContext = "true", string connectionString = "", string restPath = "rest", string restEnabled = "true", @@ -61,7 +62,7 @@ private static void GenerateConfigFile( ""data-source"": { ""database-type"": """ + databaseType + @""", ""options"": { - ""set-session-context"": true + ""set-session-context"": " + sessionContext + @" }, ""connection-string"": """ + connectionString + @""" }, @@ -300,5 +301,39 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() Assert.AreEqual(HttpStatusCode.NotFound, gQLResult.StatusCode); } - // Future Test Category + /// + /// Hot reload the configuration file by saving a new database type and connection string. + /// Validate that the response from the server is correct when making a new request after + /// the change in database type. + /// + [TestMethod] + public async Task HotReloadConfigDataSourceEndToEndTest() + { + // Arrange + RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); + + // String has additions that are not in original connection string + string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; + + // Act + GenerateConfigFile( + sessionContext: "false", + connectionString: expectedConnectionString); + System.Threading.Thread.Sleep(3000); + + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); + JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( + _testClient, + _configProvider, + GQL_QUERY_NAME, + GQL_QUERY); + + // Assert + // If the assert succeeds it implies that the connection string is hot-reloadable + Assert.AreNotEqual(previousSessionContext, actualSessionContext); + Assert.AreEqual(false, actualSessionContext.SetSessionContext); + SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); + } } From 4f141eab0a7a930a200d5b165908edfcb1ca3ee5 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 21:15:06 -0800 Subject: [PATCH 11/12] Changes --- .../HotReload/ConfigurationHotReloadTests.cs | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index e74079c624..bd30e91882 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -300,40 +300,4 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() // Assert Assert.AreEqual(HttpStatusCode.NotFound, gQLResult.StatusCode); } - - /// - /// Hot reload the configuration file by saving a new database type and connection string. - /// Validate that the response from the server is correct when making a new request after - /// the change in database type. - /// - [TestMethod] - public async Task HotReloadConfigDataSourceEndToEndTest() - { - // Arrange - RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); - - // String has additions that are not in original connection string - string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + ";Trusted_Connection=True;"; - - // Act - GenerateConfigFile( - sessionContext: "false", - connectionString: expectedConnectionString); - System.Threading.Thread.Sleep(3000); - - RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); - MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); - JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( - _testClient, - _configProvider, - GQL_QUERY_NAME, - GQL_QUERY); - - // Assert - // If the assert succeeds it implies that the connection string is hot-reloadable - Assert.AreNotEqual(previousSessionContext, actualSessionContext); - Assert.AreEqual(false, actualSessionContext.SetSessionContext); - SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); - } } From b17b25feb72c65836bee0c7115eac640a41556e8 Mon Sep 17 00:00:00 2001 From: Ruben Cerna Date: Thu, 14 Nov 2024 21:22:07 -0800 Subject: [PATCH 12/12] Fix Errors --- .../HotReload/ConfigurationHotReloadTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs index bd30e91882..f2a3e33de2 100644 --- a/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs +++ b/src/Service.Tests/Configuration/HotReload/ConfigurationHotReloadTests.cs @@ -300,4 +300,41 @@ public async Task HotReloadConfigRuntimeGQLEnabledEndToEndTest() // Assert Assert.AreEqual(HttpStatusCode.NotFound, gQLResult.StatusCode); } + + /// + /// Hot reload the configuration file by saving a new database type and connection string. + /// Validate that the response from the server is correct when making a new request after + /// the change in database type. + /// + [TestCategory(MSSQL_ENVIRONMENT)] + [TestMethod] + public async Task HotReloadConfigDataSourceEndToEndTest() + { + // Arrange + RuntimeConfig previousRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions previousSessionContext = previousRuntimeConfig.DataSource.GetTypedOptions(); + + // String has additions that are not in original connection string + string expectedConnectionString = $"{ConfigurationTests.GetConnectionStringFromEnvironmentConfig(TestCategory.MSSQL).Replace("\\", "\\\\")}" + "Trusted_Connection=True;"; + + // Act + GenerateConfigFile( + sessionContext: "false", + connectionString: expectedConnectionString); + System.Threading.Thread.Sleep(3000); + + RuntimeConfig updatedRuntimeConfig = _configProvider.GetConfig(); + MsSqlOptions actualSessionContext = updatedRuntimeConfig.DataSource.GetTypedOptions(); + JsonElement reloadGQLContents = await GraphQLRequestExecutor.PostGraphQLRequestAsync( + _testClient, + _configProvider, + GQL_QUERY_NAME, + GQL_QUERY); + + // Assert + // If the assert succeeds it implies that the connection string is hot-reloadable + Assert.AreNotEqual(previousSessionContext, actualSessionContext); + Assert.AreEqual(false, actualSessionContext.SetSessionContext); + SqlTestHelper.PerformTestEqualJsonStrings(_bookDBOContents, reloadGQLContents.GetProperty("items").ToString()); + } }