Skip to content

Commit

Permalink
cleanup test
Browse files Browse the repository at this point in the history
  • Loading branch information
arieleizenberg committed Dec 25, 2023
1 parent 70a916e commit a98b329
Showing 1 changed file with 71 additions and 74 deletions.
145 changes: 71 additions & 74 deletions tests/test_fork_multithreading.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,71 @@
"""
Test diskcache.core.Cache behaviour when process is forking.
Make sure it does not deadlock on the sqlite3 transaction lock if
forked while the lock is in use.
"""

import errno
import hashlib
import io
import os
import os.path as op
import sys
import pathlib
import pickle
import shutil
import sqlite3
import subprocess as sp
import tempfile
import threading
import time
import warnings
from threading import Thread
from unittest import mock

if sys.platform != "win32":
import signal

import pytest

import diskcache as dc

REPEATS = 1000

@pytest.fixture
def cache():
with dc.Cache() as cache:
yield cache
shutil.rmtree(cache.directory, ignore_errors=True)

def _test_thread_imp(cache):
for i in range(REPEATS * 10):
cache.set(i, i)

def _test_wait_pid(pid):
_, status = os.waitpid(pid, 0)
assert status == 0, "Child died unexpectedly"

@pytest.mark.skipif(sys.platform == "win32", reason="skips this test on Windows")
def test_fork_multithreading(cache):
thread = Thread(target=_test_thread_imp, args=(cache,))
thread.start()
try:
for i in range(REPEATS):
pid = os.fork()
if pid == 0:
cache.set(i, 0)
os._exit(0)
else:
thread = Thread(target=_test_wait_pid, args=(pid,))
thread.start()
thread.join(timeout=10)
if thread.is_alive():
os.kill(pid, signal.SIGKILL)
thread.join()
assert False, "Deadlock detected."
except OSError as e:
if e.errno != errno.EINTR:
raise

thread.join()

with dc.Cache() as cache:
test_fork_multithreading(cache)
shutil.rmtree(cache.directory, ignore_errors=True)
"""
Test diskcache.core.Cache behaviour when process is forking.
Make sure it does not deadlock on the sqlite3 transaction lock if
forked while the lock is in use.
"""

import errno
import hashlib
import io
import os
import os.path as op
import sys
import pathlib
import pickle
import shutil
import sqlite3
import subprocess as sp
import tempfile
import threading
import time
import warnings
from threading import Thread
from unittest import mock

if sys.platform != "win32":
import signal

import pytest

import diskcache as dc

REPEATS = 1000

@pytest.fixture
def cache():
with dc.Cache() as cache:
yield cache
shutil.rmtree(cache.directory, ignore_errors=True)

def _test_thread_imp(cache):
for i in range(REPEATS * 10):
cache.set(i, i)

def _test_wait_pid(pid):
_, status = os.waitpid(pid, 0)
assert status == 0, "Child died unexpectedly"

@pytest.mark.skipif(sys.platform == "win32", reason="skips this test on Windows")
def test_fork_multithreading(cache):
thread = Thread(target=_test_thread_imp, args=(cache,))
thread.start()
try:
for i in range(REPEATS):
pid = os.fork()
if pid == 0:
cache.set(i, 0)
os._exit(0)
else:
thread = Thread(target=_test_wait_pid, args=(pid,))
thread.start()
thread.join(timeout=10)
if thread.is_alive():
os.kill(pid, signal.SIGKILL)
thread.join()
assert False, "Deadlock detected."
except OSError as e:
if e.errno != errno.EINTR:
raise

thread.join()

0 comments on commit a98b329

Please sign in to comment.