Skip to content

Commit

Permalink
Wait for reload
Browse files Browse the repository at this point in the history
  • Loading branch information
piercefreeman committed Nov 22, 2024
1 parent dc74828 commit 801c61f
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions mountaineer/__tests__/test_hotreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def get_child_value(self):
return pkg_dir, pkg_name


def immediate_flush_to_disk(path: Path):
"""
Help deal with test unreliability when we reload a file
that has changed via python but hasn't yet been flushed to
the filesystem. reload seems to do a full filesystem pull and bipass
Python's write caching logic.
"""
# Seems to be the only consistent approach
time.sleep(1)


def test_initial_dependency_tracking(test_package_dir: tuple[Path, str]):
"""
Test initial dependency tracking on load.
Expand Down Expand Up @@ -153,10 +165,6 @@ def __init__(self):
)
)

# Import modules
importlib.import_module(f"{pkg_name}.module_b")
importlib.import_module(f"{pkg_name}.module_a")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.module_a")

Expand All @@ -171,10 +179,6 @@ def test_partial_reload_failure(test_package_dir: tuple[Path, str]):
"""
pkg_dir, pkg_name = test_package_dir

# Import base and child
importlib.import_module(f"{pkg_name}.base")
importlib.import_module(f"{pkg_name}.child")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.child")

Expand Down Expand Up @@ -233,11 +237,6 @@ def get_child_value(self):
)
)

# Import necessary modules
importlib.import_module(f"{pkg_name}.base")
importlib.import_module(f"{pkg_name}.mixin")
importlib.import_module(f"{pkg_name}.child")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.child")

Expand Down Expand Up @@ -287,9 +286,6 @@ def get_status(self):
)
)

# Import modules
importlib.import_module(f"{pkg_name}.status")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.document")

Expand Down Expand Up @@ -348,7 +344,6 @@ def get_model_value():
)

# Import modules
importlib.import_module(f"{pkg_name}.models")
main_module = importlib.import_module(f"{pkg_name}.main")

# Initialize HotReloader
Expand Down Expand Up @@ -432,7 +427,6 @@ def get_model_value():
)

# Import and verify initial state
importlib.import_module(f"{pkg_name}.models")
main_module = importlib.import_module(f"{pkg_name}.main")
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.main")

Expand Down Expand Up @@ -520,7 +514,6 @@ def get_model_value():
)

# Import and verify initial state
importlib.import_module(f"{pkg_name}.models")
main_module = importlib.import_module(f"{pkg_name}.main")
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.main")

Expand Down Expand Up @@ -594,7 +587,6 @@ class SubNestedInit:
)
)

# Import modules
importlib.import_module(f"{pkg_name}.nested")
importlib.import_module(f"{pkg_name}.nested.module")
importlib.import_module(f"{pkg_name}.nested.subnested")
Expand Down Expand Up @@ -653,11 +645,6 @@ class LeafClass(MiddleClass):
)
)

# Import modules
importlib.import_module(f"{pkg_name}.base")
importlib.import_module(f"{pkg_name}.middle")
importlib.import_module(f"{pkg_name}.leaf")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.leaf")

Expand Down Expand Up @@ -721,10 +708,6 @@ class DynamicClass(BaseClass):
)
)

# Import modules
importlib.import_module(f"{pkg_name}.base")
importlib.import_module(f"{pkg_name}.dynamic")

# Initialize HotReloader
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.dynamic")

Expand Down Expand Up @@ -760,7 +743,6 @@ def test_new_file_reload(test_package_dir: tuple[Path, str]):
pkg_dir, pkg_name = test_package_dir

# Import initial modules
importlib.import_module(f"{pkg_name}.base")
hot_reloader = HotReloader(pkg_name, pkg_dir, entrypoint=f"{pkg_name}.base")

# Create new file that imports base
Expand All @@ -776,17 +758,25 @@ def get_special_value(self):
)
)

# Verify dependency tracking
new_deps = hot_reloader.get_module_dependencies(f"{pkg_name}.new_module")
base_deps = hot_reloader.get_module_dependencies(f"{pkg_name}.base")
immediate_flush_to_disk(pkg_dir / "new_module.py")

# Calling this should also start tracking the new file
new_deps = hot_reloader.get_module_dependencies(f"{pkg_name}.new_module")
assert new_deps

# Verify we have also updated the old file bidirectionally
base_deps = hot_reloader.get_module_dependencies(f"{pkg_name}.base")
assert base_deps

assert f"{pkg_name}.base" in new_deps.imports
assert new_deps.superclasses == {"NewClass": {"BaseClass"}}
assert "NewClass" in base_deps.subclasses["BaseClass"]

# Verify that the new module was reloaded
new_module = sys.modules[f"{pkg_name}.new_module"]
obj = new_module.NewClass()
assert obj.get_special_value() == 20

# Modify the base module
(pkg_dir / "base.py").write_text(
textwrap.dedent(
Expand All @@ -800,15 +790,16 @@ def get_value(self):
)
)

time.sleep(0.1)
immediate_flush_to_disk(pkg_dir / "base.py")

success, reloaded = hot_reloader.reload_module(f"{pkg_name}.base")
assert success
assert reloaded == [f"{pkg_name}.base", f"{pkg_name}.new_module"]

# Verify that the new module was reloaded
new_module = sys.modules[f"{pkg_name}.new_module"]
obj = new_module.NewClass()
assert obj.get_special_value() == 20
assert obj.get_special_value() == 40


#
Expand Down

0 comments on commit 801c61f

Please sign in to comment.