def testClass(self): class Foo(object): pass compatibility.SetName(Foo, "Bar") self.assertEqual(compatibility.GetName(Foo), "Bar")
def testFunction(self): def Baz(): pass compatibility.SetName(Baz, "Thud") self.assertEqual(compatibility.GetName(Baz), "Thud")
def testDecoratedFunctionsAreWaitedForPerArguments(self): event = threading.Event() def Fn(x): if x != 42: event.wait() return x mock_fn = mock.Mock(wraps=Fn) compatibility.SetName(mock_fn, "foo") # Expected by functools.wraps. decorated = cache.WithLimitedCallFrequency( rdfvalue.DurationSeconds("30s"))(mock_fn) def T(): decorated(1) t = threading.Thread(target=T) t.start() try: # This should return immediately. There's another function call # in progress, but with different arguments, so no locking should occur. # I.e. decorated(1) and decorated(42) shouldn't influence each other. decorated(42) finally: event.set() t.join() self.assertEqual(mock_fn.call_count, 2)
def testPropagatesExceptions(self): mock_fn = mock.Mock(side_effect=ValueError()) compatibility.SetName(mock_fn, "foo") # Expected by functools.wraps. decorated = cache.WithLimitedCallFrequency( rdfvalue.Duration.From(30, rdfvalue.SECONDS))(mock_fn) with self.assertRaises(ValueError): decorated()
def testExceptionIsNotCached(self): mock_fn = mock.Mock(side_effect=ValueError()) compatibility.SetName(mock_fn, "foo") # Expected by functools.wraps. decorated = cache.WithLimitedCallFrequency( rdfvalue.Duration.From(30, rdfvalue.SECONDS))(mock_fn) for _ in range(10): with self.assertRaises(ValueError): decorated() self.assertEqual(mock_fn.call_count, 10)
def setUp(self): super(WithLimitedCallFrequencyTest, self).setUp() self.mock_fn = mock.Mock(wraps=lambda *_: random.random()) compatibility.SetName(self.mock_fn, "foo") # Expected by functools.wraps.
def setUp(self): super().setUp() self.mock_fn = mock.Mock(wraps=lambda *_: random.random()) compatibility.SetName(self.mock_fn, "foo") # Expected by functools.wraps.