Exemplo n.º 1
0
    def testPrecomputedHashOptimisation(self):

        n = GafferTest.CachingTestNode()
        n["in"].setValue("a")

        a1 = n["out"].getValue(_copy=False)
        self.assertEqual(a1, IECore.StringData("a"))
        self.assertEqual(n.numHashCalls, 1)

        # We apply some leeway in our test for how many hash calls are
        # made - a good ValuePlug implementation will probably avoid
        # unecessary repeated calls in most cases, but it's not
        # what this unit test is about.
        a2 = n["out"].getValue(_copy=False)
        self.assertTrue(a2.isSame(a1))
        self.assertTrue(n.numHashCalls == 1 or n.numHashCalls == 2)

        h = n["out"].hash()
        self.assertTrue(n.numHashCalls >= 1 and n.numHashCalls <= 3)
        numHashCalls = n.numHashCalls

        # What we care about is that calling getValue() with a precomputed hash
        # definitely doesn't recompute the hash again.
        a3 = n["out"].getValue(_copy=False, _precomputedHash=h)
        self.assertEqual(n.numHashCalls, numHashCalls)
        self.assertTrue(a3.isSame(a1))
Exemplo n.º 2
0
    def testDisableCaching(self):

        n = GafferTest.CachingTestNode()

        n["in"].setValue("d")

        v1 = n["out"].getValue(_copy=False)
        v2 = n["out"].getValue(_copy=False)

        self.assertEqual(v1, v2)
        self.assertEqual(v1, IECore.StringData("d"))

        # the objects should be one and the same, as the second computation
        # should have shortcut and returned a cached result.
        self.assertTrue(v1.isSame(v2))

        n["out"].setFlags(Gaffer.Plug.Flags.Cacheable, False)
        v3 = n["out"].getValue(_copy=False)

        self.assertEqual(v3, IECore.StringData("d"))
        self.assertEqual(v3, v1)

        # we disabled caching, so the two values should
        # be distinct objects, even though they are equal.
        self.assertFalse(v3.isSame(v1))
Exemplo n.º 3
0
    def testUncacheabilityPropagates(self):

        n = GafferTest.CachingTestNode()
        n["in"].setValue("pig")

        p1 = Gaffer.ObjectPlug("p1", Gaffer.Plug.Direction.In,
                               IECore.IntData(10))
        p2 = Gaffer.ObjectPlug("p2", Gaffer.Plug.Direction.In,
                               IECore.IntData(20))
        p3 = Gaffer.ObjectPlug("p3", Gaffer.Plug.Direction.In,
                               IECore.IntData(30))

        p1.setInput(n["out"])
        p2.setInput(p1)
        p3.setInput(p2)

        o2 = p2.getValue(_copy=False)
        o3 = p3.getValue(_copy=False)

        self.assertEqual(o2, IECore.StringData("pig"))
        self.assertEqual(o3, IECore.StringData("pig"))
        self.failUnless(o2.isSame(o3))  # they share cache entries

        n["out"].setFlags(Gaffer.Plug.Flags.Cacheable, False)

        o2 = p2.getValue(_copy=False)
        o3 = p3.getValue(_copy=False)

        self.assertEqual(o2, IECore.StringData("pig"))
        self.assertEqual(o3, IECore.StringData("pig"))
        self.failIf(o2.isSame(o3))  # they shouldn't share cache entries
Exemplo n.º 4
0
    def testExecuteObjectWriter(self):

        s = Gaffer.ScriptNode()

        s["object"] = GafferTest.CachingTestNode()
        s["write"] = Gaffer.ObjectWriter()
        s["write"]["in"].setInput(s["object"]["out"])
        s["write"]["fileName"].setValue(self.__outputFileSeq.fileName)

        s["fileName"].setValue(self.__scriptFileName)
        s.save()

        self.failIf(os.path.exists(self.__outputFileSeq.fileNameForFrame(1)))
        p = subprocess.Popen(
            "gaffer execute " + self.__scriptFileName,
            shell=True,
            stderr=subprocess.PIPE,
        )
        p.wait()

        error = "".join(p.stderr.readlines())
        self.failUnless(error == "")
        self.failUnless(
            os.path.exists(self.__outputFileSeq.fileNameForFrame(1)))
        self.failIf(p.returncode)
Exemplo n.º 5
0
    def testCacheMemoryLimit(self):

        n = GafferTest.CachingTestNode()

        n["in"].setValue("d")

        v1 = n["out"].getValue(_copy=False)
        v2 = n["out"].getValue(_copy=False)

        self.assertEqual(v1, v2)
        self.assertEqual(v1, IECore.StringData("d"))

        # the objects should be one and the same, as the second computation
        # should have shortcut and returned a cached result.
        self.failUnless(v1.isSame(v2))

        Gaffer.ValuePlug.setCacheMemoryLimit(0)

        v3 = n["out"].getValue(_copy=False)
        self.assertEqual(v3, IECore.StringData("d"))

        # the objects should be different, as we cleared the cache.
        self.failIf(v3.isSame(v2))

        Gaffer.ValuePlug.setCacheMemoryLimit(self.__originalCacheMemoryLimit)

        v1 = n["out"].getValue(_copy=False)
        v2 = n["out"].getValue(_copy=False)

        self.assertEqual(v1, v2)
        self.assertEqual(v1, IECore.StringData("d"))

        # the objects should be one and the same, as we reenabled the cache.
        self.failUnless(v1.isSame(v2))
Exemplo n.º 6
0
    def testContextParameter(self):

        s = Gaffer.ScriptNode()
        s["string"] = GafferTest.CachingTestNode()
        s["e"] = Gaffer.Expression()
        s["e"].setExpression(
            "parent['string']['in'] = '{} {}'.format( context.get( 'valueOne', 0 ), context.get( 'valueTwo', 0 ) )"
        )
        s["write"] = Gaffer.ObjectWriter()
        s["write"]["in"].setInput(s["string"]["out"])
        s["write"]["fileName"].setValue(self.__outputFileSeq.fileName)

        s["fileName"].setValue(self.__scriptFileName)
        s.save()

        self.failIf(os.path.exists(self.__outputFileSeq.fileNameForFrame(1)))
        p = subprocess.Popen(
            "gaffer execute " + self.__scriptFileName +
            " -context -valueOne 1 -valueTwo 2",
            shell=True,
            stderr=subprocess.PIPE,
        )
        p.wait()

        error = "".join(p.stderr.readlines())
        self.assertEqual(error, "")
        self.failIf(p.returncode)
        self.failUnless(
            os.path.exists(self.__outputFileSeq.fileNameForFrame(1)))

        string = IECore.ObjectReader(
            self.__outputFileSeq.fileNameForFrame(1)).read()
        self.failUnless(string.isInstanceOf(IECore.StringData))
        self.assertEqual(string.value, "1 2")
Exemplo n.º 7
0
    def testFramesParameter(self):

        s = Gaffer.ScriptNode()
        s["object"] = GafferTest.CachingTestNode()
        s["write"] = Gaffer.ObjectWriter()
        s["write"]["in"].setInput(s["object"]["out"])
        s["write"]["fileName"].setValue(self.__outputFileSeq.fileName)

        s["fileName"].setValue(self.__scriptFileName)
        s.save()

        frames = IECore.FrameList.parse("1-5")
        for f in frames.asList():
            self.failIf(
                os.path.exists(self.__outputFileSeq.fileNameForFrame(f)))

        p = subprocess.Popen(
            "gaffer execute " + self.__scriptFileName + " -frames " +
            str(frames),
            shell=True,
            stderr=subprocess.PIPE,
        )
        p.wait()

        error = "".join(p.stderr.readlines())
        self.failUnless(error == "")
        self.failIf(p.returncode)
        for f in frames.asList():
            self.failUnless(
                os.path.exists(self.__outputFileSeq.fileNameForFrame(f)))
Exemplo n.º 8
0
    def testGetValueIfCached(self):

        n = GafferTest.CachingTestNode()
        n["in"].setValue("TypedObjectPlugTest.testGetValueIfCached")

        self.assertTrue(n["out"].getValueIfCached() is None)
        v = n["out"].getValue()
        self.assertIsInstance(v, IECore.StringData)
        self.assertEqual(v.value, n["in"].getValue())
        self.assertEqual(n["out"].getValueIfCached(), v)
    def testPrecomputedHashOptimisation(self):

        n = GafferTest.CachingTestNode()
        n["in"].setValue("a")

        a1 = n["out"].getValue(_copy=False)
        self.assertEqual(a1, IECore.StringData("a"))
        self.assertEqual(n.numHashCalls, 1)

        a2 = n["out"].getValue(_copy=False)
        self.assertTrue(a2.isSame(a1))
        self.assertEqual(n.numHashCalls, 2)

        h = n["out"].hash()
        self.assertEqual(n.numHashCalls, 3)

        a3 = n["out"].getValue(_copy=False, _precomputedHash=h)
        self.assertEqual(n.numHashCalls, 3)
        self.assertTrue(a3.isSame(a1))
Exemplo n.º 10
0
    def testHash(self):

        c = Gaffer.Context()
        c.setFrame(1)
        c2 = Gaffer.Context()
        c2.setFrame(2)

        s = Gaffer.ScriptNode()
        s["n"] = GafferCortex.ObjectWriter()

        # no file produces no effect
        with c:
            self.assertEqual(s["n"]["task"].hash(), IECore.MurmurHash())

        # no input object produces no effect
        with c:
            s["n"]["fileName"].setValue(self.__exrFileName)
            self.assertEqual(s["n"]["task"].hash(), IECore.MurmurHash())

        # now theres a file and object, we get some output
        with c:
            s["o"] = GafferTest.CachingTestNode()
            s["n"]["in"].setInput(s["o"]["out"])
            self.assertNotEqual(s["n"]["task"].hash(), IECore.MurmurHash())

        # output doesn't vary by time
        with c:
            h1 = s["n"]["task"].hash()
        with c2:
            h2 = s["n"]["task"].hash()

        self.assertEqual(h1, h2)

        # output varies by time since the file name does
        s["n"]["fileName"].setValue(self.__exrSequence.fileName)
        with c:
            h1 = s["n"]["task"].hash()
        with c2:
            h2 = s["n"]["task"].hash()

        self.assertNotEqual(h1, h2)