self.assertFalse(self.dl.is_running())
self.finished.assert_called_with(self.dl) # file has been downloaded with open(self.tempfile) as f:
self.assertEquals(f.read(), '1234' * 4)
with self.assertRaises(download_manager.DownloadInterrupt):
self.dl.wait()
self.assertTrue(self.dl.is_canceled())
# response generation should have taken 1000 * 0.01 = 10 seconds. # since we canceled, this must be lower.
self.assertTrue((time.time() - start) < 1.0)
# file was deleted
self.assertFalse(os.path.exists(self.tempfile)) # finished callback was called
self.finished.assert_called_with(self.dl)
self.dl.start() with self.assertRaises(IOError):
self.dl.wait()
self.assertEquals(self.dl.error()[0], IOError) # finished callback was called
self.finished.assert_called_with(self.dl)
def test_wait_does_not_block_on_exception(self): # this test the case when a user may hit CTRL-C for example # during a dl.wait() call.
self.create_response('1234' * 1000, wait=0.01)
original_join = self.dl.thread.join
it = iter('123')
def join(timeout=None):
next(it) # will throw StopIteration after a few calls
original_join(timeout)
self.dl.thread.join = join
start = time.time()
self.dl.start()
with self.assertRaises(StopIteration):
self.dl.wait()
self.assertTrue(self.dl.is_canceled()) # wait for the thread to finish
original_join()
# response generation should have taken 1000 * 0.01 = 10 seconds. # since we got an error, this must be lower.
self.assertTrue((time.time() - start) < 1.0)
# file was deleted
self.assertFalse(os.path.exists(self.tempfile)) # finished callback was called
self.finished.assert_called_with(self.dl)
class TestDownloadManager(unittest.TestCase): def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.tempdir)
def do_download(self, url, fname, data, wait=0):
session, response = mock_session()
mock_response(response, data, wait) # patch the session, so the download will use that
self.dl_manager.session = session return self.dl_manager.download(url, fname)
# with the same fname, no new download is started. The same instance # is returned since the download is running.
dl2 = self.do_download('http://bar', 'foo', 'hello2' * 4, wait=0.02)
self.assertEquals(dl1, dl2)
# starting a download with another fname will trigger a new download
dl3 = self.do_download('http://bar', 'foo2', 'hello you' * 4)
self.assertIsInstance(dl3, download_manager.Download)
self.assertNotEquals(dl3, dl1)
# let's wait for the downloads to finish
dl3.wait()
dl1.wait()
# now if we try to download a fname that exists, None is returned
dl4 = self.do_download('http://bar', 'foo', 'hello2' * 4, wait=0.02)
self.assertIsNone(dl4)
# downloaded files are what is expected def content(fname): with open(os.path.join(self.tempdir, fname)) as f: return f.read()
self.assertEquals(content('foo'), 'hello' * 4)
self.assertEquals(content('foo2'), 'hello you' * 4)
# download instances are removed from the manager (internal test)
self.assertEquals(self.dl_manager._downloads, {})
self.assertTrue(dl1.is_canceled())
self.assertTrue(dl2.is_canceled()) # dl3 is not canceled since it finished before
self.assertFalse(dl3.is_canceled())
# wait for the completion of dl1 and dl2 threads
dl1.wait(raise_if_error=False)
dl2.wait(raise_if_error=False)
# at the end, only dl3 has been downloaded
self.assertEquals(os.listdir(self.tempdir), ["foobar"])
with open(os.path.join(self.tempdir, 'foobar')) as f:
self.assertEquals(f.read(), 'foobar' * 4)
# download instances are removed from the manager (internal test)
self.assertEquals(self.dl_manager._downloads, {})
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.