19686: api constructor returns ThreadSafeApiCache
[arvados.git] / sdk / python / tests / test_api.py
index 60183e06a352259530534bedf56da1bbba5c3443..03af8ce5ee181e8bc95cddd724ef7c65c734131f 100644 (file)
@@ -82,6 +82,19 @@ class ArvadosApiTest(run_test_server.TestCaseWithServers):
         for msg in ["Bad UUID format", "Bad output format"]:
             self.assertIn(msg, err_s)
 
+    @mock.patch('time.sleep')
+    def test_exceptions_include_request_id(self, sleep):
+        api = arvados.api('v1')
+        api.request_id='fake-request-id'
+        api._http.orig_http_request = mock.MagicMock()
+        api._http.orig_http_request.side_effect = socket.error('mock error')
+        caught = None
+        try:
+            api.users().current().execute()
+        except Exception as e:
+            caught = e
+        self.assertRegex(str(caught), r'fake-request-id')
+
     def test_exceptions_without_errors_have_basic_info(self):
         mock_responses = {
             'arvados.humans.delete': (
@@ -101,11 +114,15 @@ class ArvadosApiTest(run_test_server.TestCaseWithServers):
             text = "X" * maxsize
             arvados.api('v1').collections().create(body={"manifest_text": text}).execute()
 
-    # Checks for bug #17171
     def test_default_request_timeout(self):
         api = arvados.api('v1')
-        self.assertEqual(api._http.timeout, 10,
-            "Default timeout value should be 10")
+        self.assertEqual(api._http.timeout, 300,
+            "Default timeout value should be 300")
+
+    def test_custom_request_timeout(self):
+        api = arvados.api('v1', timeout=1234)
+        self.assertEqual(api._http.timeout, 1234,
+            "Requested timeout value was 1234")
 
     def test_ordered_json_model(self):
         mock_responses = {
@@ -122,6 +139,27 @@ class ArvadosApiTest(run_test_server.TestCaseWithServers):
         result = api.humans().get(uuid='test').execute()
         self.assertEqual(string.hexdigits, ''.join(list(result.keys())))
 
+    def test_api_is_threadsafe(self):
+        api_kwargs = {
+            'host': os.environ['ARVADOS_API_HOST'],
+            'token': os.environ['ARVADOS_API_TOKEN'],
+            'insecure': True,
+        }
+        config_kwargs = {'apiconfig': os.environ}
+        for api_constructor, kwargs in [
+                (arvados.api, {}),
+                (arvados.api, api_kwargs),
+                (arvados.api_from_config, {}),
+                (arvados.api_from_config, config_kwargs),
+        ]:
+            sub_kwargs = "kwargs" if kwargs else "no kwargs"
+            with self.subTest(f"{api_constructor.__name__} with {sub_kwargs}"):
+                api_client = api_constructor('v1', **kwargs)
+                self.assertTrue(hasattr(api_client, 'localapi'),
+                                f"client missing localapi method")
+                self.assertTrue(hasattr(api_client, 'keep'),
+                                f"client missing keep attribute")
+
 
 class RetryREST(unittest.TestCase):
     def setUp(self):