def test_duplicate_prefix_registration(self): sentinel = SentinelExtension() sentinel2 = SentinelExtension() sentinel.init_app(self.app) with self.assertRaisesRegexp(ValueError, 'Config prefix REDIS already registered'): sentinel2.init_app(self.app)
def test_default_connection_with_init_class(self): sentinel = SentinelExtension() sentinel.init_app(self.app, client_class=FakeRedis) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['url'], 'redis://localhost/0')
def connect_redis(self): self.logger.debug("Connecting to redis...") if self.app.testing: self.logger.info("Configuring Fake Redis...") import fakeredis self.app.redis = FlaskRedis.from_custom_provider( fakeredis.FakeStrictRedis) self.app.redis.connect = self._mock_redis(True) self.app.redis.disconnect = self._mock_redis(False) self.app.redis.init_app(self.app) elif self.app.config["REDIS_URL"].startswith("redis+sentinel"): self.logger.info("Configuring Redis Sentinel...", redis_url=self.app.config["REDIS_URL"]) redis_sentinel = SentinelExtension() redis_connection = redis_sentinel.default_connection redis_sentinel.init_app(self.app) self.app.redis = redis_connection else: self.logger.info("Configuring Redis...", redis_url=self.app.config["REDIS_URL"]) self.app.redis = FlaskRedis() self.app.redis.init_app(self.app) self.logger.info("Connection to redis successful")
def test_sentinel_kwargs_from_config(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' self.app.config['REDIS_SENTINEL_SOCKET_CONNECT_TIMEOUT'] = 0.3 sentinel.init_app(self.app) with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) self.assertEquals(sentinel.sentinel.sentinel_kwargs, {'socket_connect_timeout': 0.3})
def test_default_connection_with_config_class_string(self): sentinel = SentinelExtension() self.app.config['REDIS_CLASS'] = 'test_flask_redis_sentinel.FakeRedis' sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['url'], 'redis://localhost/0')
def test_unsupported_url_scheme(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config[ 'REDIS_URL'] = 'redis+something://hostname:7001/myslave/3?slave=true' with self.assertRaisesRegexp( ValueError, r'Unsupported redis URL scheme: redis\+something'): sentinel.init_app(self.app)
def test_duplicate_prefix_registration(self): sentinel = SentinelExtension() sentinel2 = SentinelExtension() sentinel.init_app(self.app) with self.assertRaisesRegexp(ValueError, 'Config prefix REDIS already registered'): sentinel2.init_app(self.app)
def test_default_connection_with_init_class(self): sentinel = SentinelExtension() sentinel.init_app(self.app, client_class=FakeRedis) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['host'], 'localhost') self.assertEqual(inst.kwargs['port'], 6379) self.assertEqual(inst.kwargs['db'], 0)
def test_default_connection(self): sentinel = SentinelExtension(client_class=FakeRedis) sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['host'], 'localhost') self.assertEqual(inst.kwargs['port'], 6379) self.assertEqual(inst.kwargs['db'], 0)
def test_named_slave_no_sentinel(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis://hostname:7001/3' sentinel.init_app(self.app) conn = sentinel.slave_for('otherslave', db=6) with self.app.app_context(): self.assertIsNone(sentinel.sentinel._get_current_object()) with self.assertRaisesRegexp(ValueError, 'Cannot get slave otherslave using non-sentinel configuration'): inst = conn._get_current_object()
def test_default_connection_with_init_sentinel_class(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' sentinel.init_app(self.app, sentinel_class=FakeSentinel) conn = sentinel.default_connection with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], True)
def test_duplicate_prefix_registration(self): sentinel = SentinelExtension() sentinel2 = SentinelExtension() sentinel.init_app(self.app) msg = 'Redis sentinel extension with config prefix REDIS is already registered' with self.assertRaisesRegexp(RuntimeError, msg): sentinel2.init_app(self.app)
def test_default_connection_with_init_sentinel_class(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config[ 'REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' sentinel.init_app(self.app, sentinel_class=FakeSentinel) conn = sentinel.default_connection with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], True)
def test_default_connection_with_config_class_string(self): sentinel = SentinelExtension() self.app.config['REDIS_CLASS'] = 'test_flask_redis_sentinel.FakeRedis' sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['host'], 'localhost') self.assertEqual(inst.kwargs['port'], 6379) self.assertEqual(inst.kwargs['db'], 0)
def test_default_connection_redis_vars(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_URL'] = 'redis://hostname:7001/3' self.app.config['REDIS_DECODE_RESPONSES'] = True sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['url'], 'redis://hostname:7001/3') self.assertEqual(inst.kwargs['decode_responses'], True)
def test_sentinel_kwargs_from_config(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config[ 'REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' self.app.config['REDIS_SENTINEL_SOCKET_CONNECT_TIMEOUT'] = 0.3 sentinel.init_app(self.app) with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) self.assertEquals(sentinel.sentinel.sentinel_kwargs, {'socket_connect_timeout': 0.3})
def test_named_slave_no_sentinel(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis://hostname:7001/3' sentinel.init_app(self.app) conn = sentinel.slave_for('otherslave', db=6) with self.app.app_context(): self.assertIsNone(sentinel.sentinel._get_current_object()) with self.assertRaisesRegexp( RuntimeError, 'Cannot get slave otherslave using non-sentinel configuration' ): inst = conn._get_current_object()
def test_named_slave(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' self.app.config['REDIS_DECODE_RESPONSES'] = True sentinel.init_app(self.app) conn = sentinel.slave_for('otherslave', db=6) with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], False) self.assertEqual(inst.kwargs['service_name'], 'otherslave') self.assertEqual(inst.kwargs['connection_kwargs']['db'], 6) self.assertEqual(inst.kwargs['connection_kwargs']['decode_responses'], True)
def test_redis_threads(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis://hostname:7001/0' sentinel.init_app(self.app) connections = self._check_threads(sentinel) self.assertIs(connections['from_another_thread'], connections['from_main_thread']) self.assertIs(connections['from_another_thread'], connections['from_main_thread_later']) self.assertIs(connections['from_main_thread'], connections['from_main_thread_later'])
def test_default_connection_redis_url(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_URL'] = 'redis://hostname:7001/3' self.app.config['REDIS_HOST'] = 'ignored' # should be ignored self.app.config['REDIS_PORT'] = 5000 # should be ignored self.app.config['REDIS_DB'] = 7 # should be ignored sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['url'], 'redis://hostname:7001/3') self.assertNotIn('host', inst.kwargs) self.assertNotIn('port', inst.kwargs) self.assertNotIn('db', inst.kwargs)
def test_default_connection_redis_vars(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_HOST'] = 'hostname' self.app.config['REDIS_PORT'] = 7001 self.app.config['REDIS_DB'] = 3 self.app.config['REDIS_DECODE_RESPONSES'] = True sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['host'], 'hostname') self.assertEqual(inst.kwargs['port'], 7001) self.assertEqual(inst.kwargs['db'], 3) self.assertEqual(inst.kwargs['decode_responses'], True)
def test_default_connection_redis_vars(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_HOST'] = 'hostname' self.app.config['REDIS_PORT'] = 7001 self.app.config['REDIS_DB'] = 3 self.app.config['REDIS_DECODE_RESPONSES'] = True sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['host'], 'hostname') self.assertEqual(inst.kwargs['port'], 7001) self.assertEqual(inst.kwargs['db'], 3) self.assertEqual(inst.kwargs['decode_responses'], True)
def test_default_connection_redis_url(self): sentinel = SentinelExtension(client_class=FakeRedis) self.app.config['REDIS_URL'] = 'redis://hostname:7001/3' self.app.config['REDIS_HOST'] = 'ignored' # should be ignored self.app.config['REDIS_PORT'] = 5000 # should be ignored self.app.config['REDIS_DB'] = 7 # should be ignored sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): inst = conn._get_current_object() self.assertEqual(inst.kwargs['url'], 'redis://hostname:7001/3') self.assertNotIn('host', inst.kwargs) self.assertNotIn('port', inst.kwargs) self.assertNotIn('db', inst.kwargs)
def test_default_connection_sentinel_url_slave(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis+sentinel://hostname:7001/myslave/3?client_type=slave' self.app.config['REDIS_DECODE_RESPONSES'] = True self.app.config['REDIS_SENTINEL_SOCKET_CONNECT_TIMEOUT'] = 0.3 sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], False) self.assertEqual(inst.kwargs['service_name'], 'myslave') self.assertEqual(inst.kwargs['connection_kwargs']['db'], 3) self.assertEqual(inst.kwargs['connection_kwargs']['decode_responses'], True)
def test_named_slave(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config[ 'REDIS_URL'] = 'redis+sentinel://hostname:7001/mymaster/3' self.app.config['REDIS_DECODE_RESPONSES'] = True sentinel.init_app(self.app) conn = sentinel.slave_for('otherslave', db=6) with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], False) self.assertEqual(inst.kwargs['service_name'], 'otherslave') self.assertEqual(inst.kwargs['connection_kwargs']['db'], 6) self.assertEqual( inst.kwargs['connection_kwargs']['decode_responses'], True)
def test_default_connection_sentinel_url_slave(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config[ 'REDIS_URL'] = 'redis+sentinel://hostname:7001/myslave/3?client_type=slave' self.app.config['REDIS_DECODE_RESPONSES'] = True self.app.config['REDIS_SENTINEL_SOCKET_CONNECT_TIMEOUT'] = 0.3 sentinel.init_app(self.app) conn = sentinel.default_connection with self.app.app_context(): self.assertIsNotNone(sentinel.sentinel) inst = conn._get_current_object() self.assertEqual(inst.kwargs['is_master'], False) self.assertEqual(inst.kwargs['service_name'], 'myslave') self.assertEqual(inst.kwargs['connection_kwargs']['db'], 3) self.assertEqual( inst.kwargs['connection_kwargs']['decode_responses'], True)
def test_multiple_prefix_registration(self): sentinel = SentinelExtension() sentinel2 = SentinelExtension() sentinel.init_app(self.app) sentinel2.init_app(self.app, config_prefix='ANOTHER_REDIS')
def test_multiple_prefix_registration(self): sentinel = SentinelExtension() sentinel2 = SentinelExtension() sentinel.init_app(self.app) sentinel2.init_app(self.app, config_prefix='ANOTHER_REDIS')
if val is None: return default_value if val.lower() == "true": return True if val.lower() == "false": return False app.logger.warn("ignoring value for: %s, should be either true/false", env_entry_name) return default_value configure() redis_sentinel.init_app(app) Bootstrap(app) class MemtierThread(threading.Thread): def __init__(self, master_ip, master_port, redis_password=None, argument_line="", **kwargs): try: # Python 3.x super().__init__(**kwargs) except TypeError: # Python 2.x
def test_unsupported_url_scheme(self): sentinel = SentinelExtension(client_class=FakeRedis, sentinel_class=FakeSentinel) self.app.config['REDIS_URL'] = 'redis+something://hostname:7001/myslave/3?slave=true' with self.assertRaisesRegexp(ValueError, r'Unsupported redis URL scheme: redis\+something'): sentinel.init_app(self.app)