Esempio n. 1
0
        public void Create_CanReturnConnectionMultiplexer()
        {
            // arrange
            RedisCacheConnectorOptions config = new RedisCacheConnectorOptions()
            {
                Host               = "localhost",
                Port               = 1234,
                Password           = "******",
                InstanceName       = "instanceId",
                AbortOnConnectFail = false,
                ConnectTimeout     = 1
            };
            RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "127.0.0.1", 4321, "sipassword")
            {
                ApplicationInfo = new ApplicationInstanceInfo()
                {
                    InstanceId = "instanceId"
                }
            };

            // act
            var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), RedisTypeLocator.StackExchangeInitializer);
            var multi   = factory.Create(null);

            // assert
            Assert.NotNull(multi);
            Assert.IsType <ConnectionMultiplexer>(multi);
        }
        public void Create_CanReturnConnectionMultiplexer()
        {
            // arrange
            RedisCacheConnectorOptions config = new RedisCacheConnectorOptions()
            {
                Host               = "localhost",
                Port               = 1234,
                Password           = "******",
                InstanceName       = "instanceId",
                AbortOnConnectFail = false,
                ConnectTimeout     = 1
            };
            RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword")
            {
                ApplicationInfo = new ApplicationInstanceInfo()
                {
                    InstanceId = "instanceId"
                }
            };
            MethodInfo initializer = typeof(ConnectionMultiplexer).GetMethod("Connect", new Type[] { typeof(ConfigurationOptions), typeof(TextWriter) });

            // act
            var factory = new RedisServiceConnectorFactory(si, config, typeof(ConnectionMultiplexer), typeof(ConfigurationOptions), initializer);
            var multi   = factory.Create(null);

            // assert
            Assert.NotNull(multi);
            Assert.IsType <ConnectionMultiplexer>(multi);
        }
Esempio n. 3
0
        public void Create_CanReturnRedisCache()
        {
            // arrange
            RedisCacheConnectorOptions config = new RedisCacheConnectorOptions()
            {
                Host         = "localhost",
                Port         = 1234,
                Password     = "******",
                InstanceName = "instanceId"
            };
            RedisServiceInfo si = new RedisServiceInfo("myId", RedisServiceInfo.REDIS_SCHEME, "foobar", 4321, "sipassword")
            {
                ApplicationInfo = new ApplicationInstanceInfo()
                {
                    InstanceId = "instanceId"
                }
            };

            // act
            var factory = new RedisServiceConnectorFactory(si, config, typeof(RedisCache), typeof(RedisCacheOptions), null);
            var cache   = factory.Create(null);

            // assert
            Assert.NotNull(cache);
            Assert.IsType <RedisCache>(cache);
        }
        /// <summary>
        /// Adds ConnectionMultiplexer (as ConnectionMultiplexer and IConnectionMultiplexer) to your Autofac Container
        /// </summary>
        /// <param name="container">Your Autofac Container Builder</param>
        /// <param name="config">Application configuration</param>
        /// <param name="serviceName">Cloud Foundry service name binding</param>
        /// <returns>the RegistrationBuilder for (optional) additional configuration</returns>
        public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterRedisConnectionMultiplexer(
            this ContainerBuilder container,
            IConfiguration config,
            string serviceName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            Type       redisInterface      = RedisTypeLocator.StackExchangeInterface;
            Type       redisImplementation = RedisTypeLocator.StackExchangeImplementation;
            Type       redisOptions        = RedisTypeLocator.StackExchangeOptions;
            MethodInfo initializer         = RedisTypeLocator.StackExchangeInitializer;

            RedisServiceInfo info = serviceName != null
                ? config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName)
                : config.GetSingletonServiceInfo <RedisServiceInfo>();

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            container.Register(c => new RedisHealthContributor(factory, redisImplementation, c.ResolveOptional <ILogger <RedisHealthContributor> >())).As <IHealthContributor>();
            return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation));
        }
        public void Microsoft_Is_Connected_Returns_Up_Status()
        {
            var redisOptions = new RedisCacheConnectorOptions();
            var sInfo        = new RedisServiceInfo("MyId", "redis://localhost:6379");
            var logrFactory  = new LoggerFactory();
            var connFactory  = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.MicrosoftImplementation, RedisTypeLocator.MicrosoftOptions, null);
            var h            = new RedisHealthContributor(connFactory, RedisTypeLocator.MicrosoftImplementation, logrFactory.CreateLogger <RedisHealthContributor>());

            var status = h.Health();

            Assert.Equal(HealthStatus.UP, status.Status);
        }
        public void StackExchange_Is_Connected_Returns_Up_Status()
        {
            // arrange
            var redisOptions = new RedisCacheConnectorOptions();
            var sInfo        = new RedisServiceInfo("MyId", "redis://localhost:6379");
            var logrFactory  = new LoggerFactory();
            var connFactory  = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.StackExchangeImplementation, RedisTypeLocator.StackExchangeOptions, RedisTypeLocator.StackExchangeInitializer);
            var h            = new RedisHealthContributor(connFactory, RedisTypeLocator.StackExchangeImplementation, logrFactory.CreateLogger <RedisHealthContributor>());

            // act
            var status = h.Health();

            // assert
            Assert.Equal(HealthStatus.UP, status.Status);
        }
        public void Microsoft_Not_Connected_Returns_Down_Status()
        {
            var redisOptions = new RedisCacheConnectorOptions()
            {
                ConnectTimeout = 1
            };
            var sInfo       = new RedisServiceInfo("MyId", "redis://localhost:6378");
            var logrFactory = new LoggerFactory();
            var connFactory = new RedisServiceConnectorFactory(sInfo, redisOptions, RedisTypeLocator.MicrosoftImplementation, RedisTypeLocator.MicrosoftOptions, null);
            var h           = new RedisHealthContributor(connFactory, RedisTypeLocator.MicrosoftImplementation, logrFactory.CreateLogger <RedisHealthContributor>());

            var status = h.Health();

            Assert.Equal(HealthStatus.DOWN, status.Status);
            Assert.Equal("Redis health check failed", status.Description);
        }
        public void Create_ReturnsRedisCache()
        {
            RedisCacheConfiguration config = new RedisCacheConfiguration()
            {
                Host       = "localhost",
                Port       = 1234,
                Password   = "******",
                InstanceId = "instanceId"
            };
            RedisServiceInfo si = new RedisServiceInfo("myId", "foobar", 4321, "sipassword");

            si.ApplicationInfo = new ApplicationInstanceInfo()
            {
                InstanceId = "instanceId"
            };
            var factory = new RedisServiceConnectorFactory(si, config);
            var cache   = factory.Create(null);

            Assert.NotNull(cache);
        }
Esempio n. 9
0
        /// <summary>
        /// Adds ConnectionMultiplexer (as ConnectionMultiplexer and IConnectionMultiplexer) to your Autofac Container
        /// </summary>
        /// <param name="container">Your Autofac Container Builder</param>
        /// <param name="config">Application configuration</param>
        /// <param name="serviceName">Cloud Foundry service name binding</param>
        /// <returns>the RegistrationBuilder for (optional) additional configuration</returns>
        public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterRedisConnectionMultiplexer(
            this ContainerBuilder container,
            IConfiguration config,
            string serviceName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            Type       redisInterface      = RedisTypeLocator.StackExchangeRedisInterface;
            Type       redisImplementation = RedisTypeLocator.StackExchangeRedisImplementation;
            Type       redisOptions        = RedisTypeLocator.StackExchangeRedisOptions;
            MethodInfo initializer         = RedisTypeLocator.StackExchangeInitializer;

            if (redisInterface == null || redisImplementation == null || redisOptions == null || initializer == null)
            {
                throw new ConnectorException("Unable to find required Redis types, are you missing a StackExchange.Redis Nuget Package?");
            }

            RedisServiceInfo info;

            if (serviceName != null)
            {
                info = config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName);
            }
            else
            {
                info = config.GetSingletonServiceInfo <RedisServiceInfo>();
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null);

            return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation));
        }
        public void CreateConnection_ReturnsConnectinMultiplexer()
        {
            RedisCacheConnectorOptions config = new RedisCacheConnectorOptions()
            {
                Host               = "localhost",
                Port               = 1234,
                Password           = "******",
                InstanceId         = "instanceId",
                AbortOnConnectFail = false
            };
            RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword");

            si.ApplicationInfo = new ApplicationInstanceInfo()
            {
                InstanceId = "instanceId"
            };
            var factory = new RedisServiceConnectorFactory(si, config);
            var multi   = factory.CreateConnection(null);

            Assert.NotNull(multi);
        }
Esempio n. 11
0
        /// <summary>
        /// Adds RedisCache (as IDistributedCache and RedisCache) to your Autofac Container
        /// </summary>
        /// <param name="container">Your Autofac Container Builder</param>
        /// <param name="config">Application configuration</param>
        /// <param name="serviceName">Cloud Foundry service name binding</param>
        /// <returns>the RegistrationBuilder for (optional) additional configuration</returns>
        public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterDistributedRedisCache(
            this ContainerBuilder container,
            IConfiguration config,
            string serviceName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            Type redisInterface      = RedisTypeLocator.MicrosoftRedisInterface;
            Type redisImplementation = RedisTypeLocator.MicrosoftRedisImplementation;
            Type redisOptions        = RedisTypeLocator.MicrosoftRedisOptions;

            if (redisInterface == null || redisImplementation == null || redisOptions == null)
            {
                throw new ConnectorException("Unable to find required Redis types, are you missing the Microsoft.Extensions.Caching.Redis Nuget package?");
            }

            RedisServiceInfo info;

            if (serviceName != null)
            {
                info = config.GetRequiredServiceInfo <RedisServiceInfo>(serviceName);
            }
            else
            {
                info = config.GetSingletonServiceInfo <RedisServiceInfo>();
            }

            RedisCacheConnectorOptions   redisConfig = new RedisCacheConnectorOptions(config);
            RedisServiceConnectorFactory factory     = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, null);

            return(container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation));
        }