Skip to content

Commit daad7a9

Browse files
committed
#267 Improve timeout test with some randomness in test files
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent ccd13c3 commit daad7a9

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

tests/scancode/test_cli.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,23 @@ def test_scan_works_with_multiple_processes(monkeypatch):
355355

356356
def test_scan_works_with_multiple_processes_and_timeouts(monkeypatch):
357357
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
358-
# this contains test files with a lot of 100+ small licenses mentions that should take more tha
358+
# this contains test files with a lot of 100+ small licenses mentions that should
359+
# take more thant timeout to scan
359360
test_dir = test_env.get_test_loc('timeout', copy=True)
361+
# add some random bytes to the test files to ensure that the license results will
362+
# not be cached
363+
import time, random
364+
for tf in os.listdir(test_dir):
365+
with open(tf, 'ab') as tfh:
366+
tfh.write(str(time.time() + random.randint(0, 10 ** 6)))
367+
360368
runner = CliRunner()
361369
result_file = test_env.get_temp_file('json')
362370

363371
patched_environ = dict(
364372
# set small memory quota for test
365373
SCANCODE_TEST_MAX_MEMORY='0', # use default
366-
SCANCODE_TEST_TIMEOUT='2',
374+
SCANCODE_TEST_TIMEOUT='1',
367375
)
368376

369377
result = runner.invoke(
@@ -375,18 +383,25 @@ def test_scan_works_with_multiple_processes_and_timeouts(monkeypatch):
375383
assert result.exit_code == 0
376384
assert 'Scanning done' in result.output
377385
expected = [
378-
{u'path': u'test1.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 2 seconds.']}]},
379-
{u'path': u'test2.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 2 seconds.']}]},
380-
{u'path': u'test3.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 2 seconds.']}]}
386+
{u'path': u'test1.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 1 seconds.']}]},
387+
{u'path': u'test2.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 1 seconds.']}]},
388+
{u'path': u'test3.txt', u'scan_errors': [{u'scan': [u'Processing interrupted: timeout after 1 seconds.']}]}
381389
]
382390

383391
result_json = json.loads(open(result_file).read())
384-
assert all(scan_result in expected for scan_result in result_json['files'])
392+
assert any([scan_result in expected for scan_result in result_json['files']])
385393

386394

387395
def test_scan_works_with_multiple_processes_and_memory_quota(monkeypatch):
388396
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
389397
test_dir = test_env.get_test_loc('multiprocessing', copy=True)
398+
# add some random bytes to the test files to ensure that the license results will
399+
# not be cached
400+
import time, random
401+
for tf in os.listdir(test_dir):
402+
with open(tf, 'ab') as tfh:
403+
tfh.write(str(time.time() + random.randint(0, 10 ** 6)))
404+
390405
runner = CliRunner()
391406
result_file = test_env.get_temp_file('json')
392407

@@ -411,4 +426,4 @@ def test_scan_works_with_multiple_processes_and_memory_quota(monkeypatch):
411426
{u'path': u'patchelf.pdf', u'scan_errors': [{u'scan': [u'Processing interrupted: excessive memory usage of more than 1MB.']}]}
412427
]
413428
result_json = json.loads(open(result_file).read())
414-
assert any(scan_result in expected for scan_result in result_json['files'])
429+
assert any([scan_result in expected for scan_result in result_json['files']])

0 commit comments

Comments
 (0)