|
| 1 | +import time |
| 2 | +from time import sleep |
| 3 | +from typing import Callable |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from datetime import datetime, timedelta |
| 8 | + |
| 9 | +from databricks.sdk.oauth import Refreshable, Token |
| 10 | + |
| 11 | + |
| 12 | +class _MockRefreshable(Refreshable): |
| 13 | + |
| 14 | + def __init__(self, disable_async, token=None, stale_duration=timedelta(seconds=60), refresh_effect: Callable[[], Token]=None): |
| 15 | + super().__init__(token, disable_async, stale_duration) |
| 16 | + self._refresh_effect = refresh_effect |
| 17 | + self._refresh_count = 0 |
| 18 | + |
| 19 | + def refresh(self) -> Token: |
| 20 | + if self._refresh_effect: |
| 21 | + self._token = self._refresh_effect() |
| 22 | + self._refresh_count += 1 |
| 23 | + return self._token |
| 24 | + |
| 25 | +def fail() -> Token: |
| 26 | + raise Exception("Failed to refresh token") |
| 27 | + |
| 28 | +def static_token(token: Token, wait: int=0) -> Callable[[], Token]: |
| 29 | + def f() -> Token: |
| 30 | + time.sleep(wait) |
| 31 | + return token |
| 32 | + return f |
| 33 | + |
| 34 | +def blocking_refresh(token: Token) -> (Callable[[], Token], Callable[[],None]): |
| 35 | + """ |
| 36 | + Create a refresh function that blocks until unblock is called. |
| 37 | +
|
| 38 | + Param: |
| 39 | + token: the token that will be returned |
| 40 | +
|
| 41 | + Returns: |
| 42 | + A tuple containing the refresh function and the unblock function. |
| 43 | +
|
| 44 | + """ |
| 45 | + blocking = True |
| 46 | + def refresh(): |
| 47 | + while blocking: |
| 48 | + sleep(0.1) |
| 49 | + return token |
| 50 | + def unblock(): |
| 51 | + nonlocal blocking |
| 52 | + blocking = False |
| 53 | + return refresh, unblock |
| 54 | + |
| 55 | + |
| 56 | +def test_disable_async_stale_does_not_refresh(): |
| 57 | + stale_token = Token( |
| 58 | + access_token="access_token", |
| 59 | + expiry=datetime.now() + timedelta(seconds=50), |
| 60 | + ) |
| 61 | + r = _MockRefreshable(token=stale_token, disable_async=True, refresh_effect=fail) |
| 62 | + result = r.token() |
| 63 | + assert r._refresh_count == 0 |
| 64 | + assert result == stale_token |
| 65 | + |
| 66 | +def test_disable_async_no_token_does_refresh(): |
| 67 | + token = Token( |
| 68 | + access_token="access_token", |
| 69 | + expiry=datetime.now() + timedelta(seconds=50), |
| 70 | + ) |
| 71 | + r = _MockRefreshable(token=None, disable_async=True, refresh_effect=static_token(token)) |
| 72 | + result = r.token() |
| 73 | + assert r._refresh_count == 1 |
| 74 | + assert result == token |
| 75 | + |
| 76 | +def test_disable_async_no_expiration_does_not_refresh(): |
| 77 | + non_expiring_token = Token( |
| 78 | + access_token="access_token", |
| 79 | + ) |
| 80 | + r = _MockRefreshable(token=non_expiring_token, disable_async=True, refresh_effect=fail) |
| 81 | + result = r.token() |
| 82 | + assert r._refresh_count == 0 |
| 83 | + assert result == non_expiring_token |
| 84 | + |
| 85 | +def test_disable_async_fresh_does_not_refresh(): |
| 86 | + # Create a token that is already stale. If async is disabled, the token should not be refreshed. |
| 87 | + token = Token( |
| 88 | + access_token="access_token", |
| 89 | + expiry=datetime.now() + timedelta(seconds=300), |
| 90 | + ) |
| 91 | + r = _MockRefreshable(token=token, disable_async=True, refresh_effect=fail) |
| 92 | + result = r.token() |
| 93 | + assert r._refresh_count == 0 |
| 94 | + assert result == token |
| 95 | + |
| 96 | +def test_disable_async_expired_does_refresh(): |
| 97 | + expired_token = Token( |
| 98 | + access_token="access_token", |
| 99 | + expiry=datetime.now() - timedelta(seconds=300), |
| 100 | + ) |
| 101 | + new_token = Token( |
| 102 | + access_token="access_token", |
| 103 | + expiry=datetime.now() + timedelta(seconds=300), |
| 104 | + ) |
| 105 | + # Add one second to the refresh time to ensure that the call is blocking. |
| 106 | + # If the call is not blocking, the wait time will ensure that the |
| 107 | + # old token is returned. |
| 108 | + r = _MockRefreshable(token=expired_token, disable_async=True, refresh_effect=static_token(new_token, wait=1)) |
| 109 | + result = r.token() |
| 110 | + assert r._refresh_count == 1 |
| 111 | + assert result == new_token |
| 112 | + |
| 113 | +def test_expired_does_refresh(): |
| 114 | + expired_token = Token( |
| 115 | + access_token="access_token", |
| 116 | + expiry=datetime.now() - timedelta(seconds=300), |
| 117 | + ) |
| 118 | + new_token = Token( |
| 119 | + access_token="access_token", |
| 120 | + expiry=datetime.now() + timedelta(seconds=300), |
| 121 | + ) |
| 122 | + # Add one second to the refresh time to ensure that the call is blocking. |
| 123 | + # If the call is not blocking, the wait time will ensure that the |
| 124 | + # old token is returned. |
| 125 | + r = _MockRefreshable(token=expired_token, disable_async=False, refresh_effect=static_token(new_token, wait=1)) |
| 126 | + result = r.token() |
| 127 | + assert r._refresh_count == 1 |
| 128 | + assert result == new_token |
| 129 | + |
| 130 | +def test_stale_does_refresh_async(): |
| 131 | + stale_token = Token( |
| 132 | + access_token="access_token", |
| 133 | + expiry=datetime.now() + timedelta(seconds=50), |
| 134 | + ) |
| 135 | + new_token = Token( |
| 136 | + access_token="access_token", |
| 137 | + expiry=datetime.now() + timedelta(seconds=300), |
| 138 | + ) |
| 139 | + # Add one second to the refresh to avoid race conditions. |
| 140 | + # Without it, the new token may be returned in some cases. |
| 141 | + refresh, unblock = blocking_refresh(new_token) |
| 142 | + r = _MockRefreshable(token=stale_token, disable_async=False, refresh_effect=refresh) |
| 143 | + result = r.token() |
| 144 | + # NOTE: Do not check for refresh count here, since the |
| 145 | + assert result == stale_token |
| 146 | + assert r._refresh_count == 0 |
| 147 | + # Unblock the refresh and wait |
| 148 | + unblock() |
| 149 | + time.sleep(2) |
| 150 | + # Call again and check that you get the new token |
| 151 | + result = r.token() |
| 152 | + assert result == new_token |
| 153 | + # Ensure that all calls have completed |
| 154 | + time.sleep(0.1) |
| 155 | + assert r._refresh_count == 1 |
| 156 | + |
| 157 | + |
| 158 | +def test_no_token_does_refresh(): |
| 159 | + new_token = Token( |
| 160 | + access_token="access_token", |
| 161 | + expiry=datetime.now() + timedelta(seconds=300), |
| 162 | + ) |
| 163 | + # Add one second to the refresh time to ensure that the call is blocking. |
| 164 | + # If the call is not blocking, the wait time will ensure that the |
| 165 | + # token is not returned. |
| 166 | + r = _MockRefreshable(token=None, disable_async=False, refresh_effect=static_token(new_token, wait=1)) |
| 167 | + result = r.token() |
| 168 | + assert r._refresh_count == 1 |
| 169 | + assert result == new_token |
| 170 | + |
| 171 | +def test_fresh_does_not_refresh(): |
| 172 | + fresh_token = Token( |
| 173 | + access_token="access_token", |
| 174 | + expiry=datetime.now() + timedelta(seconds=300), |
| 175 | + ) |
| 176 | + r = _MockRefreshable(token=fresh_token, disable_async=False, refresh_effect=fail) |
| 177 | + result = r.token() |
| 178 | + assert r._refresh_count == 0 |
| 179 | + assert result == fresh_token |
| 180 | + |
| 181 | +def test_multiple_calls_dont_start_many_threads(): |
| 182 | + stale_token = Token( |
| 183 | + access_token="access_token", |
| 184 | + expiry=datetime.now() + timedelta(seconds=59), |
| 185 | + ) |
| 186 | + new_token = Token( |
| 187 | + access_token="access_token", |
| 188 | + expiry=datetime.now() + timedelta(seconds=300), |
| 189 | + ) |
| 190 | + refresh, unblock = blocking_refresh(new_token) |
| 191 | + r = _MockRefreshable(token=stale_token, disable_async=False, refresh_effect=refresh) |
| 192 | + # Call twice. The second call should not start a new thread. |
| 193 | + result = r.token() |
| 194 | + assert result == stale_token |
| 195 | + result = r.token() |
| 196 | + assert result == stale_token |
| 197 | + unblock() |
| 198 | + # Wait for the refresh to complete |
| 199 | + time.sleep(1) |
| 200 | + result = r.token() |
| 201 | + # Check that only one refresh was called |
| 202 | + assert r._refresh_count == 1 |
| 203 | + assert result == new_token |
| 204 | + |
| 205 | +def test_async_failure_disables_async(): |
| 206 | + stale_token = Token( |
| 207 | + access_token="access_token", |
| 208 | + expiry=datetime.now() + timedelta(seconds=59), |
| 209 | + ) |
| 210 | + new_token = Token( |
| 211 | + access_token="new_token", |
| 212 | + expiry=datetime.now() + timedelta(seconds=300), |
| 213 | + ) |
| 214 | + r = _MockRefreshable(token=stale_token, disable_async=False, refresh_effect=fail) |
| 215 | + # The call should fail and disable async refresh, |
| 216 | + # but the exception will be catch inside the tread. |
| 217 | + result = r.token() |
| 218 | + assert result == stale_token |
| 219 | + # Give time to the async refresh to fail |
| 220 | + time.sleep(1) |
| 221 | + assert r._refresh_err |
| 222 | + # Now, the refresh should be blocking. |
| 223 | + # Blocking refresh only happens for expired, not stale. |
| 224 | + # Therefore, the next call should return the stale token. |
| 225 | + r._refresh_effect = static_token(new_token, wait=1) |
| 226 | + result = r.token() |
| 227 | + assert result == stale_token |
| 228 | + # Wait to be sure no async thread was started |
| 229 | + time.sleep(1) |
| 230 | + assert r._refresh_count == 0 |
| 231 | + |
| 232 | + # Inject an expired token. |
| 233 | + expired_token = Token( |
| 234 | + access_token="access_token", |
| 235 | + expiry=datetime.now() - timedelta(seconds=300), |
| 236 | + ) |
| 237 | + r._token = expired_token |
| 238 | + |
| 239 | + # This should be blocking and return the new token. |
| 240 | + result = r.token() |
| 241 | + assert r._refresh_count == 1 |
| 242 | + assert result == new_token |
| 243 | + # The refresh error should be cleared. |
| 244 | + assert not r._refresh_err |
0 commit comments