def test_load_balanced_read_apps(self, mock): load_balanced_apps = { 'users': [ ('users_db1', 5), ] } with override_settings(LOAD_BALANCED_APPS=load_balanced_apps, DATABASES={ 'default': _get_db_config('default'), 'users_db1': _get_db_config('users_db1') }): manager = ConnectionManager() self.assertEqual( manager.get_load_balanced_read_db_alais( 'users', default="default_option"), 'users_db1') with override_settings(LOAD_BALANCED_APPS=load_balanced_apps): # load_balanced_db should be part of settings.DATABASES with self.assertRaises(AssertionError): ConnectionManager().get_load_balanced_read_db_alais('users') # If `LOAD_BALANCED_APPS` is not set for an app, it should point to default kwarg manager = ConnectionManager() self.assertEqual( manager.get_load_balanced_read_db_alais('users', default='default_option'), 'default_option')
def test_read_load_balancing(self, *args): reporting_dbs = { 'ucr': { 'WRITE': 'ucr', 'READ': [('ucr', 8), ('other', 1), ('default', 1)] }, } with override_settings(REPORTING_DATABASES=reporting_dbs): manager = ConnectionManager() self.assertEqual(manager.db_connection_map, { 'default': 'postgresql+psycopg2://:@localhost:5432/default', 'ucr': 'postgresql+psycopg2://:@localhost:5432/ucr', 'other': 'postgresql+psycopg2://:@localhost:5432/other' }) # test that load balancing works with a 10% margin for randomness total_requests = 10000 randomness_margin = total_requests * 0.1 total_weighting = sum(db[1] for db in reporting_dbs['ucr']['READ']) expected = { alias: weight * total_requests // total_weighting for alias, weight in reporting_dbs['ucr']['READ'] } balanced = Counter(manager.get_load_balanced_read_db_alais('ucr') for i in range(total_requests)) for db, requests in balanced.items(): self.assertAlmostEqual(requests, expected[db], delta=randomness_margin) with override_settings(REPORTING_DATABASES={'default': 'default'}): manager = ConnectionManager() self.assertEqual( ['default', 'default', 'default'], [manager.get_load_balanced_read_db_alais('default') for i in range(3)] )
def test_load_balanced_read_apps(self, mock): load_balanced_apps = { 'users': [ ('users_db1', 5), ] } with override_settings( LOAD_BALANCED_APPS=load_balanced_apps, DATABASES = { 'default': _get_db_config('default'), 'users_db1': _get_db_config('users_db1')}): manager = ConnectionManager() self.assertEqual( manager.get_load_balanced_read_db_alais('users', default="default_option"), 'users_db1' ) with override_settings(LOAD_BALANCED_APPS=load_balanced_apps): # load_balanced_db should be part of settings.DATABASES with self.assertRaises(AssertionError): ConnectionManager().get_load_balanced_read_db_alais('users') # If `LOAD_BALANCED_APPS` is not set for an app, it should point to default kwarg manager = ConnectionManager() self.assertEqual( manager.get_load_balanced_read_db_alais('users', default='default_option'), 'default_option' )
def test_read_load_balancing(self, *args): reporting_dbs = { 'ucr': { 'WRITE': 'ucr', 'READ': [('ucr', 8), ('other', 1), ('default', 1)] }, } with override_settings(REPORTING_DATABASES=reporting_dbs): manager = ConnectionManager() self.assertEqual( manager.db_connection_map, { 'default': 'postgresql+psycopg2://:@localhost:5432/default', 'ucr': 'postgresql+psycopg2://:@localhost:5432/ucr', 'other': 'postgresql+psycopg2://:@localhost:5432/other' }) # test that load balancing works with a 10% margin for randomness total_requests = 10000 randomness_margin = total_requests * 0.1 total_weighting = sum(db[1] for db in reporting_dbs['ucr']['READ']) expected = { alias: weight * total_requests // total_weighting for alias, weight in reporting_dbs['ucr']['READ'] } balanced = Counter( manager.get_load_balanced_read_db_alais('ucr') for i in range(total_requests)) for db, requests in balanced.items(): self.assertAlmostEqual(requests, expected[db], delta=randomness_margin) with override_settings(REPORTING_DATABASES={'default': 'default'}): manager = ConnectionManager() self.assertEqual(['default', 'default', 'default'], [ manager.get_load_balanced_read_db_alais('default') for i in range(3) ])
def test_standby_filtering(self, *args): reporting_dbs = { 'ucr_engine': { 'WRITE': 'ucr', 'READ': [('ucr', 8), ('other', 1)] }, } with override_settings(REPORTING_DATABASES=reporting_dbs): # should always return the `other` db since `ucr` has bad replication delay manager = ConnectionManager() self.assertEqual(['other', 'other', 'other'], [ manager.get_load_balanced_read_db_alais('ucr_engine') for i in range(3) ])
def test_standby_filtering(self, *args): reporting_dbs = { 'ucr_engine': { 'WRITE': 'ucr', 'READ': [('ucr', 8), ('other', 1)] }, } with override_settings(REPORTING_DATABASES=reporting_dbs): # should always return the `other` db since `ucr` has bad replication delay manager = ConnectionManager() self.assertEqual( ['other', 'other', 'other'], [manager.get_load_balanced_read_db_alais('ucr_engine') for i in range(3)] )