File: //opt/saltstack/salt/lib/python3.10/site-packages/zc/lockfile/__pycache__/tests.cpython-310.pyc
o
;j� � @ s� d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl m
Z
d dlZdd� Z
dd� Zd d
� Zdd� ZG d
d� d�ZG dd� dej�Zdd� ZdS )� N)�Mock)�patch)�
setupstackc C s� zt j�d�} W n t jjy Y q w tdd�}t|�� �� �}t� d� |d7 }|�
d� |�d| �d �� |�
� | �
� d S )
NT�f.lock�fzr+bg{�G�z�?� r z%d
�ASCII)�zc�lockfile�LockFile� LockError�open�int�readline�strip�time�sleep�seek�write�encode�close)�lockr �v� r �E/opt/saltstack/salt/lib/python3.10/site-packages/zc/lockfile/tests.py�inc s �
r c C � dS )a�
>>> with open('f', 'w+b') as file:
... _ = file.write(b'0\n')
>>> with open('f.lock', 'w+b') as file:
... _ = file.write(b'0\n')
>>> n = 50
>>> threads = [threading.Thread(target=inc) for i in range(n)]
>>> _ = [thread.start() for thread in threads]
>>> _ = [thread.join() for thread in threads]
>>> with open('f', 'rb') as file:
... saved = int(file.read().strip())
>>> saved == n
True
>>> os.remove('f')
We should only have one pid in the lock file:
>>> f = open('f.lock')
>>> len(f.read().strip().split())
1
>>> f.close()
>>> os.remove('f.lock')
Nr r r r r �many_threads_read_and_write. � r c C r )a9
>>> import os, zc.lockfile
>>> pid = os.getpid()
>>> lock = zc.lockfile.LockFile("f.lock")
>>> f = open("f.lock")
>>> _ = f.seek(1)
>>> f.read().strip() == str(pid)
True
>>> f.close()
Make sure that locking twice does not overwrite the old pid:
>>> lock = zc.lockfile.LockFile("f.lock")
Traceback (most recent call last):
...
zc.lockfile.LockError: Couldn't lock 'f.lock'
>>> f = open("f.lock")
>>> _ = f.seek(1)
>>> f.read().strip() == str(pid)
True
>>> f.close()
>>> lock.close()
Nr r r r r �pid_in_lockfileL r r c C r )a'
hostname is correctly written into the lock file when it's included in the
lock file content template
>>> import zc.lockfile
>>> with patch('socket.gethostname', Mock(return_value='myhostname')):
... lock = zc.lockfile.LockFile(
... "f.lock", content_template='{hostname}')
>>> f = open("f.lock")
>>> _ = f.seek(1)
>>> f.read().rstrip()
'myhostname'
>>> f.close()
Make sure that locking twice does not overwrite the old hostname:
>>> lock = zc.lockfile.LockFile("f.lock", content_template='{hostname}')
Traceback (most recent call last):
...
zc.lockfile.LockError: Couldn't lock 'f.lock'
>>> f = open("f.lock")
>>> _ = f.seek(1)
>>> f.read().rstrip()
'myhostname'
>>> f.close()
>>> lock.close()
Nr r r r r �hostname_in_lockfileh r r c @ s e Zd Zdd� Zdd� ZdS )�
TestLoggerc C s
g | _ d S �N)�log_entries��selfr r r �__init__� s
zTestLogger.__init__c G s | j �|f| � d S r"