Skip to content

Commit 0b23951

Browse files
committed
Fix parallel build
1 parent 26eb13b commit 0b23951

9 files changed

+79
-64
lines changed

conftest.py

+45-23
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,8 @@ def mn_client_v1_v2(request):
310310
@pytest.fixture(scope='session', autouse=True)
311311
def set_unique_sciobj_store_path(request):
312312
tmp_store_path = os.path.join(
313-
tempfile.gettempdir(), 'gmn_test_obj_store_{}{}'.format(
314-
d1_test.instance_generator.random_data.random_lower_ascii(
315-
min_len=12, max_len=12
316-
), get_xdist_suffix(request)
317-
)
313+
tempfile.gettempdir(),
314+
'gmn_test_obj_store_{}'.format(get_xdist_unique_suffix(request))
318315
)
319316
django.conf.settings.OBJECT_STORE_PATH = tmp_store_path
320317
d1_gmn.app.sciobj_store.create_clean_tmp_store()
@@ -326,46 +323,50 @@ def set_unique_sciobj_store_path(request):
326323
@pytest.yield_fixture(scope='session')
327324
def django_db_setup(request, django_db_blocker):
328325
"""Set up DB fixture
326+
When running in parallel with xdist, this is called once for each worker.
327+
By default, the number of workers is the same as the number of CPU cores.
329328
"""
330329
logging.info('Setting up DB fixture')
331330

332331
test_db_key = 'default'
333-
test_db_name = django.conf.settings.DATABASES[test_db_key]['NAME']
334-
test_db_name += get_xdist_suffix(request)
332+
test_db_name = ''.join([
333+
django.conf.settings.DATABASES[test_db_key]['NAME'],
334+
get_xdist_unique_suffix(request),
335+
])
335336
django.conf.settings.DATABASES[test_db_key]['NAME'] = test_db_name
336337

337338
template_db_key = 'template'
338339
template_db_name = django.conf.settings.DATABASES[template_db_key]['NAME']
339340

340341
with django_db_blocker.unblock():
342+
341343
if pytest.config.getoption('--fixture-regen'):
342344
drop_database(test_db_name)
343345
create_blank_db(test_db_key, test_db_name)
344346
django.db.connections[test_db_key].commit()
345347
pytest.exit('Database dropped and reinitialized. Now run mk_db_fixture')
346348

347-
try:
348-
load_template_fixture(template_db_key, template_db_name)
349-
except psycopg2.DatabaseError as e:
350-
logging.error(str(e))
349+
# try:
350+
# load_template_fixture(template_db_key, template_db_name)
351+
# except psycopg2.DatabaseError as e:
352+
# logging.error(str(e))
353+
351354
drop_database(test_db_name)
352355
create_db_from_template(test_db_name, template_db_name)
353-
# Haven't found out how to prevent transactions from being started, so
354-
# closing the implicit transaction here so that template fixture remains
355-
# available.
356-
django.db.connections[test_db_key].commit()
356+
357+
# # Haven't found out how to prevent transactions from being started, so
358+
# # closing the implicit transaction here so that template fixture remains
359+
# # available.
360+
# django.db.connections[test_db_key].commit()
361+
357362
migrate_db(test_db_key)
358-
logging.debug('Test DB ready')
363+
359364
yield
360365

366+
for connection in django.db.connections.all():
367+
connection.close()
361368

362-
def get_xdist_suffix(request):
363-
"""When running in parallel with xdist, each thread gets a different suffix.
364-
- In parallel run, return '_gw1', etc.
365-
- In single run, return ''.
366-
"""
367-
s = getattr(request.config, 'slaveinput', {}).get('slaveid')
368-
return '_{}'.format(s) if s is not None else ''
369+
drop_database(test_db_name)
369370

370371

371372
def create_db_from_template(test_db_name, template_db_name):
@@ -407,6 +408,10 @@ def migrate_db(test_db_key):
407408

408409
def drop_database(db_name):
409410
logging.debug('Dropping database: {}'.format(db_name))
411+
412+
for connection in django.db.connections.all():
413+
connection.close()
414+
410415
run_sql('postgres', 'drop database if exists {};'.format(db_name))
411416

412417

@@ -436,3 +441,20 @@ def run_sql(db, sql):
436441
conn.close()
437442
for connection in django.db.connections.all():
438443
connection.close()
444+
445+
446+
def get_xdist_unique_suffix(request):
447+
return ''.join([
448+
d1_test.instance_generator.random_data.random_lower_ascii(
449+
min_len=12, max_len=12
450+
), get_xdist_suffix(request)
451+
])
452+
453+
454+
def get_xdist_suffix(request):
455+
"""When running in parallel with xdist, each thread gets a different suffix.
456+
- In parallel run, return '_gw1', etc.
457+
- In single run, return ''.
458+
"""
459+
s = getattr(request.config, 'slaveinput', {}).get('slaveid')
460+
return '_{}'.format(s) if s is not None else ''

gmn/src/d1_gmn/tests/gmn_mock.py

+9
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,12 @@ def disable_management_command_logging():
245245
log messages when the commands are launched multiple times"""
246246
with mock.patch('d1_gmn.app.management.commands._util.log_setup'):
247247
yield
248+
249+
250+
@contextlib2.contextmanager
251+
def disable_management_command_concurrent_instance_check():
252+
"""Allow concurrent instances of the same management command"""
253+
with mock.patch(
254+
'd1_gmn.app.management.commands._util.exit_if_other_instance_is_running'
255+
):
256+
yield

gmn/src/d1_gmn/tests/gmn_test_case.py

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import d1_client.mnclient_2_0
6565
import d1_client.session
6666

67+
import django.core.management
6768
import django.db
6869
import django.test
6970

@@ -534,3 +535,9 @@ def get_random_pid_sample(self, n_pids):
534535
],
535536
n_pids,
536537
)
538+
539+
def call_management_command(self, *args, **kwargs):
540+
with self.mock.disable_management_command_logging():
541+
with self.mock.disable_management_command_concurrent_instance_check():
542+
#with d1_test.d1_test_case.disable_debug_level_logging():
543+
django.core.management.call_command(*args, **kwargs)

gmn/src/d1_gmn/tests/test_mgmt_cert.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ def _cert_whitelist(self, caplog):
3838
)
3939
with self.mock.disable_management_command_logging():
4040
with d1_test.d1_test_case.disable_debug_level_logging():
41-
django.core.management.call_command('cert', 'whitelist', cert_path)
42-
django.core.management.call_command('whitelist', 'view', cert_path)
41+
self.call_management_command('cert', 'whitelist', cert_path)
42+
self.call_management_command('whitelist', 'view', cert_path)
4343
return d1_test.d1_test_case.get_caplog_text(caplog)
4444

4545
def test_1000(self, caplog):
4646
"""cert view <pem>: Lists subjects from DN and SubjectInfo
4747
"""
4848
cert_path = self.sample.get_path('cert_with_full_subject_info.pem')
49-
with self.mock.disable_management_command_logging():
50-
with d1_test.d1_test_case.disable_debug_level_logging():
51-
django.core.management.call_command('cert', 'view', cert_path)
49+
self.call_management_command('cert', 'view', cert_path)
5250
self.sample.assert_equals(
5351
d1_test.d1_test_case.get_caplog_text(caplog), 'view'
5452
)

gmn/src/d1_gmn/tests/test_mgmt_diag_export_object_list.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727

2828
import d1_gmn.tests.gmn_test_case
2929

30-
import d1_test.d1_test_case
31-
32-
import django.core.management
33-
import django.utils.six
34-
3530

3631
# TODO:
3732
@pytest.mark.skip('Disabled until move to "diag" mgmt cmd completed')
@@ -40,11 +35,9 @@ def test_1000(self):
4035
"""diag_export_object_list
4136
"""
4237
with tempfile.NamedTemporaryFile() as exp_f:
43-
with self.mock.disable_management_command_logging():
44-
with d1_test.d1_test_case.disable_debug_level_logging():
45-
django.core.management.call_command(
46-
'diag_export_object_list', '--limit', 10, exp_f.name
47-
)
38+
self.call_management_command(
39+
'diag_export_object_list', '--limit', 10, exp_f.name
40+
)
4841
exp_f.seek(0)
4942
obj_list = exp_f.read()
5043
self.sample.assert_equals(obj_list, 'diag_export_object_list')

gmn/src/d1_gmn/tests/test_mgmt_diag_repair_revision_chains.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,13 @@
3131

3232
import d1_test.d1_test_case
3333

34-
import django.core.management
35-
import django.utils.six
36-
3734

3835
# TODO:
3936
@pytest.mark.skip('Disabled until move to "diag" mgmt cmd completed')
4037
@d1_test.d1_test_case.reproducible_random_decorator('TestMgmtFixChains')
4138
class TestMgmtFixChains(d1_gmn.tests.gmn_test_case.GMNTestCase):
4239
def _call_diag_repair_revision_chains(self):
43-
with self.mock.disable_management_command_logging():
44-
with d1_test.d1_test_case.disable_debug_level_logging():
45-
django.core.management.call_command('diag_repair_revision_chains')
40+
self.call_management_command('diag_repair_revision_chains')
4641

4742
def _get_rev_list(self):
4843
return [(

gmn/src/d1_gmn/tests/test_mgmt_diag_update_sysmeta.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
import d1_test.d1_test_case
3636
import d1_test.instance_generator.system_metadata
3737

38-
import django.core.management
39-
import django.utils.six
40-
4138
N_SYSMETA_DOCS = 10
4239

4340

@@ -62,11 +59,9 @@ def _create_test_dir_with_sysmeta_docs(self, client):
6259
return tmp_dir_path, tmp_file_list, rnd_pid_list
6360

6461
def _call_diag_update_sysmeta(self, *args, **kwargs):
65-
with self.mock.disable_management_command_logging():
66-
with d1_test.d1_test_case.disable_debug_level_logging():
67-
django.core.management.call_command(
68-
'diag_update_sysmeta', '--debug', *args, **kwargs
69-
)
62+
self.call_management_command(
63+
'diag_update_sysmeta', '--debug', *args, **kwargs
64+
)
7065

7166
def test_1000(self, mn_client_v2):
7267
"""diag_update_sysmeta: local XML docs

gmn/src/d1_gmn/tests/test_mgmt_import.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import d1_test.mock_api.get_system_metadata as mock_get_system_metadata
3434
import d1_test.mock_api.list_objects as mock_object_list
3535

36-
import django
37-
import django.core.management
38-
3936

4037
@d1_test.d1_test_case.reproducible_random_decorator('TestMgmtImport')
4138
class TestMgmtImport(d1_gmn.tests.gmn_test_case.GMNTestCase):
@@ -49,12 +46,10 @@ def test_1000(self, caplog):
4946
)
5047
mock_get.add_callback(d1_test.d1_test_case.MOCK_REMOTE_BASE_URL)
5148

52-
with self.mock.disable_management_command_logging():
53-
with d1_test.d1_test_case.disable_debug_level_logging():
54-
django.core.management.call_command(
55-
'import', '--force', '--major=2',
56-
d1_test.d1_test_case.MOCK_REMOTE_BASE_URL
57-
)
49+
self.call_management_command(
50+
'import', '--force', '--major=2',
51+
d1_test.d1_test_case.MOCK_REMOTE_BASE_URL
52+
)
5853

5954
self.sample.assert_equals(
6055
d1_test.d1_test_case.get_caplog_text(caplog), 'import'

gmn/src/d1_gmn/tests/test_system_metadata_changed.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636

3737
import d1_common
3838
import d1_common.date_time
39+
import d1_common.types.exceptions
3940
import d1_common.xml
4041

4142
import d1_test.d1_test_case
4243
import d1_test.mock_api.get_system_metadata
4344

4445
import django
46+
import django.conf
47+
import django.core.management
4548

4649

4750
@d1_test.d1_test_case.reproducible_random_decorator('TestSystemMetadataChanged')
@@ -51,9 +54,7 @@ def _call_process_refresh_queue(self):
5154
d1_test.mock_api.get_system_metadata.add_callback(
5255
django.conf.settings.DATAONE_ROOT
5356
)
54-
with self.mock.disable_management_command_logging():
55-
with d1_test.d1_test_case.disable_debug_level_logging():
56-
django.core.management.call_command('process_refresh_queue', '--debug')
57+
self.call_management_command('process_refresh_queue', '--debug')
5758

5859
@responses.activate
5960
def test_1000(self, mn_client_v1_v2):

0 commit comments

Comments
 (0)