|
| 1 | +import signal,tempfile |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +import irods |
| 10 | +import irods.helpers |
| 11 | +import irods.client_configuration as config |
| 12 | +from irods.test import modules as test_modules |
| 13 | +from irods.test.modules.test_auto_close_of_data_objects__issue_456 import ( |
| 14 | + auto_close_data_objects, |
| 15 | +) |
| 16 | + |
| 17 | +OBJECT_SIZE = 2*1024**3 |
| 18 | +OBJECT_NAME = 'data_get_issue__722' |
| 19 | +LOCAL_TEMPFILE_NAME = 'data_object_for_issue_722.dat' |
| 20 | + |
| 21 | +_clock_resolution = max(.01, *(time.clock_getres(getattr(time,symbol)) |
| 22 | + for symbol in dir(time) if symbol.startswith('CLOCK_'))) |
| 23 | +def wait_till_true( function ): |
| 24 | + while not (truth_value := function()): |
| 25 | + time.sleep(_clock_resolution) |
| 26 | + return truth_value |
| 27 | + |
| 28 | + |
| 29 | +def test( |
| 30 | + # TODO - accept unittest testcase 'test_instance' param , replace assert's with test_instance.assert |
| 31 | + ): |
| 32 | + """Creates a child process executing a long get() and ensures the process can be |
| 33 | + terminated using SIGINT or SIGTERM. |
| 34 | + """ |
| 35 | + program = os.path.join(test_modules.__path__[0], os.path.basename(__file__)) |
| 36 | + |
| 37 | + # Call into this same module as a command. This will initiate another Python process that |
| 38 | + # performs a lengthy data object "get" operation (see the main body of the script, below.) |
| 39 | + process = subprocess.Popen([sys.executable, program], |
| 40 | + stderr=subprocess.PIPE, |
| 41 | + stdout=subprocess.PIPE, text = True) |
| 42 | + |
| 43 | + localfile = process.stdout.readline().strip() |
| 44 | + assert wait_till_true(lambda:os.path.exists(localfile) and os.stat(localfile).st_size > OBJECT_SIZE//2) |
| 45 | + |
| 46 | + sig = signal.SIGINT |
| 47 | + process.send_signal(sig) |
| 48 | + class TestFailed(Exception):pass |
| 49 | + try: |
| 50 | + assert process.wait(timeout = 15) == -sig |
| 51 | + assert re.search('KeyboardInterrupt',process.stderr.read()) |
| 52 | + except subprocess.TimeoutExpired as timeout_exc: |
| 53 | + raise TestFailed("Non-daemon thread(s) probably prevented subprocess's main thread from exiting") from timeout_exc |
| 54 | + return True |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + session = irods.helpers.make_session() |
| 59 | + hc = irods.helpers.home_collection(session) |
| 60 | + TESTFILE_FILL = b'_'*(1024*1024) |
| 61 | + object_path = f'{hc}/{OBJECT_NAME}' |
| 62 | + with session.data_objects.open(object_path,'w') as f: |
| 63 | + for y in range(OBJECT_SIZE//len(TESTFILE_FILL)): |
| 64 | + f.write(TESTFILE_FILL) |
| 65 | + local_path = None |
| 66 | + try: |
| 67 | + with tempfile.NamedTemporaryFile(prefix='local_file_issue_722.dat', delete = True) as t: |
| 68 | + local_path = t.name |
| 69 | + |
| 70 | + # Tell the parent process the name of the local file being "get"ted (got) from iRODS |
| 71 | + print(local_path) |
| 72 | + sys.stdout.flush() |
| 73 | + |
| 74 | + session.data_objects.get(object_path, local_path) |
| 75 | + finally: |
| 76 | + if local_path is not None and os.path.exists(local_path): |
| 77 | + os.unlink(local_path) |
| 78 | + if session.data_objects.exists(object_path): |
| 79 | + session.data_objects.unlink(object_path, force=True) |
0 commit comments