def testCallOnThread(self): main_thread = threading.current_thread() def callback(arg1, arg2): self.assertEquals(1, arg1) self.assertEquals(2, arg2) my_thread = threading.current_thread() self.assertNotEquals(my_thread, main_thread) return 3 result = concurrent.CallOnThread(callback, 1, arg2=2) self.assertEquals(3, result.get())
def SaveDeltaSizeInfo(delta_size_info, path, file_obj=None): """Saves |delta_size_info| to |path|.""" changed_symbols = delta_size_info.raw_symbols \ .WhereDiffStatusIs(models.DIFF_STATUS_UNCHANGED).Inverted() before_symbols = models.SymbolGroup( [sym.before_symbol for sym in changed_symbols if sym.before_symbol]) after_symbols = models.SymbolGroup( [sym.after_symbol for sym in changed_symbols if sym.after_symbol]) before_size_file = cStringIO.StringIO() after_size_file = cStringIO.StringIO() after_promise = concurrent.CallOnThread(SaveSizeInfo, delta_size_info.after, '', file_obj=after_size_file, include_padding=True, sparse_symbols=after_symbols) SaveSizeInfo(delta_size_info.before, '', file_obj=before_size_file, include_padding=True, sparse_symbols=before_symbols) with file_obj or open(path, 'wb') as output_file: output_file.write(_SIZEDIFF_HEADER) # JSON metadata headers = { 'version': 1, 'before_length': before_size_file.tell(), } metadata_str = json.dumps(headers, output_file, indent=2, sort_keys=True) output_file.write('%d\n' % len(metadata_str)) output_file.write(metadata_str) output_file.write('\n') before_size_file.seek(0) shutil.copyfileobj(before_size_file, output_file) after_promise.get() after_size_file.seek(0) shutil.copyfileobj(after_size_file, output_file)