def test_interrupt(self): with self.server() as (wfile, rfile): import time knock(wfile, rfile) make_env = ''' import time def mapper(patch): time.sleep(1) return patch.size def reducer(a, b): return a + b chunksize = 1 ''' pickle.dump(api.WorkerRequest(make_env, {}), wfile) time.sleep(0.1) pickle.dump(api.Interrupt(), wfile) wfile.close() while True: res = pickle.load(rfile) if not isinstance(res, api.Progress): break self.assertIsInstance(res, api.UserError)
def test_unexpected_exception(self): with self.server() as (wfile, rfile): knock(wfile, rfile) make_env = ''' def mapper(patch): 0 / 0 def reducer(a, b): return a + b ''' pickle.dump(api.WorkerRequest(make_env, {}), wfile) wfile.close() while True: res = pickle.load(rfile) if not isinstance(res, api.Progress): break self.assertIsInstance(res, ZeroDivisionError)
def test_normal_case(self): with self.server() as (wfile, rfile): knock(wfile, rfile) make_env = ''' def mapper(patch): return patch.size def reducer(a, b): return a + b ''' pickle.dump(api.WorkerRequest(make_env, {}), wfile) wfile.close() while True: res = pickle.load(rfile) if not isinstance(res, api.Progress): break res: api.WorkerResult = res self.assertIsInstance(res, api.WorkerResult) self.assertEqual( res.value, reduce(lambda a, b: a + b, [p.size for p in patches('pdr2_dud')]), )
def post_request(worker, make_env, shared, progress1: Callable[[config.Worker, float], float], interrupt_notifiyer: SafeEvent): with socket.socket() as sock: sock.connect((worker.host, worker.port)) rfile = sock.makefile('rb') wfile = sock.makefile('wb', buffering=0) knock(wfile, rfile) request = api.WorkerRequest(make_env, shared) pickle.dump(request, wfile) with wait_for_safe_event(interrupt_notifiyer, lambda: pickle.dump(api.Interrupt(), wfile)): while True: res = pickle.load(rfile) if isinstance(res, api.Progress): progress1(worker, res) else: break if isinstance(res, api.WorkerResult): return res elif isinstance(res, api.UserError): raise UserError(res.reason) else: raise RuntimeError(f'{res}@{worker.host}')
def test_invalid_credentials(self): with self.server() as (wfile, rfile): with self.assertRaises(AuthError): knock(wfile, rfile, salt=b'invalid salt')