| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
import filecmp |
| 26 |
import os |
| 27 |
import subprocess |
| 28 |
import threading |
| 29 |
import time |
| 30 |
|
| 31 |
from rsyncfns import SCRATCHDIR, make_data_file, rmtree, rsync_argv, test_fail |
| 32 |
|
| 33 |
base = SCRATCHDIR / 'growing-file' |
| 34 |
src = base / 'src' |
| 35 |
dst = base / 'dst' |
| 36 |
rmtree(base) |
| 37 |
src.mkdir(parents=True) |
| 38 |
dst.mkdir(parents=True) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
make_data_file(src / 'aaa_big', 6 * 1024 * 1024) |
| 44 |
|
| 45 |
grow = src / 'zzz_grow' |
| 46 |
make_data_file(grow, 4096) |
| 47 |
orig_size = grow.stat().st_size |
| 48 |
|
| 49 |
grow_done = threading.Event() |
| 50 |
|
| 51 |
|
| 52 |
def appender(): |
| 53 |
|
| 54 |
time.sleep(0.6) |
| 55 |
with open(grow, 'ab') as fh: |
| 56 |
fh.write(os.urandom(256 * 1024)) |
| 57 |
fh.flush() |
| 58 |
os.fsync(fh.fileno()) |
| 59 |
grow_done.set() |
| 60 |
|
| 61 |
|
| 62 |
t = threading.Thread(target=appender) |
| 63 |
t.start() |
| 64 |
|
| 65 |
proc = subprocess.run( |
| 66 |
rsync_argv('-a', '--no-inc-recursive', '--bwlimit=1500', f'{src}/', f'{dst}/'), |
| 67 |
capture_output=True, text=True) |
| 68 |
|
| 69 |
t.join() |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if not grow_done.is_set() or grow.stat().st_size <= orig_size: |
| 74 |
test_fail("test setup: source file did not grow during the transfer") |
| 75 |
|
| 76 |
if proc.returncode != 0: |
| 77 |
if 'received more data than file length' in (proc.stdout + proc.stderr): |
| 78 |
test_fail( |
| 79 |
"rsync aborted the whole transfer with RERR_PROTOCOL when a file " |
| 80 |
"grew during the run (received more data than file length); it " |
| 81 |
"should tolerate the growth like stock rsync.\n" |
| 82 |
f"exit={proc.returncode}\n{proc.stdout}{proc.stderr}") |
| 83 |
test_fail(f"rsync exited {proc.returncode}\n{proc.stdout}{proc.stderr}") |
| 84 |
|
| 85 |
if not (dst / 'zzz_grow').is_file(): |
| 86 |
test_fail("the grown file was not transferred to the destination") |
| 87 |
if not (dst / 'aaa_big').is_file(): |
| 88 |
test_fail("the earlier (throttled) file was not transferred") |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
final_src = grow.stat().st_size |
| 94 |
dst_size = (dst / 'zzz_grow').stat().st_size |
| 95 |
if dst_size <= orig_size: |
| 96 |
test_fail(f"destination only got {dst_size} bytes (<= original {orig_size}); " |
| 97 |
"the growth-during-transfer path was not exercised") |
| 98 |
if dst_size != final_src: |
| 99 |
test_fail(f"destination size {dst_size} != final source size {final_src}; " |
| 100 |
"the grown content was not fully transferred") |
| 101 |
if not filecmp.cmp(str(grow), str(dst / 'zzz_grow'), shallow=False): |
| 102 |
test_fail("destination content does not match the grown source") |
| 103 |
|
| 104 |
print("growing-file: a file appended to during the transfer no longer aborts " |
| 105 |
"the run; the grown file (and all later files) transfer intact") |