Exemplo n.º 1
0
    def test_push_over_limit(self):
        with SchedulingExecutor(max_threads=2) as executor:
            procs = (WaitForSignalProcess(), WaitForSignalProcess(),
                     WaitForSignalProcess())

            # Play first two
            executor.play(procs[0])
            executor.play(procs[1])

            # Get first two to the waiting state
            self.assertTrue(
                wait_until((procs[0], procs[1]),
                           ProcessState.WAITING,
                           timeout=2.))

            self.assertTrue(procs[0].is_playing())
            self.assertTrue(procs[1].is_playing())
            self.assertFalse(procs[2].is_playing())

            # Now play third
            executor.play(procs[2])

            # Get third to the waiting state
            self.assertTrue(
                wait_until(procs[2], ProcessState.WAITING, timeout=2.))

            # Now that it's waiting p2 should be pulled and the other two should be playing
            self.assertTrue(procs[2].wait(timeout=2.))

            # Check the final expected state
            # WARNING: The others could not be playing *yet* because it is too early
            time.sleep(0.1)
            self.assertTrue(procs[1].is_playing())
            self.assertFalse(procs[2].is_playing())
            self.assertTrue(procs[0].is_playing())
Exemplo n.º 2
0
    def test_queueing(self):
        with SchedulingExecutor(max_threads=2) as executor:
            procs = (WaitForSignalProcess(), WaitForSignalProcess(),
                     WaitForSignalProcess(), WaitForSignalProcess())

            futs = []
            for proc in procs:
                futs.append(executor.play(proc))

            for proc in procs:
                proc.continue_()

            # Make sure they all finished
            for fut in futs:
                self.assertTrue(fut.wait(timeout=1.))
Exemplo n.º 3
0
 def test_wait_continue(self):
     p = WaitForSignalProcess.new()
     self.executor.play(p)
     self.assertTrue(wait_until(p, ProcessState.WAITING, timeout=1.))
     p.continue_()
     self.assertTrue(p.wait(timeout=2.))
     self.assertTrue(p.has_finished())
Exemplo n.º 4
0
    def test_play_all(self):
        procs = []

        # Launch a bunch of processes
        for i in range(0, 9):
            procs.append(WaitForSignalProcess.new())
            self.manager.start(procs[-1])

        wait_until(procs, ProcessState.WAITING, timeout=1)

        # Check they are all in state we expect
        for p in procs:
            self.assertTrue(p.is_playing(), "state '{}'".format(p.state))

        # Now try and pause them all
        self.manager.pause_all()

        # Check they are all in state we expect
        for p in procs:
            self.assertEqual(p.state, ProcessState.WAITING)
            self.assertFalse(p.is_playing())

        self.manager.play_all()

        for p in procs:
            p.continue_()
        wait_until_stopped(procs)

        for p in procs:
            self.assertEqual(p.state, ProcessState.STOPPED)
            self.manager.shutdown()
            self.assertFalse(p.is_playing())
Exemplo n.º 5
0
 def test_play_pause_abort(self):
     procs = []
     for i in range(0, 10):
         procs.append(WaitForSignalProcess.new())
         self.manager.start(procs[-1])
     self.assertTrue(wait_until(procs, ProcessState.WAITING))
     self.assertTrue(self.manager.pause_all(timeout=2))
     self.assertTrue(self.manager.abort_all(timeout=2))
Exemplo n.º 6
0
    def test_get_processes(self):
        p = WaitForSignalProcess.new()
        self.executor.play(p)

        procs = self.executor.get_processes()
        self.assertEqual(len(procs), 1)
        self.assertIs(procs[0], p)
        self.assertTrue(p.abort(timeout=2.), "Failed to abort process")
Exemplo n.º 7
0
    def test_future_abort(self):
        p = WaitForSignalProcess.new()

        with WaitRegion(WaitOnState(p, ProcessState.RUNNING), timeout=2):
            future = self.manager.start(p)

        self.assertTrue(p.is_playing())
        self.assertTrue(future.abort(timeout=2))
        self.assertTrue(p.has_aborted())
Exemplo n.º 8
0
 def test_abort(self):
     """
     Test aborting a process through the process manager
     """
     self.assertEqual(self.executor.get_num_processes(), 0)
     proc = WaitForSignalProcess.new()
     future = self.executor.play(proc)
     self.assertTrue(future.abort(timeout=2.))
     self.assertEqual(self.executor.get_num_processes(), 0)
Exemplo n.º 9
0
    def test_waiting_state(self):
        tp = PythonThreadPoolExecutor(max_workers=1)

        p = WaitForSignalProcess.new()
        waiton = WaitOnProcessState(p, ProcessState.WAITING)
        future = tp.submit(waiton.wait)

        self.executor.play(p)
        self.assertTrue(future.result(timeout=2.))
        self.assertTrue(p.abort(timeout=2.))
Exemplo n.º 10
0
    def test_pause(self):
        # Create the process and wait until it is waiting
        p = WaitForSignalProcess.new()
        self.controller.insert_and_play(p)
        wait_until(p, ProcessState.WAITING)
        self.assertTrue(p.is_playing())

        # Send a message asking the process to pause
        self.assertIsNotNone(self.publisher.pause(p.pid, timeout=5.))
        self.assertFalse(p.is_playing())
Exemplo n.º 11
0
 def test_abort_future(self):
     """
     Test aborting a process through the future
     """
     self.assertEqual(self.executor.get_num_processes(), 0)
     proc = WaitForSignalProcess.new()
     future = self.executor.play(proc)
     wait_until(proc, ProcessState.WAITING)
     self.assertTrue(future.abort(timeout=2.))
     self.assertEqual(self.executor.get_num_processes(), 0)
Exemplo n.º 12
0
    def test_on_waiting_process(self):
        proc = WaitForSignalProcess.new()
        self.pickle_persistence.persist_process(proc)
        save_path = self.pickle_persistence.get_running_path(proc.pid)

        future = self.procman.play(proc)

        # Check the file exists
        self.assertTrue(os.path.isfile(save_path))

        self.assertTrue(future.abort(timeout=2.))
Exemplo n.º 13
0
    def test_future_abort(self):
        p = WaitForSignalProcess.new()
        future = self.executor.play(p)

        # Wait
        self.assertTrue(wait_until(p, ProcessState.WAITING))
        self.assertTrue(p.is_playing())

        # Abort
        self.assertTrue(future.abort(timeout=3.))
        self.assertTrue(p.has_aborted())
Exemplo n.º 14
0
    def test_play_pause_abort(self):
        num_procs = 10
        procs = []
        for i in range(0, num_procs):
            procs.append(WaitForSignalProcess.new())
            self.executor.play(procs[-1])

        # Wait
        self.assertTrue(wait_until(procs, ProcessState.WAITING))

        self.assertEqual(self.executor.pause_all(timeout=3.), num_procs)
Exemplo n.º 15
0
 def test_abort_interrupt(self):
     """
     Test aborting a process through the process manager
     """
     self.assertEqual(self.executor.get_num_processes(), 0)
     proc = WaitForSignalProcess.new()
     # Start a process and make sure it is waiting
     future = self.executor.play(proc)
     wait_until(proc, ProcessState.WAITING)
     # Then interrupt by aborting
     self.assertTrue(future.abort(timeout=2.))
     self.assertEqual(self.executor.get_num_processes(), 0)
Exemplo n.º 16
0
    def test_persist_twice(self):
        proc = WaitForSignalProcess.new()
        self.pickle_persistence.persist_process(proc)
        future = self.procman.play(proc)

        # Try persisting the process again using another persistence manager
        try:
            PicklePersistence(
                running_directory=self.store_dir).persist_process(proc)
        except LockError:
            pass

        self.assertTrue(proc.abort(timeout=1.))
Exemplo n.º 17
0
    def test_on_waiting_process(self):
        proc = WaitForSignalProcess.new()
        self.pickle_persistence.persist_process(proc)
        save_path = self.pickle_persistence.get_running_path(proc.pid)

        t = threading.Thread(target=proc.play)
        t.start()

        # Check the file exists
        self.assertTrue(os.path.isfile(save_path))

        proc.abort()
        t.join()
Exemplo n.º 18
0
    def test_wait_continue(self):
        p = WaitForSignalProcess.new()
        t = threading.Thread(target=p.play)
        t.start()
        self.assertTrue(wait_until(p, ProcessState.WAITING, 5))
        self.assertEqual(p.state, ProcessState.WAITING)

        self.assertTrue(p.is_playing())
        p.continue_()

        self.assertTrue(wait_until(p, ProcessState.STOPPED, 5))

        self.assertEqual(p.state, ProcessState.STOPPED)
Exemplo n.º 19
0
    def test_wait_pause_play(self):
        p = WaitForSignalProcess.new()
        self.executor.play(p)

        # Wait
        self.assertTrue(wait_until(p, ProcessState.WAITING, timeout=2.))

        # Pause
        self.assertTrue(p.pause(timeout=1.))

        # Play
        self.executor.play(p)
        self.assertTrue(wait_until(p, ProcessState.WAITING, timeout=2.))
Exemplo n.º 20
0
    def test_simple_push_over_limit(self):
        with SchedulingExecutor(max_threads=1) as executor:
            p1 = WaitForSignalProcess()
            p2 = WaitForSignalProcess()

            f1 = executor.play(p1)
            self.assertTrue(wait_until(p1, ProcessState.WAITING, timeout=2.))

            # This should push of P1
            f2 = executor.play(p2)
            self.assertTrue(p1.wait(timeout=1.))
            self.assertTrue(wait_until(p2, ProcessState.WAITING, timeout=2.))

            # Now P2 should be paused and P1 running again
            self.assertTrue(p2.wait(timeout=1.))

            # Finish p1
            p1.continue_()
            self.assertTrue(f1.wait(timeout=1.))

            # Now P2 should be running again
            p2.continue_()
            self.assertTrue(f2.wait(timeout=1.))
Exemplo n.º 21
0
    def test_abort(self):
        # Create the process and wait until it is waiting
        p = WaitForSignalProcess.new()
        self.controller.insert_and_play(p)
        self.assertTrue(wait_until(p, ProcessState.WAITING, timeout=2.))
        self.assertTrue(p.is_playing())

        # Send a message asking the process to abort
        self.assertIsNotNone(
            self.publisher.abort(p.pid, msg='Farewell', timeout=5.))
        self.assertTrue(p.wait(timeout=2.), "Process failed to stop running")
        self.assertFalse(p.is_playing())
        self.assertTrue(p.has_aborted())
        self.assertEqual(p.get_abort_msg(), 'Farewell')
Exemplo n.º 22
0
    def test_request(self):
        procs = []
        for i in range(0, 10):
            procs.append(WaitForSignalProcess.new())
            self.manager.start(procs[-1])

        response = self._send_request_poll_response(0.2)
        self.assertEqual(len(response), len(procs))
        self.assertSetEqual(set(response.keys()), {p.pid for p in procs})

        self.assertTrue(self.manager.abort_all(timeout=10),
                        "Failed to abort processes within timeout")

        response = self._send_request_poll_response(0.2)
        self.assertIsNone(response)
Exemplo n.º 23
0
    def test_pause(self):
        # Create the process and wait until it is waiting
        p = WaitForSignalProcess.new()
        self.manager.start(p)
        wait_until(p, ProcessState.WAITING)
        self.assertTrue(p.is_playing())

        # Send a message asking the process to pause
        self.channel.basic_publish(
            exchange=self.exchange, routing_key='',
            body=action_encode({'pid': p.pid, 'intent': 'pause'}))
        self.controller.poll(time_limit=1)

        self.assertFalse(p.is_playing())
        self.assertTrue(self.manager.abort(p.pid, timeout=10))
Exemplo n.º 24
0
    def test_future_pause_play(self):
        p = WaitForSignalProcess.new()
        future = self.executor.play(p)

        # Wait
        self.assertTrue(wait_until(p, ProcessState.WAITING))
        self.assertTrue(p.is_playing())

        # Pause
        self.assertTrue(future.pause(timeout=3.))
        self.assertFalse(p.is_playing())

        # Play
        future.play()
        p.continue_()
        self.assertTrue(future.wait(timeout=1.))
Exemplo n.º 25
0
    def test_future_pause_play(self):
        p = WaitForSignalProcess.new()

        # Run the process
        with WaitRegion(WaitOnState(p, ProcessState.WAITING), timeout=2):
            future = self.manager.start(p)
        self.assertTrue(p.is_playing())

        # Pause it
        self.assertTrue(future.pause(timeout=2))
        self.assertFalse(p.is_playing())

        # Play it
        future.play()
        time.sleep(1)
        self.assertTrue(p.is_playing())
Exemplo n.º 26
0
    def test_abort(self):
        # Create the process and wait until it is waiting
        p = WaitForSignalProcess.new()
        self.manager.start(p)
        wait_until(p, ProcessState.WAITING)
        self.assertTrue(p.is_playing())

        # Send a message asking the process to abort
        self.channel.basic_publish(
            exchange=self.exchange, routing_key='',
            body=action_encode({'pid': p.pid, 'intent': 'abort'}))
        self.controller.poll(time_limit=1)

        self.assertTrue(wait_until(p, ProcessState.STOPPED, 10),
                        "Process did not stop before timeout")
        self.assertTrue(p.has_aborted())
        self.manager.shutdown()
Exemplo n.º 27
0
    def test_state_messages(self):
        p = WaitForSignalProcess.new()

        # Create ones for each state
        waits = {state: WaitOnState(p, state)
                 for state in [
                     ProcessState.RUNNING,
                     ProcessState.WAITING,
                     ProcessState.STOPPED]}

        for wait in waits.itervalues():
            self.assertFalse(wait.is_done())

        self.manager.start(p)
        self.assertTrue(waits[ProcessState.RUNNING].wait(1))
        self.assertTrue(waits[ProcessState.WAITING].wait(1))
        self.assertFalse(waits[ProcessState.STOPPED].wait(1))
        p.continue_()
        self.assertTrue(waits[ProcessState.STOPPED].wait(1))
Exemplo n.º 28
0
    def test_wait_pause_play_continue(self):
        p = WaitForSignalProcess.new()

        fut = self.executor.play(p)

        # Wait
        self.assertTrue(wait_until(p, ProcessState.WAITING, 1.))
        self.assertTrue(p.is_playing())

        # Pause
        self.assertTrue(p.pause(timeout=1.))
        self.assertFalse(p.is_playing())

        # Play
        fut.play()
        self.assertEqual(p.state, ProcessState.WAITING)

        # Continue
        p.continue_()
        self.assertTrue(wait_until(p, ProcessState.STOPPED), 1.)
Exemplo n.º 29
0
    def test_wait_pause_continue_play(self):
        p = WaitForSignalProcess.new()

        # Play the process and wait until it is waiting
        self.executor.play(p)

        # Wait
        self.assertTrue(wait_until(p, ProcessState.WAITING, 1.))

        # Pause
        self.assertTrue(p.pause(timeout=1.))

        # Continue
        p.continue_()

        # Play
        p.play()

        self.assertTrue(p.wait(timeout=1.))
        self.assertEqual(p.state, ProcessState.STOPPED)
Exemplo n.º 30
0
    def test_request(self):
        procs = []
        for i in range(0, 10):
            p = WaitForSignalProcess.new()
            procs.append(p)
            self.controller.insert_and_play(p)

        responses = self._send_request_poll_response(0.2)
        self.assertEqual(len(responses), 1)
        procs_info = responses[0][status.PROCS_KEY]
        self.assertEqual(len(procs_info), len(procs))
        self.assertSetEqual(set(procs_info.keys()), {p.pid for p in procs})

        self.controller.remove_all(timeout=10.)
        self.assertEqual(self.controller.get_num_processes(), 0,
                         "Failed to abort all processes")

        responses = self._send_request_poll_response(0.2)
        self.assertEqual(len(responses), 1)
        self.assertEqual(len(responses[0][status.PROCS_KEY]), 0)