|
| 1 | +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | +from __future__ import division |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import tensorflow as tf |
| 21 | + |
| 22 | +from tensorflow.contrib.learn.python.learn.datasets import base |
| 23 | + |
| 24 | +mock = tf.test.mock |
| 25 | + |
| 26 | + |
| 27 | +_TIMEOUT = IOError(110, "timeout") |
| 28 | + |
| 29 | + |
| 30 | +class BaseTest(tf.test.TestCase): |
| 31 | + """Test load csv functions.""" |
| 32 | + |
| 33 | + def testUrlretrieveRetriesOnIOError(self): |
| 34 | + with mock.patch.object(base, "time") as mock_time: |
| 35 | + with mock.patch.object(base, "urllib") as mock_urllib: |
| 36 | + mock_urllib.request.urlretrieve.side_effect = [ |
| 37 | + _TIMEOUT, |
| 38 | + _TIMEOUT, |
| 39 | + _TIMEOUT, |
| 40 | + _TIMEOUT, |
| 41 | + _TIMEOUT, |
| 42 | + None |
| 43 | + ] |
| 44 | + base.urlretrieve_with_retry("http://dummy.com", "/tmp/dummy") |
| 45 | + |
| 46 | + # Assert full backoff was tried |
| 47 | + actual_list = [arg[0][0] for arg in mock_time.sleep.call_args_list] |
| 48 | + expected_list = [1, 2, 4, 8, 16] |
| 49 | + for actual, expected in zip(actual_list, expected_list): |
| 50 | + self.assertLessEqual(abs(actual - expected), 0.25 * expected) |
| 51 | + self.assertEquals(len(actual_list), len(expected_list)) |
| 52 | + |
| 53 | + def testUrlretrieveRaisesAfterRetriesAreExhausted(self): |
| 54 | + with mock.patch.object(base, "time") as mock_time: |
| 55 | + with mock.patch.object(base, "urllib") as mock_urllib: |
| 56 | + mock_urllib.request.urlretrieve.side_effect = [ |
| 57 | + _TIMEOUT, |
| 58 | + _TIMEOUT, |
| 59 | + _TIMEOUT, |
| 60 | + _TIMEOUT, |
| 61 | + _TIMEOUT, |
| 62 | + _TIMEOUT, |
| 63 | + ] |
| 64 | + with self.assertRaises(IOError): |
| 65 | + base.urlretrieve_with_retry("http://dummy.com", "/tmp/dummy") |
| 66 | + |
| 67 | + # Assert full backoff was tried |
| 68 | + actual_list = [arg[0][0] for arg in mock_time.sleep.call_args_list] |
| 69 | + expected_list = [1, 2, 4, 8, 16] |
| 70 | + for actual, expected in zip(actual_list, expected_list): |
| 71 | + self.assertLessEqual(abs(actual - expected), 0.25 * expected) |
| 72 | + self.assertEquals(len(actual_list), len(expected_list)) |
| 73 | + |
| 74 | + def testUrlretrieveRaisesOnNonRetriableErrorWithoutRetry(self): |
| 75 | + with mock.patch.object(base, "time") as mock_time: |
| 76 | + with mock.patch.object(base, "urllib") as mock_urllib: |
| 77 | + mock_urllib.request.urlretrieve.side_effect = [ |
| 78 | + IOError(2, "No such file or directory"), |
| 79 | + ] |
| 80 | + with self.assertRaises(IOError): |
| 81 | + base.urlretrieve_with_retry("http://dummy.com", "/tmp/dummy") |
| 82 | + |
| 83 | + # Assert no retries |
| 84 | + self.assertFalse(mock_time.called) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + tf.test.main() |
0 commit comments