OLD | NEW |
(Empty) | |
| 1 # Copyright 2010 Google Inc. |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 """ |
| 23 Test harness that allows us to raise exceptions, change file content, |
| 24 and record the byte transfer callback sequence, to test various resumable |
| 25 upload and download cases. The 'call' method of this harness can be passed |
| 26 as the 'cb' parameter to boto.s3.Key.send_file() and boto.s3.Key.get_file(), |
| 27 allowing testing of various file upload/download conditions. |
| 28 """ |
| 29 |
| 30 import socket |
| 31 |
| 32 |
| 33 class CallbackTestHarness(object): |
| 34 |
| 35 def __init__(self, fail_after_n_bytes=0, num_times_to_fail=1, |
| 36 exception=socket.error('mock socket error', 0), |
| 37 fp_to_change=None, fp_change_pos=None): |
| 38 self.fail_after_n_bytes = fail_after_n_bytes |
| 39 self.num_times_to_fail = num_times_to_fail |
| 40 self.exception = exception |
| 41 # If fp_to_change and fp_change_pos are specified, 3 bytes will be |
| 42 # written at that position just before the first exception is thrown. |
| 43 self.fp_to_change = fp_to_change |
| 44 self.fp_change_pos = fp_change_pos |
| 45 self.num_failures = 0 |
| 46 self.transferred_seq_before_first_failure = [] |
| 47 self.transferred_seq_after_first_failure = [] |
| 48 |
| 49 def call(self, total_bytes_transferred, unused_total_size): |
| 50 """ |
| 51 To use this test harness, pass the 'call' method of the instantiated |
| 52 object as the cb param to the set_contents_from_file() or |
| 53 get_contents_to_file() call. |
| 54 """ |
| 55 # Record transfer sequence to allow verification. |
| 56 if self.num_failures: |
| 57 self.transferred_seq_after_first_failure.append( |
| 58 total_bytes_transferred) |
| 59 else: |
| 60 self.transferred_seq_before_first_failure.append( |
| 61 total_bytes_transferred) |
| 62 if (total_bytes_transferred >= self.fail_after_n_bytes and |
| 63 self.num_failures < self.num_times_to_fail): |
| 64 self.num_failures += 1 |
| 65 if self.fp_to_change and self.fp_change_pos is not None: |
| 66 cur_pos = self.fp_to_change.tell() |
| 67 self.fp_to_change.seek(self.fp_change_pos) |
| 68 self.fp_to_change.write('abc') |
| 69 self.fp_to_change.seek(cur_pos) |
| 70 self.called = True |
| 71 raise self.exception |
OLD | NEW |