|
1 | 1 | # SPDX-License-Identifier: Apache-2.0
|
2 | 2 | # Copyright 2022 Atlan Pte. Ltd.
|
| 3 | +import threading |
| 4 | +import time |
3 | 5 | from importlib.resources import read_text
|
4 | 6 | from json import load, loads
|
5 | 7 | from pathlib import Path
|
@@ -2092,60 +2094,60 @@ def test_atlan_call_api_server_error_messages_with_causes(
|
2092 | 2094 | client.asset.save(glossary)
|
2093 | 2095 |
|
2094 | 2096 |
|
2095 |
| -# @pytest.mark.parametrize("thread_count", [3]) # Run with three threads |
2096 |
| -# def test_atlan_client_tls(thread_count): |
2097 |
| -# """Tests that AtlanClient instances remain isolated across multiple threads.""" |
2098 |
| -# validation_results = {} |
2099 |
| -# results_lock = threading.Lock() |
2100 |
| - |
2101 |
| -# def _test_atlan_client_isolation(name, api_key1, api_key2, api_key3): |
2102 |
| -# """Creates three AtlanClient instances within the same thread and verifies isolation.""" |
2103 |
| -# # Instantiate three separate AtlanClient instances |
2104 |
| -# client1 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key1) |
2105 |
| -# time.sleep(0.2) |
2106 |
| -# observed1 = client1.get_current_client().api_key # Should match api_key1 |
2107 |
| - |
2108 |
| -# client2 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key2) |
2109 |
| -# time.sleep(0.2) |
2110 |
| -# observed2 = client2.get_current_client().api_key # Should match api_key2 |
2111 |
| - |
2112 |
| -# client3 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key3) |
2113 |
| -# time.sleep(0.2) |
2114 |
| -# observed3 = client3.get_current_client().api_key # Should match api_key3 |
2115 |
| - |
2116 |
| -# # Store results in a thread-safe way |
2117 |
| -# with results_lock: |
2118 |
| -# validation_results[name] = (observed1, observed2, observed3) |
2119 |
| - |
2120 |
| -# # Define unique API keys for each thread |
2121 |
| -# api_keys = [ |
2122 |
| -# ("API_KEY_1A", "API_KEY_1B", "API_KEY_1C"), |
2123 |
| -# ("API_KEY_2A", "API_KEY_2B", "API_KEY_2C"), |
2124 |
| -# ("API_KEY_3A", "API_KEY_3B", "API_KEY_3C"), |
2125 |
| -# ] |
2126 |
| - |
2127 |
| -# threads = [] |
2128 |
| -# for i in range(thread_count): |
2129 |
| -# thread = threading.Thread( |
2130 |
| -# target=_test_atlan_client_isolation, |
2131 |
| -# args=(f"thread{i + 1}", *api_keys[i]), |
2132 |
| -# ) |
2133 |
| -# threads.append(thread) |
2134 |
| -# thread.start() |
2135 |
| - |
2136 |
| -# # Wait for all threads to finish |
2137 |
| -# for thread in threads: |
2138 |
| -# thread.join() |
2139 |
| - |
2140 |
| -# # Validate that each thread's clients retained their assigned API keys |
2141 |
| -# for i in range(thread_count): |
2142 |
| -# thread_name = f"thread{i + 1}" |
2143 |
| -# expected_keys = api_keys[i] |
2144 |
| - |
2145 |
| -# assert validation_results[thread_name] == expected_keys, ( |
2146 |
| -# f"Clients were overwritten across threads! " |
2147 |
| -# f"{thread_name} saw {validation_results[thread_name]} instead of {expected_keys}" |
2148 |
| -# ) |
| 2097 | +@pytest.mark.parametrize("thread_count", [3]) # Run with three threads |
| 2098 | +def test_atlan_client_tls(thread_count): |
| 2099 | + """Tests that AtlanClient instances remain isolated across multiple threads.""" |
| 2100 | + validation_results = {} |
| 2101 | + results_lock = threading.Lock() |
| 2102 | + |
| 2103 | + def _test_atlan_client_isolation(name, api_key1, api_key2, api_key3): |
| 2104 | + """Creates three AtlanClient instances within the same thread and verifies isolation.""" |
| 2105 | + # Instantiate three separate AtlanClient instances |
| 2106 | + client1 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key1) |
| 2107 | + time.sleep(0.2) |
| 2108 | + observed1 = client1.get_current_client().api_key # Should match api_key1 |
| 2109 | + |
| 2110 | + client2 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key2) |
| 2111 | + time.sleep(0.2) |
| 2112 | + observed2 = client2.get_current_client().api_key # Should match api_key2 |
| 2113 | + |
| 2114 | + client3 = AtlanClient(base_url="https://test.atlan.com", api_key=api_key3) |
| 2115 | + time.sleep(0.2) |
| 2116 | + observed3 = client3.get_current_client().api_key # Should match api_key3 |
| 2117 | + |
| 2118 | + # Store results in a thread-safe way |
| 2119 | + with results_lock: |
| 2120 | + validation_results[name] = (observed1, observed2, observed3) |
| 2121 | + |
| 2122 | + # Define unique API keys for each thread |
| 2123 | + api_keys = [ |
| 2124 | + ("API_KEY_1A", "API_KEY_1B", "API_KEY_1C"), |
| 2125 | + ("API_KEY_2A", "API_KEY_2B", "API_KEY_2C"), |
| 2126 | + ("API_KEY_3A", "API_KEY_3B", "API_KEY_3C"), |
| 2127 | + ] |
| 2128 | + |
| 2129 | + threads = [] |
| 2130 | + for i in range(thread_count): |
| 2131 | + thread = threading.Thread( |
| 2132 | + target=_test_atlan_client_isolation, |
| 2133 | + args=(f"thread{i + 1}", *api_keys[i]), |
| 2134 | + ) |
| 2135 | + threads.append(thread) |
| 2136 | + thread.start() |
| 2137 | + |
| 2138 | + # Wait for all threads to finish |
| 2139 | + for thread in threads: |
| 2140 | + thread.join() |
| 2141 | + |
| 2142 | + # Validate that each thread's clients retained their assigned API keys |
| 2143 | + for i in range(thread_count): |
| 2144 | + thread_name = f"thread{i + 1}" |
| 2145 | + expected_keys = api_keys[i] |
| 2146 | + |
| 2147 | + assert validation_results[thread_name] == expected_keys, ( |
| 2148 | + f"Clients were overwritten across threads! " |
| 2149 | + f"{thread_name} saw {validation_results[thread_name]} instead of {expected_keys}" |
| 2150 | + ) |
2149 | 2151 |
|
2150 | 2152 |
|
2151 | 2153 | class TestBatch:
|
|
0 commit comments