trouble with testing advisory_lock with pytest

Viewed 42

Im currently trying to test a function that will lock some string and will not allow to do anything under that locked string, using advisory_lock.

so here is my code:

import pytest

from django_pglocks import advisory_lock
from threading import Thread
from time import sleep

def function_that_should_lock():
    with advisory_lock('secret_string', wait=True):
        sleep(10)


@eager_db_test
def test_lock():
    failed = False

    function_that_should_lock_thread = Thread(
        target=function_that_should_lock,
    )

    function_that_should_lock_thread.start()

    with advisory_lock('secret_string', wait=False):
        failed = True

    assert not failed

For some reason function_that_should_lock actually doesn't lock the string, and failed will be set to True.

Please help me understand how it works =)

0 Answers
Related