def test_redirect(self): stream = lab.download_file('http://scripts.mit.edu/~6.009/lab9/redir.py/0/fivedollars.wav') self.assertEqual(b''.join(stream), RESULTS['test_redirect']) with self.assertRaises(RuntimeError): stream = lab.download_file('http://scripts.mit.edu/~6.009/lab9/always_error.py/') out = b''.join(stream) with self.assertRaises(RuntimeError): stream = lab.download_file('http://nonexistent.mit.edu/hello.txt') out = b''.join(stream) with self.assertRaises(FileNotFoundError): stream = lab.download_file('http://hz.mit.edu/some_file_that_doesnt_exist.txt') out = b''.join(stream)
def test_cache(self): # test that cache hits speed things up t = time.time() stream = lab.download_file('http://mit.edu/6.009/www/lab9_examples/happycat.png.parts') result = b''.join(stream) expected = 10*RESULTS['test_caching.1'] self.assertEqual(result, expected) self.assertTrue(time.time() - t < 15, msg='Test took too long.') # test that caching isn't done unnecessarily (only where specified) stream = lab.download_file('http://mit.edu/6.009/www/lab9_examples/numbers.png.parts') result = b''.join(stream) count = sum(i in result for i in RESULTS['test_caching.2']) self.assertTrue(count > 1)
def test_icantcachecani(self): stream = lab.download_file( 'http://mit.edu/6.009/www/spring19/lab10_examples/cachesucs.txt.parts' ) alltext = b''.join(stream).decode("utf-8").split("\n") self.assertTrue(len(alltext) > 5, "did not yield all times") time1 = alltext[0] time2 = alltext[1] timeCached = alltext[2] timeCached2 = alltext[3] time4 = alltext[4] time5 = alltext[5] self.assertTrue(time1 != time2, msg="Caching should only occur when (*) appears") self.assertTrue(time1 == timeCached or time2 == timeCached, msg="File was not cached") self.assertTrue( timeCached == timeCached2, msg= "Manifest should use cache first if one file has already been cached" ) self.assertTrue(time1 == timeCached2 or time2 == timeCached2, msg="File was not cached") self.assertTrue(time5 != time4, msg="You should only cache if (*) appears") self.assertTrue( timeCached != time4, msg="If you got this error, your caching is really wrong")
def test_streaming(self): t = time.time() x1, x2 = RESULTS['test_streaming'] stream = lab.download_file('http://scripts.mit.edu/~6.009/lab9/test_stream.py') self.assertEqual(next(stream), x1) self.assertEqual(next(stream), x2) self.assertTrue((time.time()-t) < 30, msg="Download took too long.")
def text_viewer(): d = download_file(URL) text = tkinter.Text(root, height=24, width=80) text.pack(fill='both', expand=True) if '-seq' in URL: d = files_from_sequence(d) def update_text(): try: if '-seq' in URL: new_text = next(d) else: new_text = b''.join(d) except (RuntimeError, FileNotFoundError): root.destroy() raise if new_text: text.delete(1.0, tkinter.END) try: text.insert(tkinter.END, new_text.decode('utf-8')) except Exception: root.destroy() raise tcl.after(delta_t, update_text) tcl.after(0, update_text)
def test_hello6009world(self): fileContents = lab.download_file( 'https://6009.cat-soop.org/_static/spring19/labs/lab10/helloworld.txt' ) self.assertEqual(next(fileContents), b'Hello 6.009!\n') try: nextValues = next(fileContents) self.assertTrue(False, "Stream did not end") except StopIteration: self.assertTrue(True)
def test_cachecachecachecache(self): t = time.time() stream = lab.download_file( 'http://mit.edu/6.009/www/spring19/lab10_examples/happycat_twice.png.parts' ) result = b''.join(stream) expected = 3 * RESULTS['test_caching.1'] self.assertEqual(result, expected) completedTime = time.time() - t self.assertTrue(completedTime < 10, msg='Test took too long.') t = time.time() stream = lab.download_file( 'http://mit.edu/6.009/www/spring19/lab10_examples/happycat_twice.png.parts' ) result = b''.join(stream) self.assertTrue( abs(time.time() - t - completedTime) < 5, msg='Caching should be independent of download_file calls.')
def image_viewer(): d = download_file(URL) canvas = tkinter.Canvas(root, height=480, width=640) canvas.pack(fill='both', expand=True) if '-seq' in URL: d = files_from_sequence(d) def update_animation(): try: if '-seq' in URL: frame = next(d) else: frame = b''.join(d) if (frame == b''): print("GUI done") return except (RuntimeError, FileNotFoundError): root.destroy() print("GUI done") raise b = io.BytesIO(frame) b2 = io.BytesIO() try: im = PIL.Image.open(b) im.save(b2, 'GIF') canvas.configure(height=im.size[1], width=im.size[0]) except: print('Invalid image file', file=sys.stderr) root.destroy() raise canvas.img = tkinter.PhotoImage(data=base64.b64encode(b2.getvalue())) img = canvas.create_image(0, 0, image=canvas.img, anchor=tkinter.NW) tcl.after(delta_t, update_animation) tcl.after(0, update_animation)
def worker(): stream = lab.download_file( 'http://mit.edu/6.009/www/spring19/lab10_examples/repeat.txt.parts' ) result = b''.join(stream)
def test_ireallylikecats(self): stream = lab.download_file( 'http://scripts.mit.edu/~6.009/spring19/lab10/redir.py/0/cat_poster.jpg.parts' ) self.assertEqual(b''.join(stream), b''.join(RESULTS['test_big']))