def test_register_daemon_manual_terminate_thread(self): conf = self.get_base_config() conf.daemon = TestCounterDaemon conf_string = conf.dumps() counter = pyro.PyroDaemonProcess("test.counter", conf_string=conf_string, conf_sub_path=["daemon"]) counter.start() expected_counts = 127 try: services = pyro.PyroServices() counter_proxy = services.daemon("test.counter", 3) for _ in range(expected_counts): counter_proxy.notify() count = counter_proxy.get_notify_count() self.assertEqual(count, expected_counts) counter_proxy.terminate() is_terminated = counter_proxy.is_terminated() self.assertTrue(is_terminated) self.assertTrue(counter.is_alive()) finally: try: counter.terminate() counter.join() except: pass
def test_wait_for_daemon_too_late(self): conf = self.get_base_config() conf.daemon = TestCounterDaemon conf_string = conf.dumps() counter = pyro.PyroDaemonProcess("test.counter", conf_string=conf_string, conf_sub_path=["daemon"]) start_thread = threading.Timer(15, counter.start) try: services = pyro.PyroServices() start_thread.start() with self.assertRaises(Exception) as context: counter_proxy = services.daemon("test.counter", 10) finally: try: start_thread.cancel() start_thread.join() except: pass try: counter.terminate() counter.join() except: pass
def test_wait_for_daemon_success(self): conf = self.get_base_config() conf.daemon = TestCounterDaemon conf_string = conf.dumps() counter = pyro.PyroDaemonProcess("test.counter", conf_string=conf_string, conf_sub_path=["daemon"]) start_thread = threading.Timer(5, counter.start) try: services = pyro.PyroServices() start_thread.start() counter_proxy = services.daemon("test.counter", 10) counter_proxy.notify() count = counter_proxy.get_notify_count() self.assertEqual(count, 1) finally: try: start_thread.cancel() start_thread.join() except: pass try: counter.terminate() counter.join() except: pass
def base_deployment(self, daemon_bookkeeping): daemon_group = "system" conf = self.get_base_config() conf.system["test.counter1"] = TestCounterDaemon conf.system["test.counter2"] = TestCounterDaemon conf.system.deploy_daemons = ["test.counter1", "test.counter2"] conf.set_immutable() deploy = pyro.PyroDaemonsDeployment( conf.system, daemon_group=daemon_group, daemon_bookkeeping=daemon_bookkeeping) deploy.deploy() # time.sleep(60) expected_counts_1 = 13 expected_counts_2 = 27 try: services = pyro.PyroServices() counter_1_proxy = services.daemon("test.counter1") counter_2_proxy = services.daemon("test.counter2") for _ in range(expected_counts_1): counter_1_proxy.notify() for _ in range(expected_counts_2): counter_2_proxy.notify() count_1 = counter_1_proxy.get_notify_count() count_2 = counter_2_proxy.get_notify_count() self.assertEqual(count_1, expected_counts_1) self.assertEqual(count_2, expected_counts_2) self.assertTrue(deploy.is_all_running()) deploy.terminate_daemon("test.counter1") time.sleep(2) self.assertFalse(deploy.is_all_running()) finally: deploy.terminate_all() deploy.join() self.assertDaemonCleanup("test.counter1", daemon_group=daemon_group, daemon_bookkeeping=daemon_bookkeeping) self.assertDaemonCleanup("test.counter2", daemon_group=daemon_group, daemon_bookkeeping=daemon_bookkeeping) self.assertDaemonCleanup("", daemon_group=daemon_group, daemon_bookkeeping=daemon_bookkeeping)
def test_daemon_async(self): counter_object = TestCounterDaemon() counter = pyro.PyroDaemonThread("test.counter", counter_object) counter.start() compute_time = 5 compute_result = 'good' try: services = pyro.PyroServices() counter_proxy = services.async_daemon("test.counter", 3) future = counter_proxy.long_operation(compute_time, compute_result) self.assertEqual(future.ready, False) finish = future.wait(compute_time + 1) self.assertTrue(finish) self.assertEqual(future.value, compute_result) finally: counter.terminate() counter.join()
def test_daemon_thread_join(self): sleeper_object = TestSleeperDaemon(sleep_time=5) sleeper = pyro.PyroDaemonThread("test.sleeper", sleeper_object) sleeper.start() try: services = pyro.PyroServices() sleeper_proxy = services.async_daemon("test.sleeper", 3) sleeper_proxy.terminate() t1 = time.time() sleeper.join() t2 = time.time() self.assertGreaterEqual(t2 - t1, 4) self.assertTrue(sleeper.is_terminated()) self.assertFalse(sleeper.is_pyro_thread_alive()) self.assertFalse(sleeper.is_alive()) finally: sleeper.terminate() sleeper.join()
def test_pyro_daemon_thread_with_non_daemon_class(self): counter_object = TestCounter() counter = pyro.PyroDaemonThread("test.counter", counter_object) counter.start() expected_counts = 127 try: services = pyro.PyroServices() counter_proxy = services.daemon("test.counter", 3) for _ in range(expected_counts): counter_proxy.add() count = counter_proxy.count() self.assertEqual(count, expected_counts) finally: try: counter.terminate() counter.join() except: pass
def test_daemon_thread(self): counter_object = TestCounterDaemon() counter = pyro.PyroDaemonThread("test.counter", counter_object) counter.start() expected_counts = 127 try: services = pyro.PyroServices() counter_proxy = services.daemon("test.counter", 3) for _ in range(expected_counts): counter_proxy.notify() count = counter_proxy.get_notify_count() self.assertEqual(count, expected_counts) self.assertFalse(counter.is_terminated()) self.assertTrue(counter.is_alive()) finally: counter.terminate() counter.join()
def test_daemon_process_conf(self): conf = self.get_base_config() conf.daemon = TestCounterDaemon counter = pyro.PyroDaemonProcess("test.counter", conf.daemon) counter.start() expected_counts = 127 try: services = pyro.PyroServices() counter_proxy = services.daemon("test.counter", 3) for _ in range(expected_counts): counter_proxy.notify() count = counter_proxy.get_notify_count() self.assertEqual(count, expected_counts) finally: try: counter.terminate() counter.join() except: pass
def test_wait_for_daemon_fail(self): services = pyro.PyroServices() with self.assertRaises(Exception) as context: services.daemon("test.counter", 3)