コード例 #1
0
        public void negative_cache()
        {
            const string connectionString  = "";
            var          parser            = new DbConnectionStringParser(LoggerBase);
            var          actualDestination = parser.ExtractDestination(connectionString, out var wasFoundInCache);

            wasFoundInCache.Should().BeFalse();
            actualDestination.Should().BeNull();
            var actualDestination2 = parser.ExtractDestination(connectionString, out var wasFoundInCache2);

            wasFoundInCache2.Should().BeTrue();
            actualDestination2.Should().BeNull();
        }
コード例 #2
0
        public void positive_cache()
        {
            const string connectionString  = @"Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword";
            var          parser            = new DbConnectionStringParser(LoggerBase);
            var          actualDestination = parser.ExtractDestination(connectionString, out var wasFoundInCache);

            wasFoundInCache.Should().BeFalse();
            actualDestination.Should().NotBeNull();
            actualDestination.Address.Should().Be("myServerAddress");
            actualDestination.Port.Should().BeNull();
            var actualDestination2 = parser.ExtractDestination(connectionString, out var wasFoundInCache2);

            wasFoundInCache2.Should().BeTrue();
            actualDestination2.Should().Be(actualDestination);
        }
コード例 #3
0
        public void caches_only_first_MaxCacheSize_results()
        {
            const string validConnectionStringPrefix = @"Server=myServerAddress;Port=";
            var          parser = new DbConnectionStringParser(LoggerBase);

            DbConnectionStringParser.MaxCacheSize.Repeat(i => { VerifyForIndex(i, /* isFirstTime: */ true); });
            DbConnectionStringParser.MaxCacheSize.Repeat(i => { VerifyForIndex(i, /* isFirstTime: */ false); });
            10.Repeat(i => { VerifyForIndex(DbConnectionStringParser.MaxCacheSize + i, /* isFirstTime: */ true); });
            10.Repeat(i => { VerifyForIndex(DbConnectionStringParser.MaxCacheSize + i, /* isFirstTime: */ false); });

            void VerifyForIndex(int index, bool isFirstTime)
            {
                var isValidConnectionString = index % 2 == 0;
                var port              = 10000 + index;
                var connectionString  = isValidConnectionString ? validConnectionStringPrefix + (port) : $"{index}";
                var actualDestination = parser.ExtractDestination(connectionString, out var wasFoundInCache);

                wasFoundInCache.Should().Be(!isFirstTime && index < DbConnectionStringParser.MaxCacheSize);
                if (isValidConnectionString)
                {
                    actualDestination.Should().NotBeNull();
                    actualDestination.Address.Should().Be("myServerAddress");
                    actualDestination.Port.Should().Be(port);
                }
                else
                {
                    actualDestination.Should().BeNull();
                }
            }
        }
コード例 #4
0
        internal static void TestImpl(IApmLogger logger, string dbgDescription, string connectionString, string expectedHost
                                      , int?expectedPort
                                      )
        {
            var dbgInfo           = $"connectionString: `{connectionString}'. dbgDescription: {dbgDescription}.";
            var parser            = new DbConnectionStringParser(logger);
            var actualDestination = parser.ExtractDestination(connectionString);

            actualDestination.Should().NotBeNull(dbgInfo);
            actualDestination.Address.Should().Be(expectedHost, dbgInfo);
            actualDestination.Port.Should().Be(expectedPort, dbgInfo);
        }
コード例 #5
0
        internal static void InvalidValueTestImpl(string dbgDescription, string connectionString, string invalidPart)
        {
            var mockLogger = new TestLogger(LogLevel.Trace);
            var dbgInfo    = $"connectionString: `{connectionString}'. dbgDescription: {dbgDescription}.";
            var parser     = new DbConnectionStringParser(mockLogger);

            parser.ExtractDestination(connectionString).Should().BeNull(dbgInfo);
            mockLogger.Lines.Should()
            .Contain(line =>
                     line.Contains(nameof(DbConnectionStringParser)) &&
                     line.Contains(connectionString) &&
                     line.Contains(invalidPart), dbgInfo);
        }