Skip to content

Commit 84d527b

Browse files
cathleenyIsaac
andcommitted
test(kernel): make header_provider serialization test deterministic
Replace the sleep-based overlap test with the event-based version from review: the first add_headers call blocks on a release event, so the second call provably cannot enter while the first is in flight — the test now fails deterministically without the serialization lock instead of only almost-always. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com>
1 parent 1b20932 commit 84d527b

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

‎tests/unit/test_kernel_auth_bridge.py‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -971,29 +971,46 @@ def factory():
971971
header_provider()
972972

973973
def test_header_provider_calls_are_serialized(self):
974+
# Event-driven (per review): nothing here depends on thread
975+
# scheduling. The first call blocks inside add_headers until
976+
# released, so the second call provably cannot enter add_headers
977+
# while the first is still in it — the test fails deterministically
978+
# if the serialization lock is removed.
974979
import threading
975-
import time
976980

977-
state = {"active": 0, "max": 0}
981+
first_entered = threading.Event()
982+
release = threading.Event()
983+
second_entered = threading.Event()
984+
calls = []
978985

979-
class _SlowProvider(AuthProvider):
986+
class _BlockingProvider(AuthProvider):
980987
def add_headers(self, request_headers):
981-
state["active"] += 1
982-
state["max"] = max(state["max"], state["active"])
983-
time.sleep(0.01)
984-
state["active"] -= 1
988+
if not calls:
989+
calls.append(1)
990+
first_entered.set()
991+
release.wait(5)
992+
else:
993+
second_entered.set()
985994
request_headers["Authorization"] = "Bearer prov#1"
986995

987996
header_provider = kernel_auth_kwargs(
988-
_SlowProvider(), {"credentials_provider": object()}
997+
_BlockingProvider(), {"credentials_provider": object()}
989998
)["header_provider"]
990-
threads = [threading.Thread(target=header_provider) for _ in range(4)]
991-
for t in threads:
992-
t.start()
993-
for t in threads:
994-
t.join()
995999

996-
assert state["max"] == 1
1000+
t1 = threading.Thread(target=header_provider)
1001+
t1.start()
1002+
assert first_entered.wait(5)
1003+
t2 = threading.Thread(target=header_provider)
1004+
t2.start()
1005+
# While the first call is blocked inside add_headers, the second
1006+
# call must still be held by the lock.
1007+
assert not second_entered.wait(0.3)
1008+
release.set()
1009+
t1.join(5)
1010+
t2.join(5)
1011+
assert not t1.is_alive()
1012+
assert not t2.is_alive()
1013+
assert second_entered.is_set()
9971014

9981015
def test_provider_without_built_auth_provider_raises(self):
9991016
with pytest.raises(ProgrammingError, match="credentials_provider"):

0 commit comments

Comments
 (0)