-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from AzureAD/release-0.3.0
Release 0.3.0
- Loading branch information
Showing
8 changed files
with
302 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: "Code Scanning - Action" | ||
|
||
on: | ||
push: | ||
schedule: | ||
- cron: '0 0 * * 0' | ||
|
||
jobs: | ||
CodeQL-Build: | ||
|
||
strategy: | ||
fail-fast: false | ||
|
||
|
||
# CodeQL runs on ubuntu-latest, windows-latest, and macos-latest | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
# Initializes the CodeQL tools for scanning. | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v1 | ||
# Override language selection by uncommenting this and choosing your languages | ||
# with: | ||
# languages: go, javascript, csharp, python, cpp, java | ||
|
||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java). | ||
# If this step fails, then you should remove it and run the build manually (see below). | ||
- name: Autobuild | ||
uses: github/codeql-action/autobuild@v1 | ||
|
||
# ℹ️ Command-line programs to run using the OS shell. | ||
# 📚 https://git.io/JvXDl | ||
|
||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines | ||
# and modify them (or add more) to build your code if your project | ||
# uses a compiled language | ||
|
||
#- run: | | ||
# make bootstrap | ||
# make release | ||
|
||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Usage: cache_file_generator.py cache_file_path sleep_interval | ||
This is a console application which is to be used for cross-platform lock performance testing. | ||
The app will acquire lock for the cache file, log the process id and then release the lock. | ||
It takes in two arguments - cache file path and the sleep interval. | ||
The cache file path is the path of cache file. | ||
The sleep interval is the time in seconds for which the lock is held by a process. | ||
""" | ||
|
||
import logging | ||
import os | ||
import sys | ||
import time | ||
|
||
from portalocker import exceptions | ||
|
||
from msal_extensions import FilePersistence, CrossPlatLock | ||
|
||
|
||
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) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 3: | ||
print(__doc__) | ||
sys.exit(0) | ||
_acquire_lock_and_write_to_cache(sys.argv[1], float(sys.argv[2])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import multiprocessing | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
import pytest | ||
|
||
from cache_file_generator import _acquire_lock_and_write_to_cache | ||
|
||
|
||
@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 _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" | ||
|
Oops, something went wrong.