Skip to content

Commit 49b8df5

Browse files
committed
fix: unrandomize test
1 parent a7163ea commit 49b8df5

1 file changed

Lines changed: 18 additions & 46 deletions

File tree

tests/test_unit_tests_threaded.py

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,29 +2096,17 @@ def add_ingredient(ingredient_json, thread_id):
20962096
builder.close()
20972097

20982098
def test_builder_sign_with_multiple_ingredient_random_many_threads(self):
2099-
"""Test Builder class operations with 10 threads, each adding 3 random ingredients and signing a random file."""
2099+
"""Test Builder class operations with 12 threads, each adding 3 specific ingredients and signing a file."""
21002100
# Number of threads to use in the test
2101-
# We are pushing it here, as we want to test with thread count one to two orders of magnitude
2102-
# higher than "usual" max numbers of cores on (server) machines may be.
2103-
21042101
TOTAL_THREADS_USED = 12
21052102

2106-
# Get list of files from files-for-reading-tests directory
2107-
reading_dir = os.path.join(self.data_dir, "files-for-reading-tests")
2108-
2109-
# Filter for JPG and PNG files only
2110-
all_files = [
2111-
f for f in os.listdir(reading_dir)
2112-
if os.path.isfile(os.path.join(reading_dir, f))
2113-
and os.path.splitext(f)[1].lower() in {'.jpg', '.jpeg', '.png'}
2103+
# Define the specific files to use as ingredients
2104+
ingredient_files = [
2105+
os.path.join(self.data_dir, "A_thumbnail.jpg"),
2106+
os.path.join(self.data_dir, "C.jpg"),
2107+
os.path.join(self.data_dir, "cloud.jpg")
21142108
]
21152109

2116-
# Ensure we have enough files
2117-
self.assertGreaterEqual(
2118-
len(all_files),
2119-
3,
2120-
"Need at least 3 JPG/PNG files for testing")
2121-
21222110
# Thread synchronization
21232111
thread_results = {}
21242112
completed_threads = 0
@@ -2129,24 +2117,17 @@ def thread_work(thread_id):
21292117
# Create a new builder for this thread
21302118
builder = Builder.from_json(self.manifestDefinition)
21312119

2132-
# Select 3 random files for ingredients
2133-
# Use thread_id as seed for reproducibility
2134-
random.seed(thread_id)
2135-
ingredient_files = random.sample(all_files, 3)
2136-
21372120
# Add each ingredient
2138-
for i, file_name in enumerate(ingredient_files, 1):
2139-
file_path = os.path.join(reading_dir, file_name)
2121+
for i, file_path in enumerate(ingredient_files, 1):
21402122
ingredient_json = json.dumps({
2141-
"title": f"Thread {thread_id} Ingredient {i} - {file_name}"
2123+
"title": f"Thread {thread_id} Ingredient {i} - {os.path.basename(file_path)}"
21422124
})
21432125

21442126
with open(file_path, 'rb') as f:
21452127
builder.add_ingredient(ingredient_json, "image/jpeg", f)
21462128

2147-
# Select a random file for signing
2148-
sign_file = random.choice(all_files)
2149-
sign_file_path = os.path.join(reading_dir, sign_file)
2129+
# Use A.jpg as the file to sign
2130+
sign_file_path = os.path.join(self.data_dir, "A.jpg")
21502131

21512132
# Sign the file
21522133
with open(sign_file_path, "rb") as file:
@@ -2170,8 +2151,8 @@ def thread_work(thread_id):
21702151
# Store results for verification
21712152
thread_results[thread_id] = {
21722153
'manifest': manifest_data,
2173-
'ingredient_files': ingredient_files,
2174-
'sign_file': sign_file
2154+
'ingredient_files': [os.path.basename(f) for f in ingredient_files],
2155+
'sign_file': os.path.basename(sign_file_path)
21752156
}
21762157

21772158
# Clean up streams
@@ -2199,8 +2180,7 @@ def thread_work(thread_id):
21992180
thread.join()
22002181

22012182
# Verify all threads completed
2202-
self.assertEqual(completed_threads, TOTAL_THREADS_USED, f"All {
2203-
TOTAL_THREADS_USED} threads should have completed")
2183+
self.assertEqual(completed_threads, TOTAL_THREADS_USED, f"All {TOTAL_THREADS_USED} threads should have completed")
22042184
self.assertEqual(
22052185
len(thread_results),
22062186
TOTAL_THREADS_USED,
@@ -2212,13 +2192,10 @@ def thread_work(thread_id):
22122192

22132193
# Check if thread encountered an error
22142194
if 'error' in result:
2215-
self.fail(
2216-
f"Thread {thread_id} failed with error: {
2217-
result['error']}")
2195+
self.fail(f"Thread {thread_id} failed with error: {result['error']}")
22182196

22192197
manifest_data = result['manifest']
22202198
ingredient_files = result['ingredient_files']
2221-
sign_file = result['sign_file']
22222199

22232200
# Verify active manifest exists
22242201
self.assertIn("active_manifest", manifest_data)
@@ -2234,16 +2211,11 @@ def thread_work(thread_id):
22342211
self.assertIsInstance(active_manifest["ingredients"], list)
22352212
self.assertEqual(len(active_manifest["ingredients"]), 3)
22362213

2237-
# Verify all ingredients exist with correct thread ID and file
2238-
# names
2239-
ingredient_titles = [ing["title"]
2240-
for ing in active_manifest["ingredients"]]
2214+
# Verify all ingredients exist with correct thread ID and file names
2215+
ingredient_titles = [ing["title"] for ing in active_manifest["ingredients"]]
22412216
for i, file_name in enumerate(ingredient_files, 1):
2242-
expected_title = f"Thread {
2243-
thread_id} Ingredient {i} - {file_name}"
2244-
self.assertIn(expected_title, ingredient_titles, f"Thread {
2245-
thread_id} should have ingredient with title {expected_title}")
2246-
2217+
expected_title = f"Thread {thread_id} Ingredient {i} - {file_name}"
2218+
self.assertIn(expected_title, ingredient_titles, f"Thread {thread_id} should have ingredient with title {expected_title}")
22472219

22482220
if __name__ == '__main__':
22492221
unittest.main()

0 commit comments

Comments
 (0)