def test_copy_path(self): stream = QueueStream() qstream = {'stream': stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream) watcher.start() time.sleep(3.) watcher.stop() data = [v for k, v in stream.get().items()][1] data = ''.join(data) self.assertTrue('XYZ' in data, data) finally: os.environ = old_environ sys.path[:] = old_paths
class SomeWatcher(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.stream = QueueStream() self.loop = self.watcher = None def run(self): qstream = {'stream': self.stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable self.loop = ioloop.IOLoop.instance() self.watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream, loop=self.loop) self.watcher.start() self.loop.start() finally: os.environ = old_environ sys.path[:] = old_paths def stop(self): if self.loop is not None: self.loop.stop() if self.watcher is not None: self.watcher.stop() self.join()
class SomeWatcher(threading.Thread): def __init__(self, **kw): threading.Thread.__init__(self) self.stream = QueueStream() self.loop = self.watcher = None self.kw = kw def run(self): qstream = {'stream': self.stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable self.loop = ioloop.IOLoop() self.watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream, loop=self.loop, **self.kw) self.watcher.start() self.loop.start() finally: os.environ = old_environ sys.path[:] = old_paths def stop(self): if self.loop is not None: self.loop.stop() if self.watcher is not None: self.watcher.stop() self.join()
def test_stopping_a_watcher_doesnt_spawn(self): watcher = Watcher("foo", "foobar", respawn=True, numprocesses=3) watcher.stopped = False watcher.spawn_processes = mock.MagicMock() watcher.loop = mock.MagicMock() watcher.send_signal = mock.MagicMock() # We have one running process and a dead one. watcher.processes = {1234: FakeProcess(1234, status=RUNNING), 1235: FakeProcess(1235, status=RUNNING)} # When we call manage_process(), the watcher should try to spawn a new # process since we aim to have 3 of them. watcher.manage_processes() self.assertTrue(watcher.spawn_processes.called) # Now, we want to stop everything. watcher.processes = {1234: FakeProcess(1234, status=RUNNING), 1235: FakeProcess(1235, status=RUNNING)} watcher.spawn_processes.reset_mock() watcher.stop() watcher.manage_processes() # And be sure we don't spawn new processes in the meantime. self.assertFalse(watcher.spawn_processes.called)
def test_stopping_a_watcher_doesnt_spawn(self): watcher = Watcher("foo", "foobar", respawn=True, numprocesses=3) watcher.stopped = False watcher.spawn_processes = mock.MagicMock() watcher.loop = mock.MagicMock() watcher.send_signal = mock.MagicMock() # We have one running process and a dead one. watcher.processes = { 1234: FakeProcess(1234, status=RUNNING), 1235: FakeProcess(1235, status=RUNNING) } # When we call manage_process(), the watcher should try to spawn a new # process since we aim to have 3 of them. watcher.manage_processes() self.assertTrue(watcher.spawn_processes.called) # Now, we want to stop everything. watcher.processes = { 1234: FakeProcess(1234, status=RUNNING), 1235: FakeProcess(1235, status=RUNNING) } watcher.spawn_processes.reset_mock() watcher.stop() watcher.manage_processes() # And be sure we don't spawn new processes in the meantime. self.assertFalse(watcher.spawn_processes.called)
class SomeWatcher(threading.Thread): def __init__(self, **kw): threading.Thread.__init__(self) self.stream = QueueStream() self.loop = self.watcher = None self.kw = kw def run(self): qstream = {"stream": self.stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ["XYZ"] os.environ = {"COCONUTS": "MIGRATE"} cmd = ( '%s -c "import sys; ' "sys.stdout.write(':'.join(sys.path)); " ' sys.stdout.flush()"' ) % sys.executable self.loop = ioloop.IOLoop() self.watcher = Watcher( "xx", cmd, copy_env=True, copy_path=True, stdout_stream=qstream, loop=self.loop, **self.kw ) self.watcher.start() self.loop.start() finally: os.environ = old_environ sys.path[:] = old_paths def stop(self): if self.loop is not None: self.loop.stop() if self.watcher is not None: self.watcher.stop() self.join()
class SomeWatcher(object): def __init__(self, loop=None, **kw): self.stream = QueueStream() self.watcher = None self.kw = kw if loop is None: self.loop = tornado.ioloop.IOLoop.instance() else: self.loop = loop @tornado.gen.coroutine def run(self): qstream = {'stream': self.stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable self.watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream, loop=self.loop, **self.kw) yield self.watcher.start() finally: os.environ = old_environ sys.path[:] = old_paths @tornado.gen.coroutine def stop(self): if self.watcher is not None: yield self.watcher.stop()
class SomeWatcher(object): def __init__(self, loop=None, **kw): self.stream = QueueStream() self.watcher = None self.kw = kw if loop is None: self.loop = tornado.ioloop.IOLoop.instance() else: self.loop = loop @tornado.gen.coroutine def run(self): qstream = {'stream': self.stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable self.watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream, loop=self.loop, **self.kw) yield self.watcher.start() finally: os.environ = old_environ sys.path[:] = old_paths @tornado.gen.coroutine def stop(self): if self.watcher is not None: yield self.watcher.stop()
def test_copy_path(self): stream = QueueStream() qstream = {'stream': stream} old_environ = os.environ old_paths = sys.path[:] try: sys.path = ['XYZ'] os.environ = {'COCONUTS': 'MIGRATE'} cmd = ('%s -c "import sys; ' 'sys.stdout.write(\':\'.join(sys.path)); ' ' sys.stdout.flush()"') % sys.executable watcher = Watcher('xx', cmd, copy_env=True, copy_path=True, stdout_stream=qstream) watcher.start() time.sleep(3.) watcher.stop() data = [v for k, v in stream.get().items()][1] data = ''.join(data) self.assertTrue('XYZ' in data, data) finally: os.environ = old_environ sys.path[:] = old_paths