public void Constructor_WithMultipleIdenticalConnections_Deduplicates() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(CreateConnectionString("LocalData"), CreateConnectionString("LocalData")); Assert.IsNotNull(loadBalancedConnection); Assert.AreEqual(1, loadBalancedConnection.Count()); }
public void Constructor_WithSingleConnectionStringAndSchemaIsIdenticalCheckSetToTrue_CreatesInstanceWithCountOfOne() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionString: CreateConnectionString("LocalData")); Assert.IsNotNull(loadBalancedConnection); Assert.AreEqual(1, loadBalancedConnection.Count()); }
public void ToString_ReturnsExpectedString() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: new[] { LocalDatabaseConnectionString, LocalDatabaseCopyConnectionString }); Assert.AreEqual("Load balanced connection string with '2' connections.", loadBalancedConnection.ToString()); }
public void Constructor_WithValidConnectionString_CreatesInstanceWithCountOfOne() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionString: CreateConnectionString("LocalData")); Assert.IsNotNull(loadBalancedConnection); Assert.AreEqual(1, loadBalancedConnection.Count()); }
public void GetEnumerator_ReturnsIEnumerator() { IEnumerable loadBalancedConnection = new LoadBalancedConnection(connectionString: CreateConnectionString("LocalData")); IEnumerator enumerator = loadBalancedConnection.GetEnumerator(); Assert.IsNotNull(enumerator); }
public void Constructor_WithNegativeWeighting_ThrowsLoggingException() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: new List <KeyValuePair <string, double> > { new KeyValuePair <string, double>(LocalDatabaseConnectionString, -1) }); Assert.IsNull(loadBalancedConnection); }
public void Constructor_WithNegativeWeighting_ThrowsLoggingException() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: new List<KeyValuePair<string, double>> { new KeyValuePair<string, double>(LocalDatabaseConnectionString, -1) }); Assert.IsNull(loadBalancedConnection); }
public Task <LoadBalancedConnection> GetLoadBalancedConnection( bool?ensureIdentical = null, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); if (!Enabled) { return(TaskResult <LoadBalancedConnection> .Default); } KeyValuePair <string, double>[] connections = Connections .Where(lbc => lbc != null && lbc.Enabled) .Select( lbc => new KeyValuePair <string, double>(lbc.ConnectionString, lbc.Weight)) .ToArray(); if (connections.Length < 1) { return(TaskResult <LoadBalancedConnection> .Default); } LoadBalancedConnection connection = Database == null ? new LoadBalancedConnection(connections) : new LoadBalancedConnection(Database.Id, Id, connections); // ReSharper disable once AssignNullToNotNullAttribute if (ensureIdentical == false || (!ensureIdentical.HasValue && !EnsureSchemasIdentical)) { return(Task.FromResult(connection)); } return(connection.CheckIdentical(cancellationToken) .ContinueWith( t => { Debug.Assert(t != null); if (!t.Result) { throw new LoggingException( () => Resources .LoadBalancedConnectionElement_GetLoadBalancedConnection_SchemasNotIdentical); } return connection; }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, // ReSharper disable once AssignNullToNotNullAttribute TaskScheduler.Current)); }
public void Constructor_WithConnectionsWithIdenticalSchemas_CreatesInstance() { List <KeyValuePair <string, double> > connections = new List <KeyValuePair <string, double> > { new KeyValuePair <string, double>(CreateConnectionString("LocalData"), 0.5), new KeyValuePair <string, double>(CreateConnectionString("LocalDataCopy"), 0.5) }; LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: connections); Assert.IsNotNull(loadBalancedConnection); }
public void CreateConnection_WithNoParameters_ReturnsSqlConnection() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(CreateConnectionString("LocalData")); using (SqlConnection connection = new SqlConnection(loadBalancedConnection.ChooseConnectionString())) { Assert.IsNotNull(connection); Assert.IsFalse(connection.State == ConnectionState.Open); // Check that we are always coerced to being asynchronous SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connection.ConnectionString); Assert.IsNotNull(builder); Assert.IsTrue(builder.AsynchronousProcessing); } }
public void Constructor_WithInvalidConnectionStrings_ThrowsLoggingException() { LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: new[] { string.Empty, string.Empty }); }
public void Constructor_WithConnectionsWithIdenticalSchemas_CreatesInstance() { List<KeyValuePair<string, double>> connections = new List<KeyValuePair<string, double>> { new KeyValuePair<string, double>(CreateConnectionString("LocalData"), 0.5), new KeyValuePair<string, double>(CreateConnectionString("LocalDataCopy"), 0.5) }; LoadBalancedConnection loadBalancedConnection = new LoadBalancedConnection(connectionStrings: connections); Assert.IsNotNull(loadBalancedConnection); }
public async Task <SqlProgram> GetSqlProgram( [NotNull] string name, [CanBeNull] IEnumerable <KeyValuePair <string, Type> > parameters = null, bool?ignoreValidationErrors = null, bool?checkOrder = null, TimeSpan?defaultCommandTimeout = null, TypeConstraintMode?constraintMode = null, CancellationToken cancellationToken = default(CancellationToken)) { if (name == null) { throw new ArgumentNullException(nameof(name)); } // Grab the default load balanced connection for the database. // ReSharper disable once PossibleNullReferenceException LoadBalancedConnectionElement connectionElement = Connections.FirstOrDefault(c => c.Enabled); if (connectionElement == null) { throw new LoggingException( () => Resources.DatabaseElement_GetSqlProgram_DefaultLoadBalanceConnectionNotFound, Id); } // Look for program mapping information ProgramElement prog = Programs[name]; if (prog != null) { // Check for name mapping if (!String.IsNullOrWhiteSpace(prog.MapTo)) { // ReSharper disable once AssignNullToNotNullAttribute name = prog.MapTo; } // Set options if not already set. ignoreValidationErrors = ignoreValidationErrors ?? prog.IgnoreValidationErrors; checkOrder = checkOrder ?? prog.CheckOrder; defaultCommandTimeout = defaultCommandTimeout ?? prog.DefaultCommandTimeout; constraintMode = constraintMode ?? prog.ConstraintMode; if (!String.IsNullOrEmpty(prog.Connection)) { // ReSharper disable once AssignNullToNotNullAttribute connectionElement = Connections[prog.Connection]; if ((connectionElement == null) || (!connectionElement.Enabled)) { throw new LoggingException( () => Resources.DatabaseElement_GetSqlProgram_LoadBalanceConnectionNotFound, prog.Connection, Id, name); } } // Check for parameter mappings if ((parameters != null) && prog.Parameters.Any()) { parameters = parameters .Select( kvp => { // ReSharper disable once AssignNullToNotNullAttribute ParameterElement param = prog.Parameters[kvp.Key]; if (param == null) { return(kvp); } if (String.IsNullOrWhiteSpace(param.MapTo)) { throw new LoggingException( () => Resources.DatabaseElement_GetSqlProgram_MappingNotSpecified, kvp.Key, prog.Name); } return(new KeyValuePair <string, Type>(param.MapTo, kvp.Value)); }).ToList(); } } if (ignoreValidationErrors == null) { ignoreValidationErrors = false; } if (checkOrder == null) { checkOrder = false; } if (constraintMode == null) { constraintMode = TypeConstraintMode.Warn; } if (connectionElement == null) { throw new LoggingException( () => Resources.DatabaseElement_GetSchemas_No_Connection, Id); } LoadBalancedConnection connection = await connectionElement.GetLoadBalancedConnection(cancellationToken).ConfigureAwait(false); Debug.Assert(connection != null); Debug.Assert(name != null); return(await SqlProgram.Create( connection, name, parameters, ignoreValidationErrors.Value, checkOrder.Value, defaultCommandTimeout, (TypeConstraintMode)constraintMode, cancellationToken).ConfigureAwait(false)); }