Skip to content

Commit

Permalink
Fix reversed file operation ordering. Added test.
Browse files Browse the repository at this point in the history
  • Loading branch information
lte678 committed Dec 20, 2023
1 parent 1242498 commit 221ac86
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/MergeAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace fmerge {
friend std::ostream& operator<<(std::ostream& os, const FileOperation& fop);
};

typedef std::map<std::string, std::vector<FileOperation>, std::greater<std::string>> SortedOperationSet;
typedef std::map<std::string, std::vector<FileOperation>, std::less<std::string>> SortedOperationSet;


enum class ConflictResolution {
Expand Down
12 changes: 8 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ add_test(
NAME bidir_small_files
COMMAND python ${TEST_DIR}/run_tests.py --test-bidir-small-files
)
add_test(
NAME simplex_medium_file
COMMAND python ${TEST_DIR}/run_tests.py --test-simplex-medium-file
)
add_test(
NAME bidir_medium_files
COMMAND python ${TEST_DIR}/run_tests.py --test-bidir-medium-files
)
add_test(
NAME bidir_simple_subdirs
COMMAND python ${TEST_DIR}/run_tests.py --test-bidir-simple-subdirs
)
add_test(
NAME simplex_medium_file
COMMAND python ${TEST_DIR}/run_tests.py --test-simplex-medium-file
)
add_test(
NAME simplex_simple_subdirs
COMMAND python ${TEST_DIR}/run_tests.py --test-simplex-simple-subdirs
)
58 changes: 44 additions & 14 deletions test/helpers/file_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
from pathlib import Path


def _create_recursive_dirs(path, subdir_levels, subir_base_count, subdir_file_count, payload, suffix, only_last_leaf):
if not only_last_leaf or subdir_levels == 0:
for i in range(subdir_file_count):
# Create files
with (path / f'file_{i:04}{suffix}').open('wb') as f:
f.write(payload)
if subdir_levels != 0:
for i in range(subir_base_count):
# Create subdirs
child_path = path / f'child_dir_{i:04}'
child_path.mkdir()
_create_recursive_dirs(child_path, subdir_levels-1, subir_base_count, subdir_file_count, payload, suffix, only_last_leaf)


def bidir_conflictless(path, number_of_files, payload_size, verbose=True):
"""
Generate number_of_files divided over both hosts with no conflicts and no subdirectories.
Expand Down Expand Up @@ -62,18 +76,34 @@ def bidir_conflictless_subdirs(path, subdir_levels, subir_base_count, subdir_fil
filek
"""

def create_recursive_dirs(path, subdir_levels, subir_base_count, subdir_file_count, payload, suffix):
if not only_last_leaf or subdir_levels == 0:
for i in range(subdir_file_count):
# Create files
with (path / f'file_{i:04}{suffix}').open('wb') as f:
f.write(payload)
if subdir_levels != 0:
for i in range(subir_base_count):
# Create subdirs
child_path = path / f'child_dir_{i:04}'
child_path.mkdir()
create_recursive_dirs(child_path, subdir_levels-1, subir_base_count, subdir_file_count, payload, suffix)
peer_a = path / 'peer_a'
peer_b = path / 'peer_b'

try:
peer_a.mkdir()
peer_b.mkdir()
except FileExistsError:
if verbose:
print('Please clean the working directory before generating the dataset.')
return

if verbose:
print(f'Generating files @ {payload_size} bytes...')

payload = b'\xFF' * payload_size

# Generate the files
_create_recursive_dirs(peer_a, subdir_levels, subir_base_count, subdir_file_count, payload, 'a', only_last_leaf)
_create_recursive_dirs(peer_b, subdir_levels, subir_base_count, subdir_file_count, payload, 'b', only_last_leaf)

if verbose:
print('Done!')


def simplex_conflictless_subdirs(path, subdir_levels, subir_base_count, subdir_file_count, payload_size, verbose=True, only_last_leaf=False):
"""
Generate the same file tree as the duplex_conflictless_subdirs case, but only for one peer
"""

peer_a = path / 'peer_a'
peer_b = path / 'peer_b'
Expand All @@ -92,8 +122,7 @@ def create_recursive_dirs(path, subdir_levels, subir_base_count, subdir_file_cou
payload = b'\xFF' * payload_size

# Generate the files
create_recursive_dirs(peer_a, subdir_levels, subir_base_count, subdir_file_count, payload, 'a')
create_recursive_dirs(peer_b, subdir_levels, subir_base_count, subdir_file_count, payload, 'b')
_create_recursive_dirs(peer_a, subdir_levels, subir_base_count, subdir_file_count, payload, 'a', only_last_leaf)

if verbose:
print('Done!')
Expand All @@ -102,6 +131,7 @@ def create_recursive_dirs(path, subdir_levels, subir_base_count, subdir_file_cou
scenarios = {
'bidir_conflictless': bidir_conflictless,
'bidir_conflictless_subdirs': bidir_conflictless_subdirs,
'simplex_conflictless_subdirs': simplex_conflictless_subdirs,
}


Expand Down
22 changes: 19 additions & 3 deletions test/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import shutil
import argparse
from helpers import TEST_NG, TEST_OK, TestException
from helpers.file_gen import bidir_conflictless, bidir_conflictless_subdirs
from helpers.file_gen import bidir_conflictless, bidir_conflictless_subdirs, simplex_conflictless_subdirs
import helpers.fmerge_wrapper as fmerge_wrapper

SUPRESS_STDOUT = False
Expand Down Expand Up @@ -106,16 +106,32 @@ def test_bidir_simple_subdirs():
return (TEST_OK, '')


def test_simplex_simple_subdirs():
# Transfer a moderately large number of small files in the base directory and some depth=1 subfolders.
# Do not use conflicts.
# Can test for some race conditions in folder creation

# Create dataset
simplex_conflictless_subdirs(TEST_PATH, 3, 3, 20, 10*1024, only_last_leaf=True, verbose=False)
# Run client-server pair
try:
fmerge_wrapper.fmerge(FMERGE_BINARY, TEST_PATH, LOG_DIR / 'bidir_simple_subdirs', server_readiness_wait=3, timeout=10)
except TestException as e:
return (TEST_NG, str(e))

return (TEST_OK, '')

###############################################################################
######################## Start of Test Harness ############################
###############################################################################

system_tests = [
test_check_version,
test_bidir_small_files,
test_simplex_medium_file,
test_bidir_medium_files,
test_bidir_simple_subdirs,
test_simplex_medium_file,
test_simplex_simple_subdirs,
]


Expand Down Expand Up @@ -161,7 +177,7 @@ def test_bidir_simple_subdirs():
for test in targets:
Path('/tmp/fmerge_tests').mkdir()

print(f'Running {test.__name__}... ', end='', flush=True)
print(f'Running {test.__name__ + "...":32} ', end='', flush=True)
try:
res, msg = test()
except Exception as e:
Expand Down

0 comments on commit 221ac86

Please sign in to comment.