File: //opt/saltstack/salt/lib/python3.10/site-packages/zc/lockfile/__pycache__/tests.cpython-310.pyc
o
�N�g� � @ s� d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl m
Z
mZ d dlZz
d dl
mZmZ W n eyG d dlmZmZ Y nw e
�e�d�dfg�Zdd� Zdd � Zd
d� Zdd
� ZG dd� de�ZG dd� dej�Zdd� ZdS )� N)�
renormalizing�
setupstack)�Mock�patchzzc\.lockfile\.LockError:z
LockError:c C s� zt j�d�} W n t jjy Y q w tdd�}t|�� �� �}t� d� |d7 }|�
d� |�d| �d�� |�
� | �
� d S ) N� �f.lock�fzr+bg{�G�z�?r z%d
�ASCII)�zc�lockfile�LockFileZ 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 )a-
>>> 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):
...
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_lockfileK 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):
...
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_lockfileg 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"