Exemplo n.º 1
0
 def test_wait_returncode(self):
     # we get the returncode
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'sys.exit(42)'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     ret_code, stdout, stderr = cmd.wait()
     assert ret_code == 42
Exemplo n.º 2
0
 def test_wait_stdout(self):
     # we can get the stdout output when wait()-ing
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'print("Hello from myscript")'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     ret_code, stdout, stderr = cmd.wait()
     assert stdout == b'Hello from myscript\n'
Exemplo n.º 3
0
 def test_wait_stdout(self):
     # we can get the stdout output when wait()-ing
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'print("Hello from myscript")'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     ret_code, stdout, stderr = cmd.wait()
     assert stdout == b'Hello from myscript\n'
Exemplo n.º 4
0
 def test_wait_returncode(self):
     # we get the returncode
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'sys.exit(42)'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     ret_code, stdout, stderr = cmd.wait()
     assert ret_code == 42
Exemplo n.º 5
0
 def test_args(self):
     # we can pass in arguments
     path = os.path.join(self.path_dir, 'myscript')
     stamp_file = os.path.join(self.path_dir, 'i_was_here')
     pysrc = 'open("%s", "w").write(sys.argv[1])' % stamp_file
     create_python_script(path, pysrc)
     cmd = BackgroundCommand([path, 'myarg'])
     cmd.run()
     cmd.wait()
     assert open(stamp_file, 'r').read() == 'myarg'
Exemplo n.º 6
0
 def test_startable(self):
     # we can start executables
     path = os.path.join(self.path_dir, 'myscript')
     stamp_file = os.path.join(self.path_dir, 'i_was_here')
     pysrc = 'open("%s", "w").write("I was here")' % stamp_file
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     cmd.wait()
     assert os.path.isfile(stamp_file)
Exemplo n.º 7
0
    def test_callback(self):
        # a passed-in callback function is really called
        global callback_counter
        callback_counter = 0

        def mycallback(*args, **kw):
            global callback_counter
            callback_counter += 1
        path = os.path.join(self.path_dir, 'myscript')
        pysrc = 'time.sleep(0.1)'
        create_python_script(path, pysrc)
        cmd = BackgroundCommand(path, callback=mycallback)
        cmd.start()
        while cmd.is_alive():
            pass
        assert callback_counter > 0
        assert cmd.is_killed is False
Exemplo n.º 8
0
    def test_callback(self):
        # a passed-in callback function is really called
        global callback_counter
        callback_counter = 0

        def mycallback(*args, **kw):
            global callback_counter
            callback_counter += 1

        path = os.path.join(self.path_dir, 'myscript')
        pysrc = 'time.sleep(0.1)'
        create_python_script(path, pysrc)
        cmd = BackgroundCommand(path, callback=mycallback)
        cmd.start()
        while cmd.is_alive():
            pass
        assert callback_counter > 0
        assert cmd.is_killed is False
Exemplo n.º 9
0
    def test_callback_retcode(self):
        # we can get return codes from callbacks
        global callback_data

        callback_data = None

        def mycallback(*args, **kw):
            global callback_data
            callback_data = (args, kw)

        path = os.path.join(self.path_dir, 'myscript')
        pysrc = ('print("stdout output")\n'
                 'print("stderr output", file=sys.stderr)\n'
                 'sys.exit(42)')
        create_python_script(path, pysrc)
        cmd = BackgroundCommand(path, callback=mycallback)
        cmd.start()
        while cmd.is_alive():
            pass
        result_cmd = callback_data[0][0]
        assert result_cmd is cmd
        assert result_cmd.returncode == 42
        assert result_cmd.stdout_data == b'stdout output\n'
        assert result_cmd.stderr_data == b'stderr output\n'
Exemplo n.º 10
0
    def test_callback_retcode(self):
        # we can get return codes from callbacks
        global callback_data

        callback_data = None

        def mycallback(*args, **kw):
            global callback_data
            callback_data = (args, kw)
        path = os.path.join(self.path_dir, 'myscript')
        pysrc = (
            'print("stdout output")\n'
            'print("stderr output", file=sys.stderr)\n'
            'sys.exit(42)')
        create_python_script(path, pysrc)
        cmd = BackgroundCommand(path, callback=mycallback)
        cmd.start()
        while cmd.is_alive():
            pass
        result_cmd = callback_data[0][0]
        assert result_cmd is cmd
        assert result_cmd.returncode == 42
        assert result_cmd.stdout_data == b'stdout output\n'
        assert result_cmd.stderr_data == b'stderr output\n'
Exemplo n.º 11
0
 def test_args(self):
     # we can pass in arguments
     path = os.path.join(self.path_dir, 'myscript')
     stamp_file = os.path.join(self.path_dir, 'i_was_here')
     pysrc = 'open("%s", "w").write(sys.argv[1])' % stamp_file
     create_python_script(path, pysrc)
     cmd = BackgroundCommand([path, 'myarg'])
     cmd.run()
     cmd.wait()
     assert open(stamp_file, 'r').read() == 'myarg'
Exemplo n.º 12
0
 def test_startable(self):
     # we can start executables
     path = os.path.join(self.path_dir, 'myscript')
     stamp_file = os.path.join(self.path_dir, 'i_was_here')
     pysrc = 'open("%s", "w").write("I was here")' % stamp_file
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path)
     cmd.run()
     cmd.wait()
     assert os.path.isfile(stamp_file)
Exemplo n.º 13
0
 def test_timeout(self):
     # we can set a timeout
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'time.sleep(10)'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path, timeout=0.1)
     t_stamp1 = time.time()
     cmd.start()
     cmd.wait()
     t_stamp2 = time.time()
     assert t_stamp2 - t_stamp1 < 5
     assert not cmd.is_alive()
     assert cmd.returncode == -9
     assert cmd.stderr_data == b''
     assert cmd.stdout_data == b''
     # timeouts kill processes
     assert cmd.is_killed is True
Exemplo n.º 14
0
 def test_timeout(self):
     # we can set a timeout
     path = os.path.join(self.path_dir, 'myscript')
     pysrc = 'time.sleep(10)'
     create_python_script(path, pysrc)
     cmd = BackgroundCommand(path, timeout=0.1)
     t_stamp1 = time.time()
     cmd.start()
     cmd.wait()
     t_stamp2 = time.time()
     assert t_stamp2 - t_stamp1 < 5
     assert not cmd.is_alive()
     assert cmd.returncode == -9
     assert cmd.stderr_data == b''
     assert cmd.stdout_data == b''
     # timeouts kill processes
     assert cmd.is_killed is True