def testMakeHumanReadable(self): """Tests converting byte counts to human-readable strings.""" self.assertEqual(unit_util.MakeHumanReadable(0), '0 B') self.assertEqual(unit_util.MakeHumanReadable(1023), '1023 B') self.assertEqual(unit_util.MakeHumanReadable(1024), '1 KiB') self.assertEqual(unit_util.MakeHumanReadable(1024**2), '1 MiB') self.assertEqual(unit_util.MakeHumanReadable(1024**3), '1 GiB') self.assertEqual(unit_util.MakeHumanReadable(1024**3 * 5.3), '5.3 GiB') self.assertEqual(unit_util.MakeHumanReadable(1024**4 * 2.7), '2.7 TiB') self.assertEqual(unit_util.MakeHumanReadable(1024**5), '1 PiB') self.assertEqual(unit_util.MakeHumanReadable(1024**6), '1 EiB')
def testEstimateWithSize(self): """Tests SeekAheadThread providing an object count and total size.""" class SeekAheadResultIteratorWithSize(object): """Yields dummy result of the given size.""" def __init__(self, num_objects, size): self.num_objects = num_objects self.size = size self.yielded = 0 def __iter__(self): while self.yielded < self.num_objects: yield SeekAheadResult(data_bytes=self.size) self.yielded += 1 cancel_event = threading.Event() status_queue = Queue.Queue() stream = six.StringIO() ui_controller = UIController() ui_thread = UIThread(status_queue, stream, ui_controller) num_objects = 5 object_size = 10 seek_ahead_iterator = SeekAheadResultIteratorWithSize( num_objects, object_size) seek_ahead_thread = SeekAheadThread(seek_ahead_iterator, cancel_event, status_queue) seek_ahead_thread.join(self.thread_wait_time) status_queue.put(_ZERO_TASKS_TO_DO_ARGUMENT) ui_thread.join(self.thread_wait_time) if seek_ahead_thread.is_alive(): seek_ahead_thread.terminate = True self.fail('SeekAheadThread is still alive.') message = stream.getvalue() if not message: self.fail('Status queue empty but SeekAheadThread should have posted ' 'summary message') total_size = num_objects * object_size self.assertEqual( message, 'Estimated work for this command: objects: %s, total size: %s\n' % (num_objects, unit_util.MakeHumanReadable(total_size)))
def testWithLocalFiles(self): """Tests SeekAheadThread with an actual directory.""" tmpdir = self.CreateTempDir() num_files = 5 total_size = 0 # Create 5 files with sizes 0, 1, 2, 3, 4. for i in xrange(num_files): self.CreateTempFile(tmpdir=tmpdir, file_name='obj%s' % str(i), contents='a' * i) total_size += i # Recursively "copy" tmpdir. seek_ahead_iterator = SeekAheadNameExpansionIterator( 'cp', 0, None, [tmpdir], True) cancel_event = threading.Event() status_queue = Queue.Queue() stream = StringIO.StringIO() ui_controller = UIController() ui_thread = UIThread(status_queue, stream, ui_controller) seek_ahead_thread = SeekAheadThread(seek_ahead_iterator, cancel_event, status_queue) seek_ahead_thread.join(self.thread_wait_time) status_queue.put(_ZERO_TASKS_TO_DO_ARGUMENT) ui_thread.join(self.thread_wait_time) if seek_ahead_thread.isAlive(): seek_ahead_thread.terminate = True self.fail('SeekAheadThread is still alive.') message = stream.getvalue() if not message: self.fail( 'Status queue empty but SeekAheadThread should have posted ' 'summary message') self.assertEqual( message, 'Estimated work for this command: objects: %s, total size: %s\n' % (num_files, unit_util.MakeHumanReadable(total_size)))