Skip to content

Commit c999c7c

Browse files
Fokkohpal
authored andcommitted
Fix retrying logic (apache#480)
1 parent 1534d2a commit c999c7c

2 files changed

Lines changed: 43 additions & 23 deletions

File tree

pyiceberg/catalog/rest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _retry_hook(retry_state: RetryCallState) -> None:
128128
_RETRY_ARGS = {
129129
"retry": retry_if_exception_type(AuthorizationExpiredError),
130130
"stop": stop_after_attempt(2),
131-
"before": _retry_hook,
131+
"before_sleep": _retry_hook,
132132
"reraise": True,
133133
}
134134

@@ -450,10 +450,10 @@ def _response_to_table(self, identifier_tuple: Tuple[str, ...], table_response:
450450
catalog=self,
451451
)
452452

453-
def _refresh_token(self, session: Optional[Session] = None, new_token: Optional[str] = None) -> None:
453+
def _refresh_token(self, session: Optional[Session] = None, initial_token: Optional[str] = None) -> None:
454454
session = session or self._session
455-
if new_token is not None:
456-
self.properties[TOKEN] = new_token
455+
if initial_token is not None:
456+
self.properties[TOKEN] = initial_token
457457
elif CREDENTIAL in self.properties:
458458
self.properties[TOKEN] = self._fetch_access_token(session, self.properties[CREDENTIAL])
459459

tests/catalog/test_rest.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,39 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
344344
]
345345

346346

347-
def test_list_namespaces_419(rest_mock: Mocker) -> None:
347+
def test_list_namespaces_token_expired(rest_mock: Mocker) -> None:
348348
new_token = "new_jwt_token"
349349
new_header = dict(TEST_HEADERS)
350350
new_header["Authorization"] = f"Bearer {new_token}"
351351

352-
rest_mock.post(
352+
namespaces = rest_mock.register_uri(
353+
"GET",
353354
f"{TEST_URI}v1/namespaces",
354-
json={
355-
"error": {
356-
"message": "Authorization expired.",
357-
"type": "AuthorizationExpiredError",
358-
"code": 419,
359-
}
360-
},
361-
status_code=419,
362-
request_headers=TEST_HEADERS,
363-
)
364-
rest_mock.post(
355+
[
356+
{
357+
"status_code": 419,
358+
"json": {
359+
"error": {
360+
"message": "Authorization expired.",
361+
"type": "AuthorizationExpiredError",
362+
"code": 419,
363+
}
364+
},
365+
"headers": TEST_HEADERS,
366+
},
367+
{
368+
"status_code": 200,
369+
"json": {"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
370+
"headers": new_header,
371+
},
372+
{
373+
"status_code": 200,
374+
"json": {"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
375+
"headers": new_header,
376+
},
377+
],
378+
)
379+
tokens = rest_mock.post(
365380
f"{TEST_URI}v1/oauth/tokens",
366381
json={
367382
"access_token": new_token,
@@ -371,19 +386,24 @@ def test_list_namespaces_419(rest_mock: Mocker) -> None:
371386
},
372387
status_code=200,
373388
)
374-
rest_mock.get(
375-
f"{TEST_URI}v1/namespaces",
376-
json={"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
377-
status_code=200,
378-
request_headers=new_header,
379-
)
380389
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN, credential=TEST_CREDENTIALS)
381390
assert catalog.list_namespaces() == [
382391
("default",),
383392
("examples",),
384393
("fokko",),
385394
("system",),
386395
]
396+
assert namespaces.call_count == 2
397+
assert tokens.call_count == 1
398+
399+
assert catalog.list_namespaces() == [
400+
("default",),
401+
("examples",),
402+
("fokko",),
403+
("system",),
404+
]
405+
assert namespaces.call_count == 3
406+
assert tokens.call_count == 1
387407

388408

389409
def test_create_namespace_200(rest_mock: Mocker) -> None:

0 commit comments

Comments
 (0)