public void SSLCertificateValidationError(bool isCertValidationSucceeded)
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl      = true;
            options.Password = TestConfig.Current.AzureCachePassword;
            options.CertificateValidation += (sender, cert, chain, errors) => isCertValidationSucceeded;
            options.AbortOnConnectFail     = false;

            using (var connection = ConnectionMultiplexer.Connect(options))
            {
                connection.ConnectionFailed += (object sender, ConnectionFailedEventArgs e) =>
                                               Assert.Equal(ConnectionFailureType.AuthenticationFailure, e.FailureType);
                if (!isCertValidationSucceeded)
                {
                    //validate that in this case it throws an certificatevalidation exception
                    var ex  = Assert.Throws <RedisConnectionException>(() => connection.GetDatabase().Ping());
                    var rde = (RedisConnectionException)ex.InnerException;
                    Assert.Equal(ConnectionFailureType.AuthenticationFailure, rde.FailureType);
                    Assert.Equal("The remote certificate is invalid according to the validation procedure.", rde.InnerException.Message);
                }
                else
                {
                    connection.GetDatabase().Ping();
                }

                //wait for a second for connectionfailed event to fire
                Thread.Sleep(1000);
            }
        }
Пример #2
0
        public async Task AzureRedisEventsAutomaticSubscribe()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            bool didUpdate = false;
            var  options   = new ConfigurationOptions()
            {
                EndPoints = { TestConfig.Current.AzureCacheServer },
                Password  = TestConfig.Current.AzureCachePassword,
                Ssl       = true
            };

            using (var connection = await ConnectionMultiplexer.ConnectAsync(options))
            {
                connection.ServerMaintenanceEvent += (object?_, ServerMaintenanceEvent e) =>
                {
                    if (e is AzureMaintenanceEvent)
                    {
                        didUpdate = true;
                    }
                };

                var pubSub = connection.GetSubscriber();
                await pubSub.PublishAsync("AzureRedisEvents", "HI");

                await Task.Delay(100);

                Assert.True(didUpdate);
            }
        }
Пример #3
0
        public void Execute(string config)
        {
            if (string.IsNullOrEmpty(config))
            {
                Skip.IfNoConfig(nameof(TestConfig.Config.VPNConfigs), TestConfig.Current.VPNConfigs);
            }

            for (int i = 0; i < 50; i++)
            {
                var log = new StringWriter();
                try
                {
                    var options = ConfigurationOptions.Parse(config);
                    options.SyncTimeout  = 3000;
                    options.ConnectRetry = 5;
                    using (var conn = ConnectionMultiplexer.Connect(options, log))
                    {
                        var ttl = conn.GetDatabase().Ping();
                        Output.WriteLine(ttl.ToString());
                    }
                }
                catch
                {
                    Output.WriteLine(log.ToString());
                    throw;
                }
                Output.WriteLine("");
                Output.WriteLine("===");
                Output.WriteLine("");
            }
        }
Пример #4
0
        public Sentinel(ITestOutputHelper output) : base(output)
        {
            ConnectionLog = new StringWriter();

            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);

            var options = ServiceOptions.Clone();

            options.EndPoints.Add(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortA);
            options.EndPoints.Add(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortB);
            options.EndPoints.Add(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortC);

            Conn = ConnectionMultiplexer.SentinelConnect(options, ConnectionLog);
            for (var i = 0; i < 150; i++)
            {
                Thread.Sleep(20);
                if (Conn.IsConnected && Conn.GetSentinelMasterConnection(options).IsConnected)
                {
                    break;
                }
            }
            Assert.True(Conn.IsConnected);
            SentinelServerA  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortA);
            SentinelServerB  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortB);
            SentinelServerC  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortC);
            SentinelsServers = new[] { SentinelServerA, SentinelServerB, SentinelServerC };

            // wait until we are in a state of a single master and replica
            WaitForReady();
        }
Пример #5
0
        public async Task AuthenticationFailureError()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl                    = true;
            options.Password               = "";
            options.AbortOnConnectFail     = false;
            options.CertificateValidation += SSL.ShowCertFailures(Writer);
            using (var muxer = ConnectionMultiplexer.Connect(options))
            {
                muxer.ConnectionFailed += (sender, e) =>
                {
                    if (e.FailureType == ConnectionFailureType.SocketFailure)
                    {
                        Skip.Inconclusive("socket fail");                                                       // this is OK too
                    }
                    Assert.Equal(ConnectionFailureType.AuthenticationFailure, e.FailureType);
                };
                var ex = Assert.Throws <RedisConnectionException>(() => muxer.GetDatabase().Ping());

                Assert.NotNull(ex.InnerException);
                var rde = Assert.IsType <RedisConnectionException>(ex.InnerException);
                Assert.Equal(CommandStatus.WaitingToBeSent, ex.CommandStatus);
                Assert.Equal(ConnectionFailureType.AuthenticationFailure, rde.FailureType);
                Assert.Equal("Error: NOAUTH Authentication required. Verify if the Redis password provided is correct.", rde.InnerException.Message);
                //wait for a second  for connectionfailed event to fire
                await Task.Delay(1000).ForAwait();
            }
        }
Пример #6
0
        public Sentinel(ITestOutputHelper output) : base(output)
        {
            ConnectionLog = new StringWriter();

            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);

            var options = new ConfigurationOptions()
            {
                CommandMap = CommandMap.Sentinel,
                EndPoints  =
                {
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort  },
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort1 },
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort2 }
                },
                AllowAdmin  = true,
                TieBreaker  = "",
                ServiceName = TestConfig.Current.SentinelSeviceName,
                SyncTimeout = 5000
            };

            Conn = ConnectionMultiplexer.Connect(options, ConnectionLog);
            Thread.Sleep(3000);
            Assert.True(Conn.IsConnected);
            Server26379      = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort);
            Server26380      = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort1);
            Server26381      = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort2);
            SentinelsServers = new IServer[] { Server26379, Server26380, Server26381 };
        }
Пример #7
0
        [InlineData(6380, true)] // all explicit
        // (note the 6379 port is closed)
        public void ConnectToAzure(int?port, bool ssl)
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions();

            options.CertificateValidation += ShowCertFailures(Writer);
            if (port == null)
            {
                options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            }
            else
            {
                options.EndPoints.Add(TestConfig.Current.AzureCacheServer, port.Value);
            }
            options.Ssl      = ssl;
            options.Password = TestConfig.Current.AzureCachePassword;
            Log(options.ToString());
            using (var connection = ConnectionMultiplexer.Connect(options))
            {
                var ttl = connection.GetDatabase().Ping();
                Log(ttl.ToString());
            }
        }
Пример #8
0
        public void RedisLabsSSL()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.RedisLabsSslServer), TestConfig.Current.RedisLabsSslServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.RedisLabsPfxPath), TestConfig.Current.RedisLabsPfxPath);

            var cert = new X509Certificate2(TestConfig.Current.RedisLabsPfxPath, "");

            Assert.NotNull(cert);
            Writer.WriteLine("Thumbprint: " + cert.Thumbprint);

            int timeout = 5000;

            if (Debugger.IsAttached)
            {
                timeout *= 100;
            }
            var options = new ConfigurationOptions
            {
                EndPoints      = { { TestConfig.Current.RedisLabsSslServer, TestConfig.Current.RedisLabsSslPort } },
                ConnectTimeout = timeout,
                AllowAdmin     = true,
                CommandMap     = CommandMap.Create(new HashSet <string> {
                    "subscribe", "unsubscribe", "cluster"
                }, false)
            };

            options.TrustIssuer("redislabs_ca.pem");

            if (!Directory.Exists(Me()))
            {
                Directory.CreateDirectory(Me());
            }
#if LOGOUTPUT
            ConnectionMultiplexer.EchoPath = Me();
#endif
            options.Ssl = true;
            options.CertificateSelection += delegate
            {
                return(cert);
            };
            RedisKey key = Me();
            using (var conn = ConnectionMultiplexer.Connect(options))
            {
                var db = conn.GetDatabase();
                db.KeyDelete(key, CommandFlags.FireAndForget);
                string s = db.StringGet(key);
                Assert.Null(s);
                db.StringSet(key, "abc", flags: CommandFlags.FireAndForget);
                s = db.StringGet(key);
                Assert.Equal("abc", s);

                var latency = db.Ping();
                Log("RedisLabs latency: {0:###,##0.##}ms", latency.TotalMilliseconds);

                using (var file = File.Create("RedisLabs.zip"))
                {
                    conn.ExportConfiguration(file);
                }
            }
        }
Пример #9
0
        public void SSLParseViaConfig_Issue883_ConfigString()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var configString = $"{TestConfig.Current.AzureCacheServer}:6380,password={TestConfig.Current.AzureCachePassword},connectRetry=3,connectTimeout=5000,syncTimeout=5000,defaultDatabase=0,ssl=true,abortConnect=false";
            var options      = ConfigurationOptions.Parse(configString);

            options.CertificateValidation += ShowCertFailures(Writer);
            using (var conn = ConnectionMultiplexer.Connect(options))
            {
                conn.GetDatabase().Ping();
            }
        }
        public void AbortOnConnectFailFalseConnectTimeoutError()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl            = true;
            options.ConnectTimeout = 0;
            options.Password       = TestConfig.Current.AzureCachePassword;
            using (var muxer = ConnectionMultiplexer.Connect(options))
            {
                var ex = Assert.Throws <RedisConnectionException>(() => muxer.GetDatabase().Ping());
                Assert.Contains("ConnectTimeout", ex.Message);
            }
        }
        public async Task SSLCertificateValidationError(bool isCertValidationSucceeded)
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl      = true;
            options.Password = TestConfig.Current.AzureCachePassword;
            options.CertificateValidation += (sender, cert, chain, errors) => isCertValidationSucceeded;
            options.AbortOnConnectFail     = false;

            using (var connection = ConnectionMultiplexer.Connect(options))
            {
                await RunBlockingSynchronousWithExtraThreadAsync(innerScenario).ForAwait();

                void innerScenario()
                {
                    connection.ConnectionFailed += (sender, e) =>
                                                   Assert.Equal(ConnectionFailureType.AuthenticationFailure, e.FailureType);
                    if (!isCertValidationSucceeded)
                    {
                        //validate that in this case it throws an certificatevalidation exception
                        var outer = Assert.Throws <RedisConnectionException>(() => connection.GetDatabase().Ping());
                        Assert.Equal(ConnectionFailureType.UnableToResolvePhysicalConnection, outer.FailureType);

                        Assert.NotNull(outer.InnerException);
                        var inner = Assert.IsType <RedisConnectionException>(outer.InnerException);
                        Assert.Equal(ConnectionFailureType.AuthenticationFailure, inner.FailureType);

                        Assert.NotNull(inner.InnerException);
                        var innerMost = Assert.IsType <AuthenticationException>(inner.InnerException);
                        Assert.Equal("The remote certificate is invalid according to the validation procedure.", innerMost.Message);
                    }
                    else
                    {
                        connection.GetDatabase().Ping();
                    }
                }

                // wait for a second for connectionfailed event to fire
                await Task.Delay(1000).ForAwait();
            }
        }
Пример #12
0
        public void ConnectToSSDB()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.SSDBServer), TestConfig.Current.SSDBServer);

            var config = new ConfigurationOptions
            {
                EndPoints  = { { TestConfig.Current.SSDBServer, TestConfig.Current.SSDBPort } },
                CommandMap = CommandMap.SSDB
            };
            RedisKey key = Me();

            using (var conn = ConnectionMultiplexer.Connect(config))
            {
                var db = conn.GetDatabase(0);
                db.KeyDelete(key);
                Assert.True(db.StringGet(key).IsNull);
                db.StringSet(key, "abc");
                Assert.Equal("abc", db.StringGet(key));
            }
        }
Пример #13
0
        private ConnectionMultiplexer Create()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl            = true;
            options.ConnectTimeout = 5000;
            options.Password       = TestConfig.Current.AzureCachePassword;
            options.TieBreaker     = "";
            var log  = new StringWriter();
            var conn = ConnectionMultiplexer.Connect(options, log);
            var s    = log.ToString();

            Output.WriteLine(s);
            Skip.IfMissingFeature(conn, nameof(RedisFeatures.Geo), r => r.Geo);
            return(conn);
        }
Пример #14
0
        public Sentinel(ITestOutputHelper output)
        {
            Output = output;

            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);

            var options = new ConfigurationOptions()
            {
                CommandMap  = CommandMap.Sentinel,
                EndPoints   = { { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort } },
                AllowAdmin  = true,
                TieBreaker  = "",
                ServiceName = TestConfig.Current.SentinelSeviceName,
                SyncTimeout = 5000
            };

            Conn = ConnectionMultiplexer.Connect(options, Console.Out);
            Thread.Sleep(3000);
            Assert.True(Conn.IsConnected);
            Server = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort);
        }
        public void AuthenticationFailureError()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);

            var options = new ConfigurationOptions();

            options.EndPoints.Add(TestConfig.Current.AzureCacheServer);
            options.Ssl                = true;
            options.Password           = "";
            options.AbortOnConnectFail = false;
            using (var muxer = ConnectionMultiplexer.Connect(options))
            {
                muxer.ConnectionFailed += (object sender, ConnectionFailedEventArgs e) =>
                                          Assert.Equal(ConnectionFailureType.AuthenticationFailure, e.FailureType);
                var ex  = Assert.Throws <RedisConnectionException>(() => muxer.GetDatabase().Ping());
                var rde = (RedisConnectionException)ex.InnerException;
                Assert.Equal(CommandStatus.WaitingToBeSent, ex.CommandStatus);
                Assert.Equal(ConnectionFailureType.AuthenticationFailure, rde.FailureType);
                Assert.Equal("Error: NOAUTH Authentication required. Verify if the Redis password provided is correct.", rde.InnerException.Message);
                //wait for a second  for connectionfailed event to fire
                Thread.Sleep(1000);
            }
        }
Пример #16
0
        public void SSLParseViaConfig_Issue883_ConfigObject()
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCacheServer), TestConfig.Current.AzureCacheServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.AzureCachePassword), TestConfig.Current.AzureCachePassword);

            var options = new ConfigurationOptions
            {
                AbortOnConnectFail = false,
                Ssl             = true,
                ConnectRetry    = 3,
                ConnectTimeout  = 5000,
                SyncTimeout     = 5000,
                DefaultDatabase = 0,
                EndPoints       = { { TestConfig.Current.AzureCacheServer, 6380 } },
                Password        = TestConfig.Current.AzureCachePassword
            };

            options.CertificateValidation += ShowCertFailures(Writer);
            using (var conn = ConnectionMultiplexer.Connect(options))
            {
                conn.GetDatabase().Ping();
            }
        }
Пример #17
0
        public Sentinel(ITestOutputHelper output) : base(output)
        {
            ConnectionLog = new StringWriter();

            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
            Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);

            var options = new ConfigurationOptions()
            {
                CommandMap = CommandMap.Sentinel,
                EndPoints  =
                {
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortA },
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortB },
                    { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortC }
                },
                AllowAdmin  = true,
                TieBreaker  = "",
                ServiceName = TestConfig.Current.SentinelSeviceName,
                SyncTimeout = 5000
            };

            Conn = ConnectionMultiplexer.Connect(options, ConnectionLog);
            for (var i = 0; i < 150; i++)
            {
                Thread.Sleep(20);
                if (Conn.IsConnected && Conn.GetSentinelMasterConnection(ServiceOptions).IsConnected)
                {
                    break;
                }
            }
            Assert.True(Conn.IsConnected);
            SentinelServerA  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortA);
            SentinelServerB  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortB);
            SentinelServerC  = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPortC);
            SentinelsServers = new IServer[] { SentinelServerA, SentinelServerB, SentinelServerC };
        }
Пример #18
0
 public SentinelBase(ITestOutputHelper output) : base(output)
 {
     Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
     Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);
 }
Пример #19
0
        public void RedisLabsEnvironmentVariableClientCertificate(bool setEnv)
        {
            try
            {
                Skip.IfNoConfig(nameof(TestConfig.Config.RedisLabsSslServer), TestConfig.Current.RedisLabsSslServer);
                Skip.IfNoConfig(nameof(TestConfig.Config.RedisLabsPfxPath), TestConfig.Current.RedisLabsPfxPath);

                if (setEnv)
                {
                    Environment.SetEnvironmentVariable("SERedis_ClientCertPfxPath", TestConfig.Current.RedisLabsPfxPath);
                    Environment.SetEnvironmentVariable("SERedis_IssuerCertPath", "redislabs_ca.pem");
                    // check env worked
                    Assert.Equal(TestConfig.Current.RedisLabsPfxPath, Environment.GetEnvironmentVariable("SERedis_ClientCertPfxPath"));
                    Assert.Equal("redislabs_ca.pem", Environment.GetEnvironmentVariable("SERedis_IssuerCertPath"));
                }
                int timeout = 5000;
                if (Debugger.IsAttached)
                {
                    timeout *= 100;
                }
                var options = new ConfigurationOptions
                {
                    EndPoints      = { { TestConfig.Current.RedisLabsSslServer, TestConfig.Current.RedisLabsSslPort } },
                    ConnectTimeout = timeout,
                    AllowAdmin     = true,
                    CommandMap     = CommandMap.Create(new HashSet <string> {
                        "subscribe", "unsubscribe", "cluster"
                    }, false)
                };

                if (!Directory.Exists(Me()))
                {
                    Directory.CreateDirectory(Me());
                }
#if LOGOUTPUT
                ConnectionMultiplexer.EchoPath = Me();
#endif
                options.Ssl = true;
                RedisKey key = Me();
                using (var conn = ConnectionMultiplexer.Connect(options))
                {
                    if (!setEnv)
                    {
                        Assert.True(false, "Could not set environment");
                    }

                    var db = conn.GetDatabase();
                    db.KeyDelete(key, CommandFlags.FireAndForget);
                    string s = db.StringGet(key);
                    Assert.Null(s);
                    db.StringSet(key, "abc");
                    s = db.StringGet(key);
                    Assert.Equal("abc", s);

                    var latency = db.Ping();
                    Log("RedisLabs latency: {0:###,##0.##}ms", latency.TotalMilliseconds);

                    using (var file = File.Create("RedisLabs.zip"))
                    {
                        conn.ExportConfiguration(file);
                    }
                }
            }
            catch (RedisConnectionException ex)
            {
                if (setEnv || ex.FailureType != ConnectionFailureType.UnableToConnect)
                {
                    throw;
                }
            }
            finally
            {
                Environment.SetEnvironmentVariable("SERedis_ClientCertPfxPath", null);
            }
        }
Пример #20
0
        public async Task ConnectToSSLServer(bool useSsl, bool specifyHost)
        {
            var    server   = TestConfig.Current.SslServer;
            int?   port     = TestConfig.Current.SslPort;
            string password = "";
            bool   isAzure  = false;

            if (string.IsNullOrWhiteSpace(server) && useSsl)
            {
                // we can bounce it past azure instead?
                server   = TestConfig.Current.AzureCacheServer;
                password = TestConfig.Current.AzureCachePassword;
                port     = null;
                isAzure  = true;
            }
            Skip.IfNoConfig(nameof(TestConfig.Config.SslServer), server);

            var config = new ConfigurationOptions
            {
                AllowAdmin  = true,
                SyncTimeout = Debugger.IsAttached ? int.MaxValue : 5000,
                Password    = password,
            };
            var map = new Dictionary <string, string>
            {
                ["config"] = null // don't rely on config working
            };

            if (!isAzure)
            {
                map["cluster"] = null;
            }
            config.CommandMap = CommandMap.Create(map);
            if (port != null)
            {
                config.EndPoints.Add(server, port.Value);
            }
            else
            {
                config.EndPoints.Add(server);
            }

            if (useSsl)
            {
                config.Ssl = useSsl;
                if (specifyHost)
                {
                    config.SslHost = server;
                }
                config.CertificateValidation += (sender, cert, chain, errors) =>
                {
                    Log("errors: " + errors);
                    Log("cert issued to: " + cert.Subject);
                    return(true); // fingers in ears, pretend we don't know this is wrong
                };
            }

            var configString = config.ToString();

            Log("config: " + configString);
            var clone = ConfigurationOptions.Parse(configString);

            Assert.Equal(configString, clone.ToString());

            using (var log = new StringWriter())
                using (var muxer = ConnectionMultiplexer.Connect(config, log))
                {
                    Log("Connect log:");
                    lock (log)
                    {
                        Log(log.ToString());
                    }
                    Log("====");
                    muxer.ConnectionFailed += OnConnectionFailed;
                    muxer.InternalError    += OnInternalError;
                    var db = muxer.GetDatabase();
                    await db.PingAsync().ForAwait();

                    using (var file = File.Create("ssl-" + useSsl + "-" + specifyHost + ".zip"))
                    {
                        muxer.ExportConfiguration(file);
                    }
                    RedisKey key = "SE.Redis";

                    const int AsyncLoop = 2000;
                    // perf; async
                    await db.KeyDeleteAsync(key).ForAwait();

                    var watch = Stopwatch.StartNew();
                    for (int i = 0; i < AsyncLoop; i++)
                    {
                        try
                        {
                            await db.StringIncrementAsync(key, flags : CommandFlags.FireAndForget).ForAwait();
                        }
                        catch (Exception ex)
                        {
                            Log($"Failure on i={i}: {ex.Message}");
                            throw;
                        }
                    }
                    // need to do this inside the timer to measure the TTLB
                    long value = (long)await db.StringGetAsync(key).ForAwait();

                    watch.Stop();
                    Assert.Equal(AsyncLoop, value);
                    Log("F&F: {0} INCR, {1:###,##0}ms, {2} ops/s; final value: {3}",
                        AsyncLoop,
                        watch.ElapsedMilliseconds,
                        (long)(AsyncLoop / watch.Elapsed.TotalSeconds),
                        value);

                    // perf: sync/multi-threaded
                    // TestConcurrent(db, key, 30, 10);
                    //TestConcurrent(db, key, 30, 20);
                    //TestConcurrent(db, key, 30, 30);
                    //TestConcurrent(db, key, 30, 40);
                    //TestConcurrent(db, key, 30, 50);
                }
        }
Пример #21
0
        public void ConnectToSSLServer(bool useSsl, bool specifyHost)
        {
            Skip.IfNoConfig(nameof(TestConfig.Config.SslServer), TestConfig.Current.SslServer);

            var config = new ConfigurationOptions
            {
                CommandMap = CommandMap.Create( // looks like "config" is disabled
                    new Dictionary <string, string>
                {
                    ["config"]  = null,
                    ["cluster"] = null
                }
                    ),
                EndPoints   = { { TestConfig.Current.SslServer, TestConfig.Current.SslPort } },
                AllowAdmin  = true,
                SyncTimeout = Debugger.IsAttached ? int.MaxValue : 5000
            };

            if (useSsl)
            {
                config.Ssl = useSsl;
                if (specifyHost)
                {
                    config.SslHost = TestConfig.Current.SslServer;
                }
                config.CertificateValidation += (sender, cert, chain, errors) =>
                {
                    Output.WriteLine("errors: " + errors);
                    Output.WriteLine("cert issued to: " + cert.Subject);
                    return(true); // fingers in ears, pretend we don't know this is wrong
                };
            }

            var configString = config.ToString();

            Output.WriteLine("config: " + configString);
            var clone = ConfigurationOptions.Parse(configString);

            Assert.Equal(configString, clone.ToString());

            using (var log = new StringWriter())
                using (var muxer = ConnectionMultiplexer.Connect(config, log))
                {
                    Output.WriteLine("Connect log:");
                    Output.WriteLine(log.ToString());
                    Output.WriteLine("====");
                    muxer.ConnectionFailed += OnConnectionFailed;
                    muxer.InternalError    += OnInternalError;
                    var db = muxer.GetDatabase();
                    db.Ping();
                    using (var file = File.Create("ssl-" + useSsl + "-" + specifyHost + ".zip"))
                    {
                        muxer.ExportConfiguration(file);
                    }
                    RedisKey key = "SE.Redis";

                    const int AsyncLoop = 2000;
                    // perf; async
                    db.KeyDelete(key, CommandFlags.FireAndForget);
                    var watch = Stopwatch.StartNew();
                    for (int i = 0; i < AsyncLoop; i++)
                    {
                        db.StringIncrement(key, flags: CommandFlags.FireAndForget);
                    }
                    // need to do this inside the timer to measure the TTLB
                    long value = (long)db.StringGet(key);
                    watch.Stop();
                    Assert.Equal(AsyncLoop, value);
                    Output.WriteLine("F&F: {0} INCR, {1:###,##0}ms, {2} ops/s; final value: {3}",
                                     AsyncLoop,
                                     (long)watch.ElapsedMilliseconds,
                                     (long)(AsyncLoop / watch.Elapsed.TotalSeconds),
                                     value);

                    // perf: sync/multi-threaded
                    TestConcurrent(db, key, 30, 10);
                    //TestConcurrent(db, key, 30, 20);
                    //TestConcurrent(db, key, 30, 30);
                    //TestConcurrent(db, key, 30, 40);
                    //TestConcurrent(db, key, 30, 50);
                }
        }