def test_shutdown(self): s = ts.TaskServer(self.dbi) t = threading.Thread(target=s.start) t.start() s.shutdown() t.join() self.assertFalse(s.is_running)
def test_many_stills(self): dbi = FakeDataBaseInterface() try: os.mkdir('still0') os.mkdir('still1') except (OSError): pass ts0 = ts.TaskServer(dbi, data_dir='still0', port=4441) ts1 = ts.TaskServer(dbi, data_dir='still1', port=4442) ts0_thd = threading.Thread(target=ts0.start) ts1_thd = threading.Thread(target=ts1.start) tc = { 0: ts.TaskClient(dbi, 'localhost', port=4441), 1: ts.TaskClient(dbi, 'localhost', port=4442) } s = ts.Scheduler(tc, actions_per_still=2, blocksize=2) s_thd = threading.Thread(target=s.start, args=(dbi, ), kwargs={ 'ActionClass': ts.Action, 'action_args': (tc, 30), 'sleeptime': 0 }) ts0_thd.start() ts1_thd.start() s_thd.start() try: def all_done(): for f in dbi.files: if dbi.get_obs_status(f) != 'COMPLETE': return False return True while not all_done(): time.sleep(.1) finally: s.quit() ts0.shutdown() ts1.shutdown() s_thd.join() ts0_thd.join() ts1_thd.join() for i in dbi.files: self.assertEqual(dbi.get_obs_status(i), 'COMPLETE')
def test_tx(self): self.pkt = '' class SleepHandler(ts.TaskHandler): def handle(me): self.pkt = me.get_pkt() s = ts.TaskServer(self.dbi, handler=SleepHandler) thd = threading.Thread(target=s.start) thd.start() try: tc = ts.TaskClient(self.dbi, 'localhost') tc.tx('UV', 1) finally: s.shutdown() thd.join() self.assertEqual(self.pkt, ('UV',1,'localhost',['test.uv', 'localhost:./test.uv']))
def test_basics(self): s = ts.TaskServer(self.dbi) t = SleepTask('UV',1,'still',[],self.dbi) s.append_task(t) self.assertEqual(len(s.active_tasks), 1) t.run() s.kill(t.process.pid) while t.poll() is None: time.sleep(.01) self.assertEqual(t.poll(), -9) thd = threading.Thread(target=s.finalize_tasks, args=(.1,)) s.is_running = True thd.start() s.is_running = False thd.join() self.assertEqual(len(s.active_tasks),0)
def test_dbi(self): self.var = 0 for f in self.dbi.files: self.dbi.files[f] = 'UV_POT' class NullHandler(ts.TaskHandler): def handle(me): task, obs, still, args = me.get_pkt() t = NullTask(task, obs, still, args, self.dbi) me.server.append_task(t) t.run() self.var += 1 s = ts.TaskServer(self.dbi, handler=NullHandler) thd = threading.Thread(target=s.start) thd.start() try: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(ts.to_pkt('UV',1,'still',[]), ('localhost', ts.STILL_PORT)) while self.var != 1: time.sleep(.6) self.assertEqual(self.var, 1) self.assertEqual(self.dbi.get_obs_status(1), 'UV') finally: s.shutdown() thd.join()
def test_send_task(self): self.var = 0 class SleepHandler(ts.TaskHandler): def handle(me): self.var += 1 t = SleepTask('UV',1,'still',[],self.dbi) t.run() me.server.append_task(t) s = ts.TaskServer(self.dbi, handler=SleepHandler) thd = threading.Thread(target=s.start) thd.start() try: self.assertEqual(len(s.active_tasks), 0) self.assertEqual(self.var, 0) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto('test', ('localhost', ts.STILL_PORT)) while self.var != 1: time.sleep(.1) self.assertEqual(self.var, 1) self.assertEqual(len(s.active_tasks), 1) finally: s.shutdown() thd.join()
def setUp(self): self.dbi = FakeDataBaseInterface() self.ts = ts.TaskServer(self.dbi) self.ts_thread = threading.Thread(target=self.ts.start) self.ts_thread.start()