'Check that logcontexts are set and restored correctly when
using the cache.'

def test_cache_logcontexts(self):

    complete_lookup = defer.Deferred()
    class Cls(object, ):
        @descriptors.cached()
        def fn(self, arg1):
            @defer.inlineCallbacks
            def inner_fn():
                with logcontext.PreserveLoggingContext():
                    (yield complete_lookup)
                defer.returnValue(1)
            return inner_fn()
    @defer.inlineCallbacks
    def do_lookup():
        with logcontext.LoggingContext() as c1:
            c1.name = 'c1'
            r = (yield obj.fn(1))
            self.assertEqual(logcontext.LoggingContext.current_context(), c1)
        defer.returnValue(r)
    def check_result(r):
        self.assertEqual(r, 1)
    obj = Cls()
    d1 = do_lookup()
    self.assertEqual(logcontext.LoggingContext.current_context(), logcontext.LoggingContext.sentinel)
    d1.addCallback(check_result)
    d2 = do_lookup()
    self.assertEqual(logcontext.LoggingContext.current_context(), logcontext.LoggingContext.sentinel)
    d2.addCallback(check_result)
    complete_lookup.callback(None)
    return defer.gatherResults([d1, d2])
