Ejemplo n.º 1
0
    def test_9(self):
        for _ in self.poolAndThread:
            with AsyncController(_) as ac:

                def f():
                    raise TypeError

                em = Emitter()
                # Fun with exceptions: if an exception is raised while handling
                # a signal, it doesn't show up with normal try/catch semantics.
                # Instead, ``sys.excepthook`` must be overridden to see it.
                # ``WaitForSignal`` does this in ``__exit__``, so use it for the
                # convenience. Put another way, removing ``WaitForSignal`` and
                # adding a time.sleep(1.0) produces test failures, since the
                # exceptions raised are not caught by standard Python mechanisms
                # (here, ``self.assertRaises``).
                #
                # **However**, ``WaitForSignal`` doesn't do this in the body of
                # the ``with`` statement, so 'Sync' raises an exception but this
                # is discarded. For simplicity, skip this test case for now.
                with self.assertRaises(TypeError), WaitForSignal(
                        em.bing, 1000, printExcTraceback=False):
                    ac.start(em.g, f)

                # Make sure that the exception is still raised even if g doesn't
                # check for it.
                with self.assertRaises(TypeError), WaitForSignal(
                        em.bing, 1000, printExcTraceback=False):
                    ac.start(lambda result: None, f)

                # Make sure that the exception is still raised even there is no
                # g to check for it.
                with self.assertRaises(TypeError), WaitForSignal(
                        em.bing, 1000, printExcTraceback=False):
                    ac.start(None, f)
Ejemplo n.º 2
0
 def test_14(self):
     for _ in self.poolAndThread:
         with AsyncController(_) as ac:
             def f(assertEqual, priority):
                 assertEqual(QThread.currentThread().priority(), priority)
             em = Emitter()
             ac.defaultPriority = QThread.LowPriority
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, f, self.assertEqual, QThread.LowestPriority,
                          _futurePriority=QThread.LowestPriority)
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, f, self.assertEqual, QThread.LowPriority)
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, f, self.assertEqual, QThread.HighestPriority,
                          _futurePriority=QThread.HighestPriority)
Ejemplo n.º 3
0
 def _WaitForHtmlReady(self, timeout=8000, numEmittedExpected=3):
     # Expect two calls to loadFinished: one
     # produced by _clear(), then the second when the page is actually ready.
     lf = self._widget().webEngineView.page().loadFinished
     return WaitForSignal(lf,
                          timeout,
                          numEmittedExpected=numEmittedExpected)
Ejemplo n.º 4
0
    def test_15(self):
        for _ in self.singleThreadOnly:
            rl = RunLatest(_)

            # Start a job, keeping it running.
            q1a = Queue()
            q1b = Queue()

            def f1():
                q1b.put(None)
                q1a.get()

            em1 = Emitter()
            future1 = rl.start(em1.g, f1)
            q1b.get()
            self.assertEqual(future1.state, Future.STATE_RUNNING)

            # Start two more. The first should not run; if it does, it raises
            # an exception.
            def f2():
                raise TypeError

            rl.start(None, f2)
            em3 = Emitter()
            rl.start(em3.g, lambda: None)

            with WaitForSignal(em3.bing, 1000):
                q1a.put(None)

            rl.terminate()
Ejemplo n.º 5
0
    def test_16(self):
        for _ in self.singleThreadOnly:
            rl = RunLatest(_, True)

            # Start a job.
            q1a = Queue()
            q1b = Queue()

            def f1():
                q1b.put(None)
                q1a.get()

            em1 = Emitter('em1 should never be called by {}'.format(_),
                          self.assertEqual)
            future1 = rl.start(em1.g, f1)
            q1b.get()
            self.assertEqual(future1.state, Future.STATE_RUNNING)

            # Start another job, canceling the previous job while it's running.
            em2 = Emitter()
            rl.start(em2.g, lambda: None)
            with WaitForSignal(em2.bing, 1000):
                q1a.put(None)

            rl.terminate()
Ejemplo n.º 6
0
 def test_5(self):
     for _ in self.poolAndThread:
         with AsyncController(_) as ac:
             def f(currentThread):
                 self.assertNotEqual(currentThread, QThread.currentThread())
             em = Emitter()
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, f, QThread.currentThread())
Ejemplo n.º 7
0
 def test_10(self):
     with AsyncController('QThread') as ac:
         em1 = Emitter()
         def f1():
             ac.start(em1.g, lambda: QThread.currentThread())
         with WaitForSignal(em1.bing, 1000):
             ac.start(None, f1)
         self.assertEquals(em1.thread, em1.result)
Ejemplo n.º 8
0
 def test_6(self):
     # Don't test with one pooled thread -- this test expects at least two
     # threads.
     with AsyncController(2) as ac:
         q = Queue()
         def f():
             q.get()
             return QThread.currentThread()
         em1 = Emitter()
         em2 = Emitter()
         ac.start(em1.g, f)
         ac.start(em2.g, f)
         with WaitForSignal(em1.bing, 1000), WaitForSignal(em2.bing, 1000):
             q.put(None)
             q.put(None)
         s = set([em1.result, em2.result, QThread.currentThread()])
         self.assertEquals(len(s), 3)
Ejemplo n.º 9
0
 def test_4(self):
     for _ in self.syncPoolAndThread:
         with AsyncController(_) as ac:
             def f(a, b, c=2, d=4):
                 self.assertEqual(a, 2)
                 self.assertEqual(b, 3)
                 self.assertEqual(c, 4)
                 self.assertEqual(d, 5)
             em = Emitter()
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, f, 2, 3, d=5, c=4)
Ejemplo n.º 10
0
 def test_7(self):
     for _ in ('Sync', 'QThread'):
         with AsyncController(_) as ac:
             em1 = Emitter(15, self.assertEqual)
             em2 = Emitter(16, self.assertEqual)
             em3 = Emitter(17, self.assertEqual)
             ac.start(em1.g, lambda: 15)
             ac.start(em2.g, lambda: 16)
             future3 = ac._wrap(em3.g, lambda: 17)
             with WaitForSignal(em3.bing, 1000):
                 ac._start(future3)
Ejemplo n.º 11
0
 def test_17(self):
     for _ in self.singleThreadOnly:
         with AsyncController(_) as ac:
             f = ac._wrap(self.fail, lambda: None)
             f.cancel()
             ac._start(f)
             em = Emitter()
             # Make sure the canceled job was processed by waiting until the
             # next job finishes.
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, lambda: None)
Ejemplo n.º 12
0
 def test_1(self):
     for _ in self.syncPoolAndThread:
         with AsyncController(_) as ac:
             # gotHere must be a list in order to f to change it in a way
             # that is visible outside of f.
             gotHere = [False]
             def f():
                 gotHere[0] = True
             future = ac._wrap(None, f)
             with WaitForSignal(future._signalInvoker.doneSignal, 1000):
                 ac._start(future)
             self.assertTrue(gotHere[0])
Ejemplo n.º 13
0
    def test_11(self):
        # Don't test with one pooled thread -- this test expects at least two
        # threads.
        with AsyncController(2) as ac:

            def f1():
                em2 = Emitter()
                with WaitForSignal(em2.bing, 1000):
                    ac.start(em2.g, lambda x: x, QThread.currentThread())
                self.assertEqual(em2.thread, em2.result)

            em1 = Emitter()
            with WaitForSignal(em1.bing, 1000):
                ac.start(em1.g, f1)
Ejemplo n.º 14
0
 def test_11(self):
     # Don't test with one pooled thread -- this test expects at least two
     # threads.
     with AsyncController(2) as ac:
         em2 = Emitter()
         def f2():
             future = ac.start(em2.g, lambda x: x, QThread.currentThread())
             # The doneSignal won't be processed without an event loop. A
             # thread pool doesn't create one, so make our own to run ``g``.
             qe = QEventLoop()
             future._signalInvoker.doneSignal.connect(qe.exit)
             qe.exec_()
         with WaitForSignal(em2.bing, 1000):
             ac.start(None, f2)
         self.assertEquals(em2.thread, em2.result)
Ejemplo n.º 15
0
 def test_8(self):
     for _ in self.poolOnly:
         with AsyncController(_) as ac:
             q1 = Queue()
             q2 = Queue()
             q3 = Queue()
             em1 = Emitter(15, self.assertEqual)
             em2 = Emitter(16, self.assertEqual)
             em3 = Emitter(17, self.assertEqual)
             ac.start(em1.g, lambda: q1.get())
             ac.start(em2.g, lambda: q2.get())
             ac.start(em3.g, lambda: q3.get())
             sc = SignalCombiner()
             em1.bing.connect(sc.onBing)
             em2.bing.connect(sc.onBing)
             em3.bing.connect(sc.onBing)
             with WaitForSignal(sc.allEmitted, 1000):
                 q1.put(15)
                 q2.put(16)
                 q3.put(17)
Ejemplo n.º 16
0
    def test_12(self):
        for _ in self.singleThreadOnly:
            with AsyncController(_) as ac:
                q1a = Queue()
                q1b = Queue()
                def f1():
                    q1b.put(None)
                    q1a.get()
                em1 = Emitter()
                future1 = ac.start(em1.g, f1)
                q1b.get()
                self.assertEquals(future1.state, Future.STATE_RUNNING)

                future2 = ac.start(None, lambda: None)
                QTest.qWait(100)
                self.assertEquals(future2.state, Future.STATE_WAITING)
                with WaitForSignal(em1.bing, 1000):
                    future2.cancel()
                    q1a.put(None)
                self.assertEquals(future1.state, Future.STATE_FINISHED)
                QTest.qWait(100)
                self.assertEquals(future2.state, Future.STATE_CANCELED)
Ejemplo n.º 17
0
 def test_2(self):
     for _ in self.syncPoolAndThread:
         with AsyncController(_) as ac:
             em = Emitter(2, self.assertEqual)
             with WaitForSignal(em.bing, 1000):
                 ac.start(em.g, lambda: 2)
Ejemplo n.º 18
0
 def f1():
     em2 = Emitter()
     with WaitForSignal(em2.bing, 1000):
         ac.start(em2.g, lambda x: x, QThread.currentThread())
     self.assertEqual(em2.thread, em2.result)