1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
6 from builtins import object
11 from . import run_test_server
14 from . import arvados_testutil as tutil
15 from apiclient import http as apiclient_http
19 class ApiClientRetryTestMixin(object):
21 TEST_UUID = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
22 TEST_LOCATOR = 'd41d8cd98f00b204e9800998ecf8427e+0'
29 # Patch arvados.api() to return our mock API, so we can mock
31 self.api_client = arvados.api('v1', cache=False)
32 self.api_patch = mock.patch('arvados.api', return_value=self.api_client)
33 self.api_patch.start()
39 raise NotImplementedError("test subclasses must define run_method")
41 def test_immediate_success(self):
42 with tutil.mock_api_responses(self.api_client, '{}', [200]):
45 def test_immediate_failure(self):
46 with tutil.mock_api_responses(self.api_client, '{}', [400]), self.assertRaises(self.DEFAULT_EXCEPTION):
49 def test_retry_then_success(self):
50 with tutil.mock_api_responses(self.api_client, '{}', [500, 200]):
53 def test_error_after_default_retries_exhausted(self):
54 with tutil.mock_api_responses(self.api_client, '{}', [500, 500, 500, 500, 500, 500, 200]), self.assertRaises(self.DEFAULT_EXCEPTION):
57 def test_no_retry_after_immediate_success(self):
58 with tutil.mock_api_responses(self.api_client, '{}', [200, 400]):
62 class CurrentJobTestCase(ApiClientRetryTestMixin, unittest.TestCase):
64 DEFAULT_EXCEPTION = arvados.errors.ApiError
67 super(CurrentJobTestCase, self).setUp()
68 os.environ['JOB_UUID'] = 'zzzzz-zzzzz-zzzzzzzzzzzzzzz'
69 os.environ['JOB_WORK'] = '.'
72 del os.environ['JOB_UUID']
73 del os.environ['JOB_WORK']
74 arvados._current_job = None
75 super(CurrentJobTestCase, self).tearDown()