Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_retry.py 2.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  1. import pytest
  2. import datetime
  3. import requests
  4. from wandb import retry
  5. from wandb import util
  6. from wandb.apis import CommError
  7. class FailException(Exception):
  8. pass
  9. def fail_for_n_function(n):
  10. call_num = [0] # use a list so we can modify in this scope
  11. def fn(an_arg):
  12. print(an_arg)
  13. try:
  14. if call_num[0] < n:
  15. raise retry.TransientException(
  16. exc=FailException('Failed at call_num: %s' % call_num))
  17. finally:
  18. call_num[0] += 1
  19. return True
  20. return fn
  21. def test_fail_for_n_function():
  22. failing_fn = fail_for_n_function(3)
  23. with pytest.raises(retry.TransientException):
  24. failing_fn('hello')
  25. with pytest.raises(retry.TransientException):
  26. failing_fn('hello')
  27. with pytest.raises(retry.TransientException):
  28. failing_fn('hello')
  29. assert failing_fn('hello')
  30. def test_retry_with_success():
  31. failing_fn = fail_for_n_function(3)
  32. fn = retry.Retry(failing_fn)
  33. fn('hello', retry_timedelta=datetime.timedelta(days=1), retry_sleep_base=0.001)
  34. assert fn.num_iters == 3
  35. def test_retry_with_timeout():
  36. failing_fn = fail_for_n_function(10000)
  37. fn = retry.Retry(failing_fn)
  38. with pytest.raises(retry.TransientException):
  39. fn('hello', retry_timedelta=datetime.timedelta(
  40. 0, 0, 0, 50), retry_sleep_base=0.001)
  41. def test_retry_with_noauth_401(capsys):
  42. def fail():
  43. res = requests.Response()
  44. res.status_code = 401
  45. raise retry.TransientException(exc=requests.HTTPError(response=res))
  46. fn = retry.Retry(fail, check_retry_fn=util.no_retry_auth)
  47. with pytest.raises(CommError) as excinfo:
  48. fn()
  49. assert excinfo.value.message == 'Invalid or missing api_key. Run wandb login'
  50. def test_retry_with_noauth_403(capsys):
  51. def fail():
  52. res = requests.Response()
  53. res.status_code = 403
  54. raise retry.TransientException(exc=requests.HTTPError(response=res))
  55. fn = retry.Retry(fail, check_retry_fn=util.no_retry_auth)
  56. with pytest.raises(CommError) as excinfo:
  57. fn()
  58. assert excinfo.value.message == 'Permission denied, ask the project owner to grant you access'
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...