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

    class Foo(object):
      pass

    compatibility.SetName(Foo, "Bar")
    self.assertEqual(compatibility.GetName(Foo), "Bar")
Exemplo n.º 2
0
  def testFunction(self):

    def Baz():
      pass

    compatibility.SetName(Baz, "Thud")
    self.assertEqual(compatibility.GetName(Baz), "Thud")
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 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.
Exemplo n.º 7
0
 def setUp(self):
     super().setUp()
     self.mock_fn = mock.Mock(wraps=lambda *_: random.random())
     compatibility.SetName(self.mock_fn,
                           "foo")  # Expected by functools.wraps.