def test_consume(self, fudge_time):

        fudge_time.is_callable()
        fudge_time.returns(0)
        bucket = PersistentTokenBucket(2)

        assert_that(bucket, validly_provides(interfaces.ITokenBucket))

        # at time 0, the bucket has two tokens in it
        assert_that(bucket.consume(), is_true())
        assert_that(bucket.consume(), is_true())

        # Which are now gone
        assert_that(bucket.consume(), is_false())

        # If we strobe the clock forward, we can get another token, since
        # we are refilling at one per second
        fudge_time.returns(1)

        assert_that(bucket.consume(), is_true())
        assert_that(bucket.consume(), is_false())

        # skip forward two seconds, and we can consume both tokens again
        fudge_time.returns(3)
        assert_that(bucket.consume(2), is_true())
        assert_that(bucket.consume(), is_false())

        # cover
        assert_that(repr(bucket), is_('PersistentTokenBucket(2.0,1.0)'))
Beispiel #2
0
    def test_isBroken_no_activate(self):
        class O(object):
            pass

        o = O()
        assert_that(isBroken(o), is_false())

        interface.alsoProvides(o, IBroken)
        assert_that(isBroken(o), is_true())
    def test_wait(self, fudge_time, fudge_sleep):

        fudge_time.is_callable()
        fudge_time.returns(0)
        bucket = PersistentTokenBucket(2)

        # at time 0, the bucket has two tokens in it
        assert_that(bucket.wait_for_token(), is_true())
        assert_that(bucket.wait_for_token(), is_true())

        # Which are now gone
        assert_that(bucket.consume(), is_false())

        fudge_sleep.is_callable()

        def _sleep(how_long):
            # If we strobe the clock forward, we can get another token, since
            # we are refilling at one per second
            assert_that(how_long, is_(1.0))
            fudge_time.returns(1)

        fudge_sleep._callable.call_replacement = _sleep
        # Sleep gets called, strobes the clock, and we move forward
        assert_that(bucket.wait_for_token(), is_true())
Beispiel #4
0
    def test_ext_accept_external_id_tagged_value_true(self):
        # If there is no id field, the value is false

        class I(interface.Interface):
            id = interface.Attribute("An id")
            id.setTaggedValue('__external_accept_id__', True)

        @interface.implementer(I)
        class O(object):
            pass

        ext_self = O()
        inst = self._makeOne(ext_self, iface_upper_bound=I)

        assert_that(inst._ext_accept_external_id(ext_self, None), is_true())
Beispiel #5
0
    def test_activate_errors(self):
        from ZODB.POSException import POSKeyError
        class O(object):
            raises = TypeError

            def _p_activate(self):
                raise self.raises()

        o = O()

        # Raising a TypeError doesn't do anything
        assert_that(isBroken(o), is_false())

        # POSKeyError does
        o.raises = POSKeyError
        assert_that(isBroken(o), is_true())


        # other exceptions are passed
        o.raises = KeyError
        self.assertRaises(KeyError, isBroken, o)
 def test_isSyntheticKey(self):
     assert_that(isSyntheticKey('OID'), is_true())
     assert_that(isSyntheticKey('key'), is_false())
Beispiel #7
0
 def test_isBroken_no_obj(self):
     assert_that(isBroken(None, None), is_false())
     # WTF
     assert_that(isBroken(None, 'foo'), is_true())