コード例 #1
0
    def test_blocking_aqcuire(self, mock_acquire, mock_random):
        backend = mock.Mock(spec=LockBackend)
        key = "lock"
        duration = 60
        routing_key = None

        lock = Lock(backend, key, duration, routing_key)

        class MockTime:
            time = 0

            @classmethod
            def incr(cls, delta):
                cls.time += delta

        with patch("sentry.utils.locking.lock.time.monotonic",
                   side_effect=lambda: MockTime.time), patch(
                       "sentry.utils.locking.lock.time.sleep",
                       side_effect=MockTime.incr) as mock_sleep:
            with self.assertRaises(UnableToAcquireLock):
                lock.blocking_acquire(initial_delay=0.1, timeout=1, exp_base=2)

            # 0.0, 0.05, 0.15, 0.35, 0.75
            assert len(mock_acquire.mock_calls) == 5
            assert mock_sleep.mock_calls == [
                call(0.05), call(0.1),
                call(0.2), call(0.4)
            ]

        with patch("sentry.utils.locking.lock.Lock.acquire",
                   return_value="foo"):
            # Success case:
            assert lock.blocking_acquire(initial_delay=0, timeout=1) == "foo"
コード例 #2
0
 def get(self,
         key: str,
         duration: int,
         routing_key: Optional[str] = None) -> Lock:
     """
     Retrieve a ``Lock`` instance.
     """
     return Lock(self.backend, key, duration, routing_key)
コード例 #3
0
    def test_context_manager_interface(self):
        backend = mock.Mock(spec=LockBackend)
        key = "lock"
        duration = 60
        routing_key = None

        lock = Lock(backend, key, duration, routing_key)

        with lock.acquire():
            backend.acquire.assert_called_once_with(key, duration, routing_key)

        backend.release.assert_called_once_with(key, routing_key)
コード例 #4
0
    def test_procedural_interface(self):
        backend = mock.Mock(spec=LockBackend)
        key = "lock"
        duration = 60
        routing_key = None

        lock = Lock(backend, key, duration, routing_key)

        lock.acquire()
        backend.acquire.assert_called_once_with(key, duration, routing_key)

        lock.release()
        backend.release.assert_called_once_with(key, routing_key)

        backend.acquire.side_effect = Exception("Boom!")
        with pytest.raises(UnableToAcquireLock):
            lock.acquire()
コード例 #5
0
 def get(self, key, duration, routing_key=None):
     """
     Retrieve a ``Lock`` instance.
     """
     return Lock(self.backend, key, duration, routing_key)