|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import shutil |
|
import signal |
|
import tempfile |
|
from io import BytesIO |
|
|
|
from s3transfer.compat import BaseManager, readable, seekable |
|
from tests import skip_if_windows, unittest |
|
|
|
|
|
class ErrorRaisingSeekWrapper: |
|
"""An object wrapper that throws an error when seeked on |
|
|
|
:param fileobj: The fileobj that it wraps |
|
:param exception: The exception to raise when seeked on. |
|
""" |
|
|
|
def __init__(self, fileobj, exception): |
|
self._fileobj = fileobj |
|
self._exception = exception |
|
|
|
def seek(self, offset, whence=0): |
|
raise self._exception |
|
|
|
def tell(self): |
|
return self._fileobj.tell() |
|
|
|
|
|
class TestSeekable(unittest.TestCase): |
|
def setUp(self): |
|
self.tempdir = tempfile.mkdtemp() |
|
self.filename = os.path.join(self.tempdir, 'foo') |
|
|
|
def tearDown(self): |
|
shutil.rmtree(self.tempdir) |
|
|
|
def test_seekable_fileobj(self): |
|
with open(self.filename, 'w') as f: |
|
self.assertTrue(seekable(f)) |
|
|
|
def test_non_file_like_obj(self): |
|
|
|
self.assertFalse(seekable(object())) |
|
|
|
def test_non_seekable_ioerror(self): |
|
|
|
with open(self.filename, 'w') as f: |
|
self.assertFalse(seekable(ErrorRaisingSeekWrapper(f, IOError()))) |
|
|
|
def test_non_seekable_oserror(self): |
|
|
|
with open(self.filename, 'w') as f: |
|
self.assertFalse(seekable(ErrorRaisingSeekWrapper(f, OSError()))) |
|
|
|
|
|
class TestReadable(unittest.TestCase): |
|
def test_readable_fileobj(self): |
|
with tempfile.TemporaryFile() as f: |
|
self.assertTrue(readable(f)) |
|
|
|
def test_readable_file_like_obj(self): |
|
self.assertTrue(readable(BytesIO())) |
|
|
|
def test_non_file_like_obj(self): |
|
self.assertFalse(readable(object())) |
|
|
|
|
|
class TestBaseManager(unittest.TestCase): |
|
def create_pid_manager(self): |
|
class PIDManager(BaseManager): |
|
pass |
|
|
|
PIDManager.register('getpid', os.getpid) |
|
return PIDManager() |
|
|
|
def get_pid(self, pid_manager): |
|
pid = pid_manager.getpid() |
|
|
|
|
|
return int(str(pid)) |
|
|
|
@skip_if_windows('os.kill() with SIGINT not supported on Windows') |
|
def test_can_provide_signal_handler_initializers_to_start(self): |
|
manager = self.create_pid_manager() |
|
manager.start(signal.signal, (signal.SIGINT, signal.SIG_IGN)) |
|
pid = self.get_pid(manager) |
|
try: |
|
os.kill(pid, signal.SIGINT) |
|
except KeyboardInterrupt: |
|
pass |
|
|
|
|
|
self.assertEqual(pid, self.get_pid(manager)) |
|
|