gunicorn/tests/test_sock.py
benoitc 8a6748ee65 fix unix socket locking
This change add proper file locking to gunicorn. By default "gunicorn.lock" is created in the temporary directory when a unix socket is bound.  In case someone want to fix the lock file path or use multiple gunicorn instance the "--lock-file" setting can be used to set the path of this file.

fix #1259
2016-05-14 22:03:40 +02:00

37 lines
1.0 KiB
Python

try:
import unittest.mock as mock
except ImportError:
import mock
from gunicorn import sock
@mock.patch('os.getpid')
@mock.patch('os.unlink')
@mock.patch('socket.fromfd')
def test_unix_socket_close_delete_if_exlock(fromfd, unlink, getpid):
gsock = sock.UnixSocket('test.sock', mock.Mock(), mock.Mock(), mock.Mock())
gsock.close(False)
unlink.assert_called_with('test.sock')
@mock.patch('os.getpid')
@mock.patch('os.unlink')
@mock.patch('socket.fromfd')
def test_unix_socket_close_keep_if_no_exlock(fromfd, unlink, getpid):
gsock = sock.UnixSocket('test.sock', mock.Mock(), mock.Mock(), mock.Mock())
gsock.close(True)
unlink.assert_not_called()
@mock.patch('os.getpid')
@mock.patch('os.unlink')
@mock.patch('socket.fromfd')
def test_unix_socket_not_deleted_by_worker(fromfd, unlink, getpid):
fd = mock.Mock()
gsock = sock.UnixSocket('test.sock', mock.Mock(), mock.Mock(), fd)
getpid.reset_mock()
getpid.return_value = "fake" # fake a pid change
gsock.close(False)
unlink.assert_not_called()