def test_block_size_when_chunks_exceeded_limit(self, getsize):
        # mock
        getsize.return_value = 4 * 1024 * 1024 * 1024 * 1024
        default_block_size = 4 * 1024 * 1024

        block_size = FileService.get_block_size("test")

        self.assertNotEqual(default_block_size, block_size)
    def test_block_size_should_be_default(self, getsize):
        # mock
        getsize.return_value = 128
        default_block_size = 4 * 1024 * 1024

        block_size = FileService.get_block_size("test")

        self.assertEqual(default_block_size, block_size)
Esempio n. 3
0
 def run(self):
     init()
     auth = (self.user, self.auth_token)
     block_size = FileService.get_block_size(self.file)
     file_size = os.path.getsize(self.file)
     expected_chunks = (file_size // block_size) + 1
     lock = Lock()
     click.echo('Preparing file to upload: %s' %
                click.format_filename(self.file))
     t = tqdm(total=expected_chunks, unit='B', unit_scale=False)
     t.set_description("Uploading progress")
     count = itertools.count()
     try:
         with ThreadPoolExecutor(max_workers=4) as executor:
             uploads = []
             with open(self.file, 'rb') as infile:
                 buf = infile.read(block_size)
                 while len(buf) > 0:
                     self.semaphore.acquire()
                     try:
                         future = executor.submit(self.process_chunk,
                                                  self.base_url, auth,
                                                  self.issue_key, buf, lock,
                                                  t, count)
                         uploads.append(future)
                         buf = infile.read(block_size)
                     except:
                         self.semaphore.release()
                         raise
                     else:
                         future.add_done_callback(
                             lambda x: self.semaphore.release())
             chunk_list = [
                 future.result() for future in futures.as_completed(uploads)
             ]
         self.create_file_chunked(self.base_url, auth, chunk_list,
                                  self.file, self.issue_key)
         t.update(1)
         t.close()
         click.echo(
             'The file has been successfully uploaded and attached to the ticket %s'
             % self.issue_key)
     except AuthException as e:
         t.close()
         print(Fore.RED +
               "[ERROR] Authentication error. Check your API credentials.")
         raise e
     except Exception as e:
         print(Fore.RED + "[ERROR] " + str(e))
         raise e