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 func(i, sleep=False): with logcontext.LoggingContext("func(%s)" % i) as lc: with (yield linearizer.queue("")): self.assertEqual(logcontext.LoggingContext.current_context(), lc) if sleep: yield Clock(reactor).sleep(0) self.assertEqual(logcontext.LoggingContext.current_context(), lc)
def test_cache(self): class Cls(object): def __init__(self): self.mock = mock.Mock() @descriptors.cached() def fn(self, arg1, arg2): pass @descriptors.cachedList("fn", "args1", inlineCallbacks=True) def list_fn(self, args1, arg2): assert ( logcontext.LoggingContext.current_context().request == "c1" ) # we want this to behave like an asynchronous function yield run_on_reactor() assert ( logcontext.LoggingContext.current_context().request == "c1" ) defer.returnValue(self.mock(args1, arg2)) with logcontext.LoggingContext() as c1: c1.request = "c1" obj = Cls() obj.mock.return_value = {10: 'fish', 20: 'chips'} d1 = obj.list_fn([10, 20], 2) self.assertEqual( logcontext.LoggingContext.current_context(), logcontext.LoggingContext.sentinel, ) r = yield d1 self.assertEqual( logcontext.LoggingContext.current_context(), c1 ) obj.mock.assert_called_once_with([10, 20], 2) self.assertEqual(r, {10: 'fish', 20: 'chips'}) obj.mock.reset_mock() # a call with different params should call the mock again obj.mock.return_value = {30: 'peas'} r = yield obj.list_fn([20, 30], 2) obj.mock.assert_called_once_with([30], 2) self.assertEqual(r, {20: 'chips', 30: 'peas'}) obj.mock.reset_mock() # all the values should now be cached r = yield obj.fn(10, 2) self.assertEqual(r, 'fish') r = yield obj.fn(20, 2) self.assertEqual(r, 'chips') r = yield obj.fn(30, 2) self.assertEqual(r, 'peas') r = yield obj.list_fn([10, 20, 30], 2) obj.mock.assert_not_called() self.assertEqual(r, {10: 'fish', 20: 'chips', 30: 'peas'})
def do_lookup(): with logcontext.LoggingContext() as c1: c1.name = "c1" try: yield obj.fn(1) self.fail("No exception thrown") except SynapseError: pass self.assertEqual(logcontext.LoggingContext.current_context(), c1)
def test_logcontext_is_preserved_on_cancellation(self): blocking_was_cancelled = [False] @defer.inlineCallbacks def blocking(): non_completing_d = Deferred() with logcontext.PreserveLoggingContext(): try: yield non_completing_d except CancelledError: blocking_was_cancelled[0] = True raise with logcontext.LoggingContext("one") as context_one: # the errbacks should be run in the test logcontext def errback(res, deferred_name): self.assertIs( LoggingContext.current_context(), context_one, "errback %s run in unexpected logcontext %s" % ( deferred_name, LoggingContext.current_context(), ) ) return res original_deferred = blocking() original_deferred.addErrback(errback, "orig") timing_out_d = timeout_deferred(original_deferred, 1.0, self.clock) self.assertNoResult(timing_out_d) self.assertIs(LoggingContext.current_context(), LoggingContext.sentinel) timing_out_d.addErrback(errback, "timingout") self.clock.pump((1.0, )) self.assertTrue( blocking_was_cancelled[0], "non-completing deferred was not cancelled", ) self.failureResultOf(timing_out_d, defer.TimeoutError, ) self.assertIs(LoggingContext.current_context(), context_one)