def test_gmpy2_parallelism_is_safe(self): """ Ensures running lots of parallel exponentiations still yields the correct answer. This verifies that nothing incorrect is happening in the GMPY2 library """ # Arrange scheduler = Scheduler() problem_size = 1000 random_secret_keys = Nonces(int_to_q_unchecked(3))[0:problem_size] log_info( f"testing GMPY2 powmod parallelism safety (cpus = {scheduler.cpu_count}, problem_size = {problem_size})" ) # Act start = timer() keypairs = scheduler.schedule( elgamal_keypair_from_secret, [list([secret_key]) for secret_key in random_secret_keys], ) end1 = timer() # Assert for keypair in keypairs: self.assertEqual( keypair.public_key, elgamal_keypair_from_secret(keypair.secret_key).public_key, ) end2 = timer() scheduler.close() log_info(f"Parallelism speedup: {(end2 - end1) / (end1 - start):.3f}")
def test_schedule_callable_throws(self): # Arrange subject = Scheduler() # Act result = subject.schedule(_exception_callable, [list([1]), list([2])]) # Assert self.assertIsNotNone(result) self.assertIsInstance(result, List) subject.close()
def test_safe_map(self): # Arrange from multiprocessing import Pool process_pool = Pool(1) subject = Scheduler() # Act result = subject.safe_map(process_pool, _callable, [1]) self.assertEqual(result, [1]) # verify exceptions are caught result = subject.safe_map(process_pool, _exception_callable, [1]) self.assertIsNotNone(result) self.assertIsInstance(result, List) # verify closing the poll catches the value error process_pool.close() result = subject.safe_map(process_pool, _callable, [1]) self.assertIsNotNone(result) self.assertIsInstance(result, List) subject.close()