Skip to content

Commit

Permalink
Fix test_lock_function failures on windows (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jrose authored Jun 20, 2024
1 parent 79b05c4 commit 432ac3d
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions tests/unit/test_generated_udf_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@

import threading
import time
from threading import RLock
from threading import Event, RLock

import cachetools
import pytest

lock = RLock()


class InvokedFlag:
def __init__(self) -> None:
self.invoked = False


def lock_function_once(f, flag):
def wrapper(*args, **kwargs):
if not flag.invoked:
if not flag.is_set():
with lock:
if not flag.invoked:
if not flag.is_set():
result = f(*args, **kwargs)
flag.invoked = True
flag.set()
return result
return f(*args, **kwargs)
return f(*args, **kwargs)
Expand All @@ -34,18 +29,19 @@ def wrapper(*args, **kwargs):
@pytest.mark.parametrize("has_lock", [True, False])
def test_lock_function(has_lock):
load_model_called = 0
inc_lock = RLock()

@cachetools.cached({})
def load_model():
nonlocal load_model_called
time.sleep(0.5) # simulate a long operation
with lock:
time.sleep(1.0) # simulate a long operation
with inc_lock:
load_model_called += 1

def mock_udf_handler():
load_model()

locked_mock_udf_handler = lock_function_once(mock_udf_handler, InvokedFlag())
locked_mock_udf_handler = lock_function_once(mock_udf_handler, Event())

threads = []
for _ in range(10):
Expand Down

0 comments on commit 432ac3d

Please sign in to comment.