Ejemplo n.º 1
0
    def testUndoRedoCancellation(self):

        operations = []

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()
        with Gaffer.UndoScope(s):
            s["n"]["op1"].setValue(10)

        def f(canceller):

            operations.append("background")
            with self.assertRaises(IECore.Cancelled):
                while True:
                    s["n"]["sum"].getValue()
                    IECore.Canceller.check(canceller)

        c = s["n"].plugSetSignal().connect(
            lambda plug: operations.append("undo"))
        t = Gaffer.BackgroundTask(s["n"]["sum"], f)
        time.sleep(0.01)  # Give task a chance to start before we cancel it
        s.undo()

        self.assertEqual(operations, ["background", "undo"])

        del operations[:]
        c = s["n"].plugSetSignal().connect(
            lambda plug: operations.append("redo"))
        t = Gaffer.BackgroundTask(s["n"]["sum"], f)
        time.sleep(0.01)  # Give task a chance to start before we cancel it
        s.redo()

        self.assertEqual(operations, ["background", "redo"])
Ejemplo n.º 2
0
    def testMetadataChangesDontCancel(self):

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()

        operations = []

        def f(canceller):

            while True:
                try:
                    IECore.Canceller.check(canceller)
                except IECore.Cancelled:
                    operations.append("cancellation received")
                    return

        t = Gaffer.BackgroundTask(s["n"]["sum"], f)
        Gaffer.Metadata.registerValue(s["n"], "test", 10)
        Gaffer.Metadata.registerValue(s["n"]["sum"], "test", 10)

        time.sleep(0.25)
        operations.append("requesting explicit cancellation")
        t.cancelAndWait()

        self.assertEqual(
            operations,
            ["requesting explicit cancellation", "cancellation received"])
Ejemplo n.º 3
0
    def testManualCancellation(self):

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()

        def f(canceller):

            s["n"]["sum"].getValue()
            while True:
                IECore.Canceller.check(canceller)

        t = Gaffer.BackgroundTask(s["n"]["sum"], f)
        t.cancelAndWait()

        self.assertEqual(t.status(), t.Status.Cancelled)
Ejemplo n.º 4
0
    def testScriptNodeRemovalCancellation(self):
        def f(canceller):

            while True:
                IECore.Canceller.check(canceller)

        a = Gaffer.ApplicationRoot()
        a["scripts"]["s"] = Gaffer.ScriptNode()
        a["scripts"]["s"]["n"] = GafferTest.AddNode()

        # If not cancelled, this task will spin forever.
        t = Gaffer.BackgroundTask(a["scripts"]["s"]["n"]["sum"], f)
        # But removing the script from the application should cancel it.
        del a["scripts"]["s"]
        t.wait()
Ejemplo n.º 5
0
    def testWaitFor(self):

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()

        def f(canceller):

            while True:
                try:
                    IECore.Canceller.check(canceller)
                except IECore.Cancelled:
                    return

        t = Gaffer.BackgroundTask(s["n"]["sum"], f)

        startTime = time.time()
        self.assertFalse(t.waitFor(0.2))
        self.assertGreaterEqual(time.time(), startTime + 0.2)

        t.cancelAndWait()
Ejemplo n.º 6
0
    def testExceptionHandling(self):
        def f(canceller):

            raise RuntimeError("Oops!")

        originalMessageHandler = IECore.MessageHandler.getDefaultHandler()
        mh = IECore.CapturingMessageHandler()
        IECore.MessageHandler.setDefaultHandler(mh)

        try:
            t = Gaffer.BackgroundTask(None, f)
            t.wait()
        finally:
            IECore.MessageHandler.setDefaultHandler(originalMessageHandler)

        self.assertEqual(len(mh.messages), 1)
        self.assertEqual(mh.messages[0].level, IECore.Msg.Level.Error)
        self.assertEqual(mh.messages[0].context, "BackgroundTask")
        self.assertIn("Oops!", mh.messages[0].message)
        self.assertEqual(t.status(), t.Status.Errored)
Ejemplo n.º 7
0
    def testGraphEditCancellation(self):

        operations = []

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()
        c = s["n"].plugSetSignal().connect(
            lambda plug: operations.append("set"))

        def f(canceller):

            operations.append("background")
            with self.assertRaises(IECore.Cancelled):
                while True:
                    s["n"]["sum"].getValue()
                    IECore.Canceller.check(canceller)

        s["n"]["op1"].setValue(10)
        t = Gaffer.BackgroundTask(s["n"]["sum"], f)
        time.sleep(0.01)  # Give task a chance to start before we cancel it
        s["n"]["op1"].setValue(20)

        self.assertEqual(operations, ["set", "background", "set"])
Ejemplo n.º 8
0
    def testScriptNodeLifetime(self):
        def f(canceller, plug):

            while True:
                self.assertIsNotNone(plug.ancestor(Gaffer.ScriptNode))
                self.assertEqual(plug.getValue(), 0)
                IECore.Canceller.check(canceller)

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()

        t = Gaffer.BackgroundTask(s["n"]["sum"],
                                  functools.partial(f, plug=s["n"]["sum"]))

        # Drop our reference to the script, and sleep for
        # a bit to demonstrate that the background task can
        # still operate.
        del s
        time.sleep(0.25)

        # Cancel the task. This should drop the final reference
        # to the script.
        t.cancelAndWait()