def test_qcomm(): "Test queue-based bidirectional communicator." inq = queue.Queue() outq = queue.Queue() comm = comms.QComm(inq, outq) comm.send('a', 1) comm.send('b') assert outq.get() == ('a', 1) and outq.get() == ('b',) and outq.empty(), \ "Check send appropriately goes to output queue." comm.push_to_buffer('b', 0) inq.put(('c', 3)) inq.put(('d', )) assert (comm.recv() == ('b', 0) and comm.recv() == ('c', 3) and comm.recv() == ('d',) and inq.empty()), \ "Check recv appropriately comes from input queue." flag = True try: comm.recv(0.1) flag = False except comms.Timeout: pass assert flag, "Check comms receive returns Timeout at appropriate time."
def test_sim_comm_handler(): "Test SimCommHandler interface" class TestSimComm(comms.SimCommHandler): "Dummy SimCommHandler" def on_request(self, sim_id, recs): return "on_request", sim_id, recs def on_kill(self, sim_id): return "on_kill", sim_id inq = queue.Queue() outq = queue.Queue() comm = comms.QComm(inq, outq) scomm = TestSimComm(comm) scomm.send_result(1, None) scomm.send_update(1, 10) scomm.send_killed(1) assert outq.get() == ('result', 1, None) assert outq.get() == ('update', 1, 10) assert outq.get() == ('killed', 1) assert outq.empty() inq.put(('request', 1, 100)) inq.put(('kill', 1)) inq.put(('qwerty', )) assert scomm.process_message() == ('on_request', 1, 100) assert scomm.process_message() == ('on_kill', 1) flag = True try: scomm.process_message(0.1) except ValueError: pass assert flag, "Check error handling for bad message type" flag = True try: scomm.process_message(0.1) flag = False except comms.Timeout: pass assert flag, "Check timeout on process_message" flag = True try: inq.put(('stop', )) scomm.process_message(0.1) flag = False except comms.ManagerStop: pass assert flag, "Check exception raised on manager requested stop"
def test_gen_comm_handler(): "Test GenCommHandler interface" class TestGenComm(comms.GenCommHandler): "Dummy GenComm handler" def on_worker_avail(self, nworker): return "on_worker_avail", nworker def on_queued(self, sim_id): return "on_queued", sim_id def on_result(self, sim_id, recs): return "on_result", sim_id, recs def on_update(self, sim_id, recs): return "on_update", sim_id, recs def on_killed(self, sim_id): return "on_killed", sim_id inq = queue.Queue() outq = queue.Queue() comm = comms.QComm(inq, outq) gcomm = TestGenComm(comm) gcomm.send_request(None) gcomm.send_kill(10) gcomm.send_get_history(1, 2) gcomm.send_subscribe() assert outq.get() == ('request', None) assert outq.get() == ('kill', 10) assert outq.get() == ('get_history', 1, 2) assert outq.get() == ('subscribe', ) assert outq.empty() inq.put(('worker_avail', 3)) inq.put(('queued', 1)) inq.put(('result', 1, 100)) inq.put(('update', 1, 50)) inq.put(('killed', 1)) inq.put(('qwerty', )) assert gcomm.process_message() == ('on_worker_avail', 3) assert gcomm.process_message() == ('on_queued', 1) assert gcomm.process_message() == ('on_result', 1, 100) assert gcomm.process_message() == ('on_update', 1, 50) assert gcomm.process_message() == ('on_killed', 1) flag = True try: gcomm.process_message(0.1) except ValueError: pass assert flag, "Check error handling for bad message type" flag = True try: gcomm.process_message(0.1) flag = False except comms.Timeout: pass assert flag, "Check timeout on process_message" flag = True try: inq.put(('stop', )) gcomm.process_message(0.1) flag = False except comms.ManagerStop: pass assert flag, "Check exception raised on manager requested stop"
def test_comm_eval(): "Test CommEval and Future interfaces" gen_specs = {'out': [('x', float), ('flag', bool)]} inq = queue.Queue() outq = queue.Queue() comm = comms.QComm(inq, outq) gcomm = comms.CommEval(comm, gen_specs=gen_specs) inq.put(('worker_avail', 3)) inq.put(('queued', 1)) H_o = np.zeros(2, dtype=gen_specs['out']) promises = gcomm.request(H_o) assert len(promises) == 2 assert gcomm.workers == 3 assert gcomm.sim_started == 2 assert gcomm.sim_pending == 2 assert outq.get(timeout=0.1)[0] == 'request' assert outq.empty() inq.put(('queued', 3)) promise = gcomm(10.0, True) assert outq.get(timeout=0.1)[0] == 'request' assert not promise.cancelled() and not promise.done() assert gcomm.sim_started == 3 assert gcomm.sim_pending == 3 flag = True try: promise.result(timeout=0.1) flag = False except comms.Timeout: pass assert flag, "Checking timeout on result check" promise.cancel() assert outq.get(timeout=0.1) == ('kill', 3) assert not promise.cancelled() and not promise.done() inq.put(('killed', 3)) gcomm.process_message(timeout=0.1) assert promise.cancelled() and promise.done() assert promise.result() is None assert gcomm.sim_started == 3 assert gcomm.sim_pending == 2 results = np.zeros(3, dtype=[('f', float)]) results['f'] = [20, 10, 15] resultsf = results['f'] resultsf[0] = 15 inq.put(('update', 1, results[0])) gcomm.process_message(timeout=0.1) assert not promises[0].cancelled() and not promises[0].done() assert promises[0].current_result == 15 assert gcomm.sim_started == 3 assert gcomm.sim_pending == 2 resultsf[0] = 20 inq.put(('result', 1, results[0])) gcomm.process_message(timeout=0.1) assert not promises[0].cancelled() and promises[0].done() assert promises[0].result() == 20 assert gcomm.sim_started == 3 assert gcomm.sim_pending == 1 inq.put(('update', 2, results[1])) gcomm.process_message(timeout=0.1) assert not promises[1].cancelled() and not promises[1].done() assert gcomm.sim_started == 3 assert gcomm.sim_pending == 1 inq.put(('result', 2, results[2])) gcomm.process_message(timeout=0.1) assert not promises[1].cancelled() and promises[1].done() assert gcomm.sim_started == 3 assert gcomm.sim_pending == 0 inq.put(('queued', 4)) promise = gcomm(x=5.0) msg_type, recs = outq.get() assert msg_type == 'request' assert recs['x'][0] == 5.0 and not recs['flag'][0] assert gcomm.sim_started == 4 and gcomm.sim_pending == 1 inq.put(('killed', 4)) gcomm.process_message(timeout=0.1) assert promise.cancelled() and promise.done() assert gcomm.sim_started == 4 and gcomm.sim_pending == 0