public async Task Test_UnmappedProgram_RunsAndReturnsExpectedResult() { DatabasesConfiguration configuration = DatabasesConfiguration.Active; SqlProgram unmappedProgram = await configuration.GetSqlProgram("test2", "spReturnsScalar"); Assert.AreEqual("spReturnsScalar", unmappedProgram.Name); Assert.AreEqual("HelloWorld", await unmappedProgram.ExecuteScalarAsync <string>()); }
public async Task Test_UnmappedProgramOnDifferentConnection_RunsAndReturnsExpectedResult() { DatabasesConfiguration configuration = DatabasesConfiguration.Active; // Get a program which does not use the default lbc. SqlProgram diffConnection = await configuration.GetSqlProgram("test", "spReturnsScalarString"); // Check name was not mapped Assert.AreEqual("spReturnsScalarString", diffConnection.Name); // Check the sproc runs Assert.AreEqual("HelloWorld", await diffConnection.ExecuteScalarAsync <string>()); }
public async Task Test_CheckMappedProgramReturnsExpectedResult() { DatabasesConfiguration configuration = DatabasesConfiguration.Active; // Get a program that is mapped by the configuration SqlProgram <string> mappedProgram = await configuration.GetSqlProgram <string>("test2", "TestProgram", "@P1"); // Check the names were mapped. Assert.AreEqual("spTakesParamAndReturnsScalar", mappedProgram.Name); Assert.AreEqual("@firstName", mappedProgram.Parameters.Single().Key); // Check the sproc runs. string name = Guid.NewGuid().ToString().Substring(0, 10); string result = mappedProgram.ExecuteScalar <string>(name); Assert.AreEqual("Hello " + name, result); }
public async Task ExecuteReader_WithByteArrayParameterTooLong_ThrowsExceptionWhenTypeConstraintModeError() { // This Sql Program is configured in the app.config to use TypeConstraintMode Error, so will throw error // if byte[] is truncated. SqlProgram <byte[]> byteArrayTest = await DatabasesConfiguration.GetConfiguredSqlProgram <byte[]>( "test", "spTakeByteArrayLength10", "@byteArrayParam"); byte[] testParam = new byte[11]; Random.NextBytes(testParam); byteArrayTest.ExecuteReader( reader => { if (reader.Read()) { CollectionAssert.AreEqual( testParam, (ICollection)reader.GetValue(0)); } }, testParam); }
public void Test_ActiveConfiguration_IsNotNull() { DatabasesConfiguration configuration = DatabasesConfiguration.Active; Assert.IsNotNull(configuration); }
/// <summary> /// Updates the semaphores. /// </summary> /// <param name="config">The configuration.</param> public static void UpdateSemaphores(DatabasesConfiguration config) { HashSet <string> databases = new HashSet <string>(); HashSet <Id> loadBalancedConns = new HashSet <Id>(); HashSet <ConnectionId> connections = new HashSet <ConnectionId>(); HashSet <Id> programs = new HashSet <Id>(); lock (_updatedLock) { // Add or update the semaphores for the values in the config foreach (DatabaseElement db in config.Databases) { Debug.Assert(db != null); string databaseId = db.Id; Debug.Assert(!databases.Contains(databaseId)); databases.Add(databaseId); UpdateSemaphore(db.MaximumConcurrency, databaseId, _databaseSemaphores); foreach (LoadBalancedConnectionElement lbConnection in db.Connections) { Debug.Assert(lbConnection != null); Id lbcId = new Id(databaseId, lbConnection.Id); Debug.Assert(!loadBalancedConns.Contains(lbcId)); loadBalancedConns.Add(lbcId); UpdateSemaphore(lbConnection.MaximumConcurrency, lbcId, _loadBalancedConnectionSemaphores); foreach (IGrouping <ConnectionId, KeyValuePair <ConnectionId, int> > conn in lbConnection.Connections .Select( c => new KeyValuePair <ConnectionId, int>( // ReSharper disable once PossibleNullReferenceException new ConnectionId(lbcId, c.ConnectionString), c.MaximumConcurrency)) .GroupBy(c => c.Key)) { Debug.Assert(conn != null); ConnectionId connectionId = conn.Key; int maxConcurrency = conn.Aggregate( (int?)null, (mc, c) => { if (mc < 1) { return(mc); } if (c.Value < 1) { return(-1); } return((mc ?? 0) + c.Value); }) ?? -1; connections.Add(connectionId); UpdateSemaphore(maxConcurrency, connectionId, _connectionSemaphores); } } foreach (ProgramElement program in db.Programs) { Debug.Assert(program != null); Id programId = new Id(databaseId, string.IsNullOrWhiteSpace(program.MapTo) ? program.Name : program.MapTo); Debug.Assert(!programs.Contains(programId)); programs.Add(programId); UpdateSemaphore(program.MaximumConcurrency, programId, _programSemaphores); } } // Remove any semaphores that are no longer in the configuration foreach (string id in _databaseSemaphores.Keys) { Debug.Assert(id != null); AsyncSemaphore semaphore; if (databases.Contains(id) || !_databaseSemaphores.TryRemove(id, out semaphore)) { continue; } Debug.Assert(semaphore != null); semaphore.MaxCount = int.MaxValue; } foreach (Id id in _loadBalancedConnectionSemaphores.Keys) { AsyncSemaphore semaphore; if (loadBalancedConns.Contains(id) || !_loadBalancedConnectionSemaphores.TryRemove(id, out semaphore)) { continue; } Debug.Assert(semaphore != null); semaphore.MaxCount = int.MaxValue; } foreach (ConnectionId id in _connectionSemaphores.Keys) { AsyncSemaphore semaphore; if (connections.Contains(id) || !_connectionSemaphores.TryRemove(id, out semaphore)) { continue; } Debug.Assert(semaphore != null); semaphore.MaxCount = int.MaxValue; } foreach (Id id in _programSemaphores.Keys) { AsyncSemaphore semaphore; if (programs.Contains(id) || !_programSemaphores.TryRemove(id, out semaphore)) { continue; } Debug.Assert(semaphore != null); semaphore.MaxCount = int.MaxValue; } } }
/// <summary> /// Called when the databse configuartion changes. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="ConfigurationSection{T}.ConfigurationChangedEventArgs"/> instance containing the event data.</param> public static void OnActiveConfigChanged( [NotNull] DatabasesConfiguration sender, [NotNull] ConfigurationSection <DatabasesConfiguration> .ConfigurationChangedEventArgs args) { UpdateSemaphores(sender); }