Skip to content

Commit 705d8a3

Browse files
authored
fix: return the same application from a repeated bootstrap() (#325)
1 parent b506453 commit 705d8a3

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎lite_bootstrap/bootstrappers/base.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class BaseBootstrapper(abc.ABC, typing.Generic[ApplicationT]):
2525
instruments: list[BaseInstrument]
2626
skipped_instruments: list[tuple[type[BaseInstrument], str]]
2727
bootstrap_config: BaseConfig
28+
_application: ApplicationT
2829

2930
# Marker tagged on a user-supplied app (or its config) once this bootstrapper has
3031
# attached its teardown to the framework's shutdown lifecycle. Prevents a second
@@ -131,11 +132,12 @@ def bootstrap(self) -> ApplicationT:
131132
)
132133
raise ConfigurationError(msg)
133134
if self.is_bootstrapped:
134-
return self._prepare_application()
135+
return self._application
135136
self.is_bootstrapped = True
136137
for one_instrument in self.instruments:
137138
one_instrument.bootstrap()
138-
return self._prepare_application()
139+
self._application = self._prepare_application()
140+
return self._application
139141

140142
def teardown(self) -> None:
141143
if not self.is_bootstrapped:

‎tests/test_litestar_bootstrap.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ def test_litestar_bootstrap(litestar_config: LitestarConfig) -> None:
118118
assert not bootstrapper.is_bootstrapped
119119

120120

121+
def test_litestar_bootstrap_twice_returns_the_same_application() -> None:
122+
bootstrapper = LitestarBootstrapper(LitestarConfig(logging_enabled=False))
123+
assert bootstrapper.bootstrap() is bootstrapper.bootstrap()
124+
125+
126+
def test_litestar_bootstrap_after_teardown_builds_a_new_application() -> None:
127+
bootstrapper = LitestarBootstrapper(
128+
LitestarConfig(logging_enabled=False, health_checks_enabled=False, prometheus_metrics_path="")
129+
)
130+
first = bootstrapper.bootstrap()
131+
bootstrapper.teardown()
132+
assert bootstrapper.bootstrap() is not first
133+
134+
121135
def test_litestar_bootstrapper_not_ready() -> None:
122136
with emulate_package_missing("litestar"), pytest.raises(RuntimeError, match="litestar is not installed"):
123137
LitestarBootstrapper(bootstrap_config=LitestarConfig())

0 commit comments

Comments
 (0)