Skip to content
Merged
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f6d52b0
Merge pull request #29 from AzureAD/release-0.1.1
rayluo Jul 17, 2019
20c6b35
Merge pull request #34 from AzureAD/release-0.1.2
rayluo Oct 19, 2019
9b44cc7
Merge pull request #38 from AzureAD/release-0.1.3
rayluo Nov 2, 2019
4efc521
Merge pull request #39 from AzureAD/release-0.1.3
rayluo Nov 2, 2019
32914b3
Merge pull request #45 from AzureAD/release-0.2.0
rayluo Apr 15, 2020
fae4931
Merge pull request #48 from AzureAD/release-0.2.1
rayluo Apr 23, 2020
3df9da0
Merge pull request #54 from AzureAD/release-0.2.2
abhidnya13 Apr 27, 2020
f99585c
Initial commit
abhidnya13 May 11, 2020
c3aef9e
Merge branch 'master' of https://github.com/AzureAD/microsoft-authent…
abhidnya13 May 14, 2020
e835383
Refactoring
abhidnya13 May 14, 2020
129b3de
Removing cache_file after finishing
abhidnya13 May 14, 2020
6f5014c
Splitting into two cases
abhidnya13 May 15, 2020
99c03a6
Reducing no_of_processes to avoid lock exception
abhidnya13 May 15, 2020
e9994f4
reducing processes
abhidnya13 May 15, 2020
784ab6a
Experimenting with no_of processes
abhidnya13 May 18, 2020
bb91f1c
testing limit of no of processes
abhidnya13 May 18, 2020
93a5551
Reducing no of processes
abhidnya13 May 19, 2020
1632f00
Modifying logic to catch exception and re-raise it
abhidnya13 May 19, 2020
0a418b7
Experimenting
abhidnya13 May 19, 2020
da5574f
Experimenting 1
abhidnya13 May 19, 2020
3ba7b2c
Experimenting 2
abhidnya13 May 19, 2020
795975e
Fixing typo in env variable
abhidnya13 May 19, 2020
e19a0b1
Experimenting 3
abhidnya13 May 22, 2020
0fd8862
Experimenting 4
abhidnya13 May 22, 2020
7571d4d
Experimenting 5
abhidnya13 May 22, 2020
789b5f1
Adding normal workload case and refactor
abhidnya13 May 23, 2020
b015921
Arranging order of the tests
abhidnya13 May 23, 2020
1127b31
refactor
abhidnya13 May 23, 2020
436bbf7
Cache file console app (#60)
abhidnya13 Jun 5, 2020
cf96ef6
Adding a description for cache file generator
abhidnya13 Jun 5, 2020
e7fd866
Changing the format of doc_string
abhidnya13 Jun 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions tests/test_cache_lock_file_perf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import logging
import multiprocessing
import os
import shutil
import tempfile
import time

from portalocker import exceptions
import pytest

from msal_extensions import FilePersistence, CrossPlatLock


@pytest.fixture
def temp_location():
test_folder = tempfile.mkdtemp(prefix="test_persistence_roundtrip")
yield os.path.join(test_folder, 'persistence.bin')
shutil.rmtree(test_folder, ignore_errors=True)


def _validate_result_in_cache(cache_location):
with open(cache_location) as handle:
data = handle.read()
prev_process_id = None
count = 0
for line in data.split("\n"):
if line:
count += 1
tag, process_id = line.split(" ")
if prev_process_id is not None:
assert process_id == prev_process_id, "Process overlap found"
assert tag == '>', "Process overlap_found"
prev_process_id = None
else:
assert tag == '<', "Opening bracket not found"
prev_process_id = process_id
return count


def _acquire_lock_and_write_to_cache(cache_location, sleep_interval):
cache_accessor = FilePersistence(cache_location)
lock_file_path = cache_accessor.get_location() + ".lockfile"
try:
with CrossPlatLock(lock_file_path):
data = cache_accessor.load()
if data is None:
data = ""
data += "< " + str(os.getpid()) + "\n"
time.sleep(sleep_interval)
data += "> " + str(os.getpid()) + "\n"
cache_accessor.save(data)
except exceptions.LockException as e:
logging.warning("Unable to acquire lock %s", e)


def _run_multiple_processes(no_of_processes, cache_location, sleep_interval):
open(cache_location, "w+")
processes = []
for i in range(no_of_processes):
process = multiprocessing.Process(
target=_acquire_lock_and_write_to_cache,
args=(cache_location, sleep_interval))
processes.append(process)

for process in processes:
process.start()

for process in processes:
process.join()


def test_lock_for_normal_workload(temp_location):
num_of_processes = 4
sleep_interval = 0.1
_run_multiple_processes(num_of_processes, temp_location, sleep_interval)
count = _validate_result_in_cache(temp_location)
assert count == num_of_processes * 2, "Should not observe starvation"


def test_lock_for_high_workload(temp_location):
num_of_processes = 80
sleep_interval = 0
_run_multiple_processes(num_of_processes, temp_location, sleep_interval)
count = _validate_result_in_cache(temp_location)
assert count <= num_of_processes * 2, "Starvation or not, we should not observe garbled payload"


def test_lock_for_timeout(temp_location):
num_of_processes = 10
sleep_interval = 1
_run_multiple_processes(num_of_processes, temp_location, sleep_interval)
count = _validate_result_in_cache(temp_location)
assert count < num_of_processes * 2, "Should observe starvation"