def test_getExpectedCookie_caches(self):
        bf = MockBuilderFactory(MockBuilder(), FakeBuildQueue())
        scanner = SlaveScanner('mock',
                               bf,
                               BufferLogger(),
                               interactor_factory=FakeMethod(None),
                               slave_factory=FakeMethod(None),
                               behavior_factory=FakeMethod(TrivialBehavior()))

        def assertCounts(expected):
            self.assertEqual(expected,
                             (scanner.interactor_factory.call_count,
                              scanner.behavior_factory.call_count,
                              scanner.builder_factory.get_call_count))

        # The first call will get a Builder and a BuildFarmJobBehavior.
        assertCounts((0, 0, 0))
        cookie1 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual('trivial', cookie1)
        assertCounts((0, 1, 1))

        # A second call with the same BuildQueue will not reretrieve them.
        cookie2 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual(cookie1, cookie2)
        assertCounts((0, 1, 1))

        # But a call with a new BuildQueue will regrab.
        bf.updateTestData(bf._builder, FakeBuildQueue())
        cookie3 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual(cookie1, cookie3)
        assertCounts((0, 2, 2))

        # And unsetting the BuildQueue returns None again.
        bf.updateTestData(bf._builder, None)
        cookie4 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertIs(None, cookie4)
        assertCounts((0, 2, 2))
    def test_getExpectedCookie_caches(self):
        bf = MockBuilderFactory(MockBuilder(), FakeBuildQueue())
        scanner = SlaveScanner(
            'mock', bf, BufferLogger(), interactor_factory=FakeMethod(None),
            slave_factory=FakeMethod(None),
            behavior_factory=FakeMethod(TrivialBehavior()))

        def assertCounts(expected):
            self.assertEqual(
                expected,
                (scanner.interactor_factory.call_count,
                 scanner.behavior_factory.call_count,
                 scanner.builder_factory.get_call_count))

        # The first call will get a Builder and a BuildFarmJobBehavior.
        assertCounts((0, 0, 0))
        cookie1 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual('trivial', cookie1)
        assertCounts((0, 1, 1))

        # A second call with the same BuildQueue will not reretrieve them.
        cookie2 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual(cookie1, cookie2)
        assertCounts((0, 1, 1))

        # But a call with a new BuildQueue will regrab.
        bf.updateTestData(bf._builder, FakeBuildQueue())
        cookie3 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertEqual(cookie1, cookie3)
        assertCounts((0, 2, 2))

        # And unsetting the BuildQueue returns None again.
        bf.updateTestData(bf._builder, None)
        cookie4 = scanner.getExpectedCookie(bf.getVitals('foo'))
        self.assertIs(None, cookie4)
        assertCounts((0, 2, 2))